|
| 1 | +import { useEffect, useState, type FC } from "react"; |
| 2 | + |
| 3 | +import { |
| 4 | + Dialog, |
| 5 | + DialogFooter, |
| 6 | + DialogActionButton, |
| 7 | + DialogHeader, |
| 8 | + DialogContent, |
| 9 | + type DialogPropsBase, |
| 10 | + DialogError, |
| 11 | +} from "../../dialog"; |
| 12 | +import { BookmarkExportSelection, filterExports, makeExportable, walkAndSetAllChildrenToSelection, walkAndUpdateBookmarkExportSelection, type BookmarkItem, type ExportableBookmarkItem } from "../../../types/bookmarks"; |
| 13 | +import { BookmarkExportList } from "../export/BookmarkExportList"; |
| 14 | + |
| 15 | +export type ExportBookmarksDialogProps = { |
| 16 | + onClose: () => void; |
| 17 | + bookmarks: BookmarkItem[]; |
| 18 | +} & DialogPropsBase; |
| 19 | + |
| 20 | +export const ExportBookmarksDialog: FC<ExportBookmarksDialogProps> = ({ |
| 21 | + open, |
| 22 | + onClose, |
| 23 | + bookmarks, |
| 24 | +}) => { |
| 25 | + const [exportList, setExportList] = useState<ExportableBookmarkItem[]>([]); |
| 26 | + const [error, setError] = useState<string | null>(null); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + setExportList(bookmarks.map(makeExportable)); |
| 30 | + }, [bookmarks]); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const timeoutId = setTimeout(() => { |
| 34 | + if (error) { |
| 35 | + setError(null); |
| 36 | + } |
| 37 | + }, 3000); |
| 38 | + return () => clearTimeout(timeoutId); |
| 39 | + }, [error]); |
| 40 | + |
| 41 | + const close = () => { |
| 42 | + setExportList(prev => walkAndSetAllChildrenToSelection(prev, BookmarkExportSelection.DONT_EXPORT)); |
| 43 | + onClose(); |
| 44 | + }; |
| 45 | + |
| 46 | + const doExport = () => { |
| 47 | + const exported = filterExports(exportList); |
| 48 | + |
| 49 | + if (exported.length === 0) { |
| 50 | + setError("No bookmarks selected for export."); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const blob = new Blob([JSON.stringify(exported)], { type: "application/json" }); |
| 55 | + const url = URL.createObjectURL(blob); |
| 56 | + const link = document.createElement('a'); |
| 57 | + link.href = url; |
| 58 | + link.download = "bookmarks.json"; // Forces download instead of navigation |
| 59 | + document.body.appendChild(link); |
| 60 | + link.click(); |
| 61 | + document.body.removeChild(link); |
| 62 | + URL.revokeObjectURL(url); // Clean up the URL |
| 63 | + |
| 64 | + close(); |
| 65 | + }; |
| 66 | + |
| 67 | + const handleBookmarkExportSelectionChange = (id: string, state: BookmarkExportSelection) => { |
| 68 | + setExportList(prev => walkAndUpdateBookmarkExportSelection(prev, id, state)); |
| 69 | + }; |
| 70 | + |
| 71 | + const selectAll = () => { |
| 72 | + setExportList(prev => walkAndSetAllChildrenToSelection(prev, BookmarkExportSelection.EXPORT)); |
| 73 | + }; |
| 74 | + |
| 75 | + const deselectAll = () => { |
| 76 | + setExportList(prev => walkAndSetAllChildrenToSelection(prev, BookmarkExportSelection.DONT_EXPORT)); |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <Dialog open={open} width="70%"> |
| 81 | + <DialogHeader title="Export Bookmarks" onClose={close} /> |
| 82 | + {error && ( |
| 83 | + <DialogError heading="Error"> |
| 84 | + <p>{error}</p> |
| 85 | + </DialogError> |
| 86 | + )} |
| 87 | + <DialogContent> |
| 88 | + <div className="buttonBar full" style={{ marginBottom: ".5rem" }}> |
| 89 | + <button className="button small outline-green" onClick={selectAll}>Select All</button> |
| 90 | + <button className="button small outline-red" onClick={deselectAll}>Deselect All</button> |
| 91 | + </div> |
| 92 | + <BookmarkExportList |
| 93 | + bookmarks={exportList} |
| 94 | + changeBookmarkExportSelection={handleBookmarkExportSelectionChange} |
| 95 | + /> |
| 96 | + </DialogContent> |
| 97 | + <DialogFooter> |
| 98 | + <DialogActionButton type="confirm" onClick={doExport}>Export</DialogActionButton> |
| 99 | + <DialogActionButton type="cancel" onClick={close}>Cancel</DialogActionButton> |
| 100 | + </DialogFooter> |
| 101 | + </Dialog> |
| 102 | + ); |
| 103 | +}; |
0 commit comments