|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import { Button, Dialog, DialogRoot, Input } from '@helpwave/hightide' |
| 3 | +import { useScaffoldTranslation } from '../i18n/ScaffoldTranslationContext' |
| 4 | +import type { ScaffoldNodeData } from '../lib/scaffoldGraph' |
| 5 | + |
| 6 | +interface NodeSettingsDialogProps { |
| 7 | + isOpen: boolean, |
| 8 | + nodeId: string | null, |
| 9 | + nodeData: ScaffoldNodeData | null, |
| 10 | + onClose: () => void, |
| 11 | + onSave: (nodeId: string, data: Partial<ScaffoldNodeData>) => void, |
| 12 | +} |
| 13 | + |
| 14 | +export function NodeSettingsDialog({ |
| 15 | + isOpen, |
| 16 | + nodeId, |
| 17 | + nodeData, |
| 18 | + onClose, |
| 19 | + onSave, |
| 20 | +}: NodeSettingsDialogProps) { |
| 21 | + const t = useScaffoldTranslation() |
| 22 | + const [organizationIds, setOrganizationIds] = useState<string[]>([]) |
| 23 | + const [newIdInput, setNewIdInput] = useState('') |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + if (isOpen && nodeData) { |
| 27 | + setOrganizationIds(nodeData.organization_ids ?? []) |
| 28 | + setNewIdInput('') |
| 29 | + } |
| 30 | + }, [isOpen, nodeData]) |
| 31 | + |
| 32 | + const handleAdd = () => { |
| 33 | + const trimmed = newIdInput.trim() |
| 34 | + if (trimmed && !organizationIds.includes(trimmed)) { |
| 35 | + setOrganizationIds((prev) => [...prev, trimmed]) |
| 36 | + setNewIdInput('') |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + const handleRemove = (index: number) => { |
| 41 | + setOrganizationIds((prev) => prev.filter((_, i) => i !== index)) |
| 42 | + } |
| 43 | + |
| 44 | + const handleSave = () => { |
| 45 | + if (nodeId && nodeData) { |
| 46 | + onSave(nodeId, { |
| 47 | + ...nodeData, |
| 48 | + organization_ids: organizationIds.length > 0 ? organizationIds : undefined, |
| 49 | + }) |
| 50 | + onClose() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const handleClose = () => { |
| 55 | + setNewIdInput('') |
| 56 | + onClose() |
| 57 | + } |
| 58 | + |
| 59 | + if (!nodeData || !nodeId) return null |
| 60 | + |
| 61 | + return ( |
| 62 | + <DialogRoot |
| 63 | + isOpen={isOpen} |
| 64 | + onIsOpenChange={(open) => { |
| 65 | + if (!open) handleClose() |
| 66 | + }} |
| 67 | + isModal |
| 68 | + > |
| 69 | + <Dialog |
| 70 | + titleElement={t('nodeSettingsTitle')} |
| 71 | + description={nodeData.name} |
| 72 | + onClose={handleClose} |
| 73 | + > |
| 74 | + <div className="flex flex-col gap-4"> |
| 75 | + <div className="flex flex-col gap-2"> |
| 76 | + <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 77 | + {t('organizationIds')} |
| 78 | + </span> |
| 79 | + <div className="flex gap-2"> |
| 80 | + <Input |
| 81 | + aria-label={t('addOrganizationIdPlaceholder')} |
| 82 | + value={newIdInput} |
| 83 | + onValueChange={setNewIdInput} |
| 84 | + onEditComplete={(v) => { |
| 85 | + if (v?.trim()) handleAdd() |
| 86 | + }} |
| 87 | + placeholder={t('addOrganizationIdPlaceholder')} |
| 88 | + /> |
| 89 | + <Button size="sm" onClick={handleAdd} disabled={!newIdInput.trim()}> |
| 90 | + {t('add')} |
| 91 | + </Button> |
| 92 | + </div> |
| 93 | + {organizationIds.length > 0 && ( |
| 94 | + <ul className="flex flex-col gap-1.5 max-h-[200px] overflow-auto rounded border border-gray-200 dark:border-gray-600 p-2"> |
| 95 | + {organizationIds.map((id, index) => ( |
| 96 | + <li |
| 97 | + key={`${id}-${index}`} |
| 98 | + className="flex items-center justify-between gap-2 rounded bg-gray-50 dark:bg-gray-800 px-2 py-1.5" |
| 99 | + > |
| 100 | + <span className="truncate text-sm font-mono">{id}</span> |
| 101 | + <Button |
| 102 | + size="sm" |
| 103 | + color="negative" |
| 104 | + coloringStyle="text" |
| 105 | + onClick={() => handleRemove(index)} |
| 106 | + > |
| 107 | + {t('remove')} |
| 108 | + </Button> |
| 109 | + </li> |
| 110 | + ))} |
| 111 | + </ul> |
| 112 | + )} |
| 113 | + </div> |
| 114 | + <div className="flex gap-2 justify-end"> |
| 115 | + <Button coloringStyle="text" onClick={handleClose}> |
| 116 | + {t('cancel')} |
| 117 | + </Button> |
| 118 | + <Button onClick={handleSave}> |
| 119 | + {t('save')} |
| 120 | + </Button> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </Dialog> |
| 124 | + </DialogRoot> |
| 125 | + ) |
| 126 | +} |
0 commit comments