import React, { useMemo, useState } from 'react'; import { useI18n } from '@/application/i18n/I18nProvider'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; const ROOT_PACKAGE_VALUE = '__root__'; function toSelectValue(packagePath: string): string { return packagePath || ROOT_PACKAGE_VALUE; } function fromSelectValue(value: string): string { return value === ROOT_PACKAGE_VALUE ? '' : value; } export interface ScriptSaveRecordingDialogProps { open: boolean; code: string; packages: string[]; defaultName?: string; onClose: () => void; onSave: (payload: { name: string; packagePath: string; code: string; editAfterSave: boolean }) => void; } export const ScriptSaveRecordingDialog: React.FC = ({ open, code, packages, defaultName, onClose, onSave, }) => { const { t } = useI18n(); const [name, setName] = useState(defaultName || ''); const [packageSelectValue, setPackageSelectValue] = useState(ROOT_PACKAGE_VALUE); const packageOptions = useMemo(() => { const unique = Array.from(new Set(packages.filter(Boolean))); return [ROOT_PACKAGE_VALUE, ...unique]; }, [packages]); React.useEffect(() => { if (!open) return; setName(defaultName || ''); setPackageSelectValue(toSelectValue(packages[0] || '')); }, [defaultName, open, packages]); const handleSave = (editAfterSave: boolean) => { onSave({ name, packagePath: fromSelectValue(packageSelectValue), code, editAfterSave, }); }; return ( { if (!next) onClose(); }}> {t('scripts.recording.saveTitle')}
setName(event.target.value)} placeholder={t('scripts.recording.namePlaceholder')} />
{code}
); };