'use client'; import { useLayoutEffect, useMemo, useState } from 'react'; import { IconReset, IconSave } from '@/components/Icons'; import MiniButton from '@/components/ui/MiniButton'; import Modal from '@/components/ui/Modal'; import TextArea from '@/components/ui/TextArea'; import TextInput from '@/components/ui/TextInput'; import { useRSForm } from '@/context/RSFormContext'; import { IVersionData, IVersionInfo, VersionID } from '@/models/library'; import TableVersions from './TableVersions'; interface DlgEditVersionsProps { hideWindow: () => void; versions: IVersionInfo[]; onDelete: (versionID: VersionID) => void; onUpdate: (versionID: VersionID, data: IVersionData) => void; } function DlgEditVersions({ hideWindow, versions, onDelete, onUpdate }: DlgEditVersionsProps) { const { processing } = useRSForm(); const [selected, setSelected] = useState(undefined); const [version, setVersion] = useState(''); const [description, setDescription] = useState(''); const isValid = useMemo(() => { if (!selected) { return false; } return versions.every(ver => ver.id === selected.id || ver.version != version); }, [selected, version, versions]); const isModified = useMemo(() => { if (!selected) { return false; } return selected.version != version || selected.description != description; }, [version, description, selected]); function handleUpdate() { if (!isModified || !selected || processing || !isValid) { return; } const data: IVersionData = { version: version, description: description }; onUpdate(selected.id, data); } function handleReset() { if (!selected) { return false; } setVersion(selected?.version ?? ''); setDescription(selected?.description ?? ''); } useLayoutEffect(() => { setVersion(selected?.version ?? ''); setDescription(selected?.description ?? ''); }, [selected]); const versionsTable = useMemo( () => ( setSelected(versions.find(ver => ver.id === versionID))} selected={selected?.id} /> ), [processing, versions, onDelete, selected?.id] ); return ( {versionsTable}
setVersion(event.target.value)} />
} onClick={handleUpdate} /> } />