diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 458342e..15bc98c 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -15,8 +15,8 @@ import { ReadCustomInstructionsFile, WriteCustomInstructionsFile, } from '../wailsjs/go/main/App'; -import { TaskTypeEditModal } from './components/TaskTypeEditModal'; -import { CustomInstructionsEditModal } from './components/CustomInstructionsEditModal'; +import TaskTypeEditModal from './components/TaskTypeEditModal'; +import CustomInstructionsEditModal from './components/CustomInstructionsEditModal'; import { TaskTypeOption, CustomInstructionOption } from './types'; import { v4 as uuidv4 } from 'uuid'; import { CheckedState } from '@radix-ui/react-checkbox'; diff --git a/frontend/src/components/CustomInstructionsEditModal.tsx b/frontend/src/components/CustomInstructionsEditModal.tsx index f25b573..803705d 100644 --- a/frontend/src/components/CustomInstructionsEditModal.tsx +++ b/frontend/src/components/CustomInstructionsEditModal.tsx @@ -1,6 +1,4 @@ -// CustomInstructionsEditModal.tsx - -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { Dialog, DialogContent, @@ -11,8 +9,13 @@ import { import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; -import { CustomInstructionOption } from '../types'; -import { v4 as uuidv4 } from 'uuid'; +import { Select, SelectTrigger, SelectContent, SelectItem, SelectValue } from '@/components/ui/select'; + +interface CustomInstructionOption { + id: string; + label: string; + description: string; +} interface CustomInstructionsEditModalProps { isOpen: boolean; @@ -21,86 +24,172 @@ interface CustomInstructionsEditModalProps { onSave: (options: CustomInstructionOption[]) => void; } -export function CustomInstructionsEditModal({ +export default function CustomInstructionsEditModal({ isOpen, onClose, options, onSave, }: CustomInstructionsEditModalProps) { - const [localOptions, setLocalOptions] = useState(options); - - const handleAddOption = () => { - const newOption: CustomInstructionOption = { - id: uuidv4(), - label: '', - description: '', - }; - setLocalOptions([...localOptions, newOption]); - }; + // State management + const [localOptions, setLocalOptions] = useState([]); + const [selectedItemId, setSelectedItemId] = useState('new'); + const [label, setLabel] = useState(''); + const [description, setDescription] = useState(''); + const [error, setError] = useState(''); + + // Initialize local options when modal opens + useEffect(() => { + setLocalOptions(options); + }, [options]); + + // Handle selection changes + useEffect(() => { + setError(''); + if (selectedItemId === 'new') { + setLabel(''); + setDescription(''); + } else { + const selectedItem = localOptions.find((option) => option.id === selectedItemId); + if (selectedItem) { + setLabel(selectedItem.label); + setDescription(selectedItem.description); + } + } + }, [selectedItemId, localOptions]); + + const handleSave = () => { + if (label.trim() === '') { + setError('Label is required'); + return; + } + + if (description.trim() === '') { + setError('Description is required'); + return; + } + + setError(''); - const handleOptionChange = ( - id: string, - field: keyof CustomInstructionOption, - value: string - ) => { - setLocalOptions((prevOptions) => - prevOptions.map((option) => - option.id === id ? { ...option, [field]: value } : option - ) - ); + if (selectedItemId === 'new') { + // Add new item + const newOption: CustomInstructionOption = { + id: crypto.randomUUID(), + label: label.trim(), + description: description.trim(), + }; + setLocalOptions((prev) => [...prev, newOption]); + } else { + // Update existing item + setLocalOptions((prev) => + prev.map((option) => + option.id === selectedItemId + ? { ...option, label: label.trim(), description: description.trim() } + : option + ) + ); + } + + // Reset form + setSelectedItemId('new'); + setLabel(''); + setDescription(''); }; - const handleDeleteOption = (id: string) => { - setLocalOptions((prevOptions) => prevOptions.filter((option) => option.id !== id)); + const handleDelete = () => { + if (selectedItemId !== 'new') { + setLocalOptions((prev) => prev.filter((option) => option.id !== selectedItemId)); + setSelectedItemId('new'); + setLabel(''); + setDescription(''); + } }; - const handleSave = () => { + const handleModalSave = () => { onSave(localOptions); onClose(); }; return ( - + - Edit Custom Instructions + Edit Custom Instructions -
- {localOptions.map((option) => ( -
-
- - handleOptionChange(option.id, 'label', e.target.value) - } - /> - -
-