| 1 | import { useEffect, useState } from 'react' |
| 2 | import { Button } from '../../ui/Button' |
| 3 | import { |
| 4 | Dialog, |
| 5 | DialogContent, |
| 6 | DialogDescription, |
| 7 | DialogFooter, |
| 8 | DialogHeader, |
| 9 | DialogTitle |
| 10 | } from '../../ui/Dialog' |
| 11 | import { Input } from '../../ui/Input' |
| 12 | import { useT } from '@renderer/i18n' |
| 13 | |
| 14 | export function SaveAsNewSessionDialog({ |
| 15 | open, |
| 16 | defaultName, |
| 17 | saving, |
| 18 | onOpenChange, |
| 19 | onSubmit |
| 20 | }: { |
| 21 | open: boolean |
| 22 | defaultName: string |
| 23 | saving?: boolean |
| 24 | onOpenChange: (open: boolean) => void |
| 25 | onSubmit: (payload: { title: string }) => void |
| 26 | }): React.JSX.Element { |
| 27 | const t = useT() |
| 28 | const [title, setTitle] = useState(defaultName) |
| 29 | |
| 30 | useEffect(() => { |
| 31 | if (!open) return |
| 32 | setTitle(defaultName) |
| 33 | }, [defaultName, open]) |
| 34 | |
| 35 | const submit = (): void => { |
| 36 | const cleanTitle = title.trim() |
| 37 | if (!cleanTitle || saving) return |
| 38 | onSubmit({ title: cleanTitle }) |
| 39 | } |
| 40 | |
| 41 | return ( |
| 42 | <Dialog open={open} onOpenChange={(next) => !saving && onOpenChange(next)}> |
| 43 | <DialogContent showClose={!saving}> |
| 44 | <DialogHeader> |
| 45 | <DialogTitle>{t('sessionDetail.saveAsNewSessionDialogTitle')}</DialogTitle> |
| 46 | <DialogDescription className="text-xs leading-5"> |
| 47 | {t('sessionDetail.saveAsNewSessionDialogDescription')} |
| 48 | </DialogDescription> |
| 49 | </DialogHeader> |
| 50 | <form |
| 51 | className="space-y-4" |
| 52 | onSubmit={(event) => { |
| 53 | event.preventDefault() |
| 54 | submit() |
| 55 | }} |
| 56 | > |
| 57 | <div> |
| 58 | <label className="mb-1 block text-xs font-medium text-[#5f6b50]"> |
| 59 | {t('sessionDetail.saveAsNewSessionNameLabel')} |
| 60 | </label> |
| 61 | <Input |
| 62 | autoFocus |
| 63 | value={title} |
| 64 | maxLength={120} |
| 65 | placeholder={t('sessionDetail.saveAsNewSessionNamePlaceholder')} |
| 66 | disabled={saving} |
| 67 | onChange={(event) => setTitle(event.target.value)} |
| 68 | /> |
| 69 | </div> |
| 70 | <DialogFooter> |
| 71 | <Button |
| 72 | type="button" |
| 73 | variant="outline" |
| 74 | size="sm" |
| 75 | onClick={() => onOpenChange(false)} |
| 76 | disabled={saving} |
| 77 | > |
| 78 | {t('common.cancel')} |
| 79 | </Button> |
| 80 | <Button type="submit" size="sm" disabled={saving || !title.trim()}> |
| 81 | {saving |
| 82 | ? t('sessionDetail.saveAsNewSessionSaving') |
| 83 | : t('sessionDetail.saveAsNewSessionConfirm')} |
| 84 | </Button> |
| 85 | </DialogFooter> |
| 86 | </form> |
| 87 | </DialogContent> |
| 88 | </Dialog> |
| 89 | ) |
| 90 | } |
| 91 |