Add dark mode, export preview, and keyboard navigation
- Add theme store with light/dark/system modes and ThemeToggle component - Prevent flash of wrong theme on initial load via inline script - Add ExportPreviewModal for previewing session exports before download - Add copy-to-clipboard functionality to session export - Implement keyboard shortcuts for tree navigation (1-9 options, Esc back, Enter continue) - Display keyboard hints in tree navigation UI - Fix findNode to safely handle undefined structure parameter - Update page title to "Apoklisis" Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useParams, useNavigate, useLocation } from 'react-router-dom'
|
||||
import { treesApi, sessionsApi } from '@/api'
|
||||
import { useTreeNavigationShortcuts } from '@/hooks/useKeyboardShortcuts'
|
||||
import type { Tree, Session, DecisionRecord, TreeStructure } from '@/types'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
@@ -80,7 +81,8 @@ export function TreeNavigationPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const findNode = (nodeId: string, structure: TreeStructure = tree?.tree_structure!): TreeStructure | null => {
|
||||
const findNode = (nodeId: string, structure?: TreeStructure): TreeStructure | null => {
|
||||
if (!structure) return null
|
||||
if (structure.id === nodeId) return structure
|
||||
if (structure.children) {
|
||||
for (const child of structure.children) {
|
||||
@@ -91,15 +93,16 @@ export function TreeNavigationPage() {
|
||||
return null
|
||||
}
|
||||
|
||||
// Handler functions - defined before hook call to avoid temporal dead zone
|
||||
const handleSelectOption = async (_optionId: string, optionLabel: string, nextNodeId: string) => {
|
||||
if (!session || !tree) return
|
||||
|
||||
const currentNode = findNode(currentNodeId)
|
||||
if (!currentNode) return
|
||||
const node = findNode(currentNodeId, tree.tree_structure)
|
||||
if (!node) return
|
||||
|
||||
const newDecision: DecisionRecord = {
|
||||
node_id: currentNodeId,
|
||||
question: currentNode.question || null,
|
||||
question: node.question || null,
|
||||
answer: optionLabel,
|
||||
action_performed: null,
|
||||
notes: notes || null,
|
||||
@@ -130,26 +133,26 @@ export function TreeNavigationPage() {
|
||||
const handleContinue = async (actionPerformed?: string) => {
|
||||
if (!session || !tree) return
|
||||
|
||||
const currentNode = findNode(currentNodeId)
|
||||
if (!currentNode || !currentNode.next_node_id) return
|
||||
const node = findNode(currentNodeId, tree.tree_structure)
|
||||
if (!node || !node.next_node_id) return
|
||||
|
||||
const newDecision: DecisionRecord = {
|
||||
node_id: currentNodeId,
|
||||
question: null,
|
||||
answer: null,
|
||||
action_performed: actionPerformed || currentNode.title || 'Action completed',
|
||||
action_performed: actionPerformed || node.title || 'Action completed',
|
||||
notes: notes || null,
|
||||
automation_used: false,
|
||||
timestamp: new Date().toISOString(),
|
||||
attachments: [],
|
||||
}
|
||||
|
||||
const newPath = [...pathTaken, currentNode.next_node_id]
|
||||
const newPath = [...pathTaken, node.next_node_id]
|
||||
const newDecisions = [...decisions, newDecision]
|
||||
|
||||
setPathTaken(newPath)
|
||||
setDecisions(newDecisions)
|
||||
setCurrentNodeId(currentNode.next_node_id)
|
||||
setCurrentNodeId(node.next_node_id)
|
||||
setNotes('')
|
||||
|
||||
try {
|
||||
@@ -163,18 +166,18 @@ export function TreeNavigationPage() {
|
||||
}
|
||||
|
||||
const handleComplete = async () => {
|
||||
if (!session) return
|
||||
if (!session || !tree) return
|
||||
setIsCompleting(true)
|
||||
setError(null)
|
||||
try {
|
||||
// Add final decision
|
||||
const currentNode = findNode(currentNodeId)
|
||||
if (currentNode) {
|
||||
const node = findNode(currentNodeId, tree.tree_structure)
|
||||
if (node) {
|
||||
const finalDecision: DecisionRecord = {
|
||||
node_id: currentNodeId,
|
||||
question: null,
|
||||
answer: null,
|
||||
action_performed: currentNode.title || 'Session completed',
|
||||
action_performed: node.title || 'Session completed',
|
||||
notes: notes || null,
|
||||
automation_used: false,
|
||||
timestamp: new Date().toISOString(),
|
||||
@@ -204,6 +207,31 @@ export function TreeNavigationPage() {
|
||||
setCurrentNodeId(newPath[newPath.length - 1])
|
||||
}
|
||||
|
||||
// Compute current node for keyboard shortcuts (must be before any returns for hooks rules)
|
||||
const currentNode = tree ? findNode(currentNodeId, tree.tree_structure) : null
|
||||
const currentOptions = currentNode?.options || []
|
||||
|
||||
// Keyboard shortcuts - must be called unconditionally (React hooks rules)
|
||||
useTreeNavigationShortcuts({
|
||||
onSelectOption: (index) => {
|
||||
const option = currentOptions[index]
|
||||
if (option && session && tree) {
|
||||
handleSelectOption(option.id, option.label, option.next_node_id)
|
||||
}
|
||||
},
|
||||
onGoBack: handleGoBack,
|
||||
onContinue: () => {
|
||||
if (currentNode?.type === 'action' && currentNode.next_node_id) {
|
||||
handleContinue()
|
||||
} else if (currentNode?.type === 'solution') {
|
||||
handleComplete()
|
||||
}
|
||||
},
|
||||
optionCount: currentOptions.length,
|
||||
canGoBack: pathTaken.length > 1 && !showMetadataForm && !isLoading,
|
||||
canContinue: !showMetadataForm && !isLoading && (currentNode?.type === 'action' || currentNode?.type === 'solution'),
|
||||
})
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
@@ -289,8 +317,6 @@ export function TreeNavigationPage() {
|
||||
)
|
||||
}
|
||||
|
||||
const currentNode = findNode(currentNodeId)
|
||||
|
||||
if (!currentNode) {
|
||||
return (
|
||||
<div className="container mx-auto px-4 py-8">
|
||||
@@ -357,16 +383,22 @@ export function TreeNavigationPage() {
|
||||
<p className="mb-4 text-sm text-muted-foreground">{currentNode.help_text}</p>
|
||||
)}
|
||||
<div className="mb-4 space-y-2">
|
||||
{currentNode.options?.map((option) => (
|
||||
{currentNode.options?.map((option, index) => (
|
||||
<button
|
||||
key={option.id}
|
||||
onClick={() => handleSelectOption(option.id, option.label, option.next_node_id)}
|
||||
className={cn(
|
||||
'w-full rounded-md border border-input p-3 text-left transition-colors',
|
||||
'hover:border-primary hover:bg-accent'
|
||||
'hover:border-primary hover:bg-accent',
|
||||
'flex items-center gap-3'
|
||||
)}
|
||||
>
|
||||
{option.label}
|
||||
{index < 9 && (
|
||||
<span className="flex h-6 w-6 shrink-0 items-center justify-center rounded bg-muted text-xs font-medium text-muted-foreground">
|
||||
{index + 1}
|
||||
</span>
|
||||
)}
|
||||
<span>{option.label}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
@@ -476,6 +508,18 @@ export function TreeNavigationPage() {
|
||||
← Go back
|
||||
</button>
|
||||
)}
|
||||
|
||||
{/* Keyboard Shortcuts Hint */}
|
||||
<div className="mt-4 border-t border-border pt-3 text-xs text-muted-foreground">
|
||||
<span className="font-medium">Keyboard:</span>{' '}
|
||||
{currentNode.type === 'decision' && currentOptions.length > 0 && (
|
||||
<span>1-{Math.min(currentOptions.length, 9)} select option</span>
|
||||
)}
|
||||
{pathTaken.length > 1 && <span>, Esc go back</span>}
|
||||
{(currentNode.type === 'action' || currentNode.type === 'solution') && (
|
||||
<span>, Enter {currentNode.type === 'solution' ? 'complete' : 'continue'}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user