import { useRef, useState } from 'react'; import { useDialogueStore } from '../../store/dialogueStore'; import { parseDialogueFile } from '../../utils/fileIO'; import { useValidation } from '../../hooks/useValidation'; import styles from './LeftPanel.module.css'; export function LeftPanel() { const { file, fileName, selectedDialogueId, loadFile, selectDialogue, createDialogue, deleteDialogue, duplicateDialogue, exportFile } = useDialogueStore(); const { issuesByNodeId } = useValidation(); const fileInputRef = useRef(null); const [newId, setNewId] = useState(''); const [creating, setCreating] = useState(false); const [error, setError] = useState(''); function handleFileChange(e: React.ChangeEvent) { const f = e.target.files?.[0]; if (!f) return; const reader = new FileReader(); reader.onload = (ev) => { try { const parsed = parseDialogueFile(ev.target?.result as string); loadFile(parsed, f.name); setError(''); } catch (err) { setError(String(err)); } }; reader.readAsText(f); e.target.value = ''; } function handleCreate() { if (!newId.trim()) return; if (file?.dialogues.some(d => d.id === newId.trim())) { setError(`ID "${newId.trim()}" already exists`); return; } createDialogue(newId.trim()); setNewId(''); setCreating(false); setError(''); } function dialogueHasIssues(dialogueId: string) { // Simple check: are there any issues for nodes in this dialogue? // We only have current dialogue issues so we check if this is selected return selectedDialogueId === dialogueId && Object.keys(issuesByNodeId).length > 0; } function dialogueHasErrors(dialogueId: string) { return selectedDialogueId === dialogueId && Object.values(issuesByNodeId).some(list => list.some(i => i.severity === 'error')); } return (
Dialogues {file && {fileName}}
{file && ( )}
{error &&
{error}
}
{!file && (
Load a JSON file to start
)} {file?.dialogues.map(d => { const hasErr = dialogueHasErrors(d.id); const hasWarn = !hasErr && dialogueHasIssues(d.id); return (
selectDialogue(d.id)} > {hasErr && ●} {hasWarn && ●} {d.mobileMode && 📱} {d.id}
); })}
{creating ? (
setNewId(e.target.value)} placeholder="dialogue_id" onKeyDown={e => { if (e.key === 'Enter') handleCreate(); if (e.key === 'Escape') { setCreating(false); setNewId(''); } }} autoFocus />
) : ( file && ( ) )}
); }