({
content,
defaultFilename,
onDone
}: ExportDialogProps)
| 23 | }; |
| 24 | type ExportOption = 'clipboard' | 'file'; |
| 25 | export function ExportDialog({ |
| 26 | content, |
| 27 | defaultFilename, |
| 28 | onDone |
| 29 | }: ExportDialogProps): React.ReactNode { |
| 30 | const [, setSelectedOption] = useState<ExportOption | null>(null); |
| 31 | const [filename, setFilename] = useState<string>(defaultFilename); |
| 32 | const [cursorOffset, setCursorOffset] = useState<number>(defaultFilename.length); |
| 33 | const [showFilenameInput, setShowFilenameInput] = useState(false); |
| 34 | const { |
| 35 | columns |
| 36 | } = useTerminalSize(); |
| 37 | |
| 38 | // Handle going back from filename input to option selection |
| 39 | const handleGoBack = useCallback(() => { |
| 40 | setShowFilenameInput(false); |
| 41 | setSelectedOption(null); |
| 42 | }, []); |
| 43 | const handleSelectOption = async (value: string): Promise<void> => { |
| 44 | if (value === 'clipboard') { |
| 45 | // Copy to clipboard immediately |
| 46 | const raw = await setClipboard(content); |
| 47 | if (raw) process.stdout.write(raw); |
| 48 | onDone({ |
| 49 | success: true, |
| 50 | message: 'Conversation copied to clipboard' |
| 51 | }); |
| 52 | } else if (value === 'file') { |
| 53 | setSelectedOption('file'); |
| 54 | setShowFilenameInput(true); |
| 55 | } |
| 56 | }; |
| 57 | const handleFilenameSubmit = () => { |
| 58 | const finalFilename = filename.endsWith('.txt') ? filename : filename.replace(/\.[^.]+$/, '') + '.txt'; |
| 59 | const filepath = join(getCwd(), finalFilename); |
| 60 | try { |
| 61 | writeFileSync_DEPRECATED(filepath, content, { |
| 62 | encoding: 'utf-8', |
| 63 | flush: true |
| 64 | }); |
| 65 | onDone({ |
| 66 | success: true, |
| 67 | message: `Conversation exported to: ${filepath}` |
| 68 | }); |
| 69 | } catch (error) { |
| 70 | onDone({ |
| 71 | success: false, |
| 72 | message: `Failed to export conversation: ${error instanceof Error ? error.message : 'Unknown error'}` |
| 73 | }); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | // Dialog calls onCancel when Escape is pressed. If we are in the filename |
| 78 | // input sub-screen, go back to the option list instead of closing entirely. |
| 79 | const handleCancel = useCallback(() => { |
| 80 | if (showFilenameInput) { |
| 81 | handleGoBack(); |
| 82 | } else { |
nothing calls this directly
no test coverage detected