|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { createContext, useCallback, useContext, useRef, useTransition, type ReactNode } from "react"; |
| 4 | +import { createUsePuck } from "@puckeditor/core"; |
| 5 | +import { toast } from "sonner"; |
| 6 | +import { getEditorUrl, getPreviewUrl } from "../../../../lib/editor-url"; |
| 7 | +import { saveVersionAction } from "../../../../lib/documents/actions"; |
| 8 | +import { runAction } from "../../runAction"; |
| 9 | +import { useKeybind } from "@/hooks/useKeybind"; |
| 10 | +import { useDialogs } from "@/components/ui/dialog-provider"; |
| 11 | +import { useDocumentContext } from "./document-context"; |
| 12 | + |
| 13 | +const usePuck = createUsePuck(); |
| 14 | + |
| 15 | +const SAVE_KEYBINDS = [ |
| 16 | + { key: "s", ctrlKey: true }, |
| 17 | + { key: "s", metaKey: true }, |
| 18 | +] as const; |
| 19 | + |
| 20 | +type EditorSaveContextType = { |
| 21 | + canSave: boolean; |
| 22 | + isSaving: boolean; |
| 23 | + save: () => void; |
| 24 | +}; |
| 25 | + |
| 26 | +const EditorSaveContext = createContext<EditorSaveContextType>({ |
| 27 | + canSave: false, |
| 28 | + isSaving: false, |
| 29 | + save: () => {}, |
| 30 | +}); |
| 31 | + |
| 32 | +function useSaveHotkey(save: () => void, target?: Document | null, enabled = true) { |
| 33 | + useKeybind( |
| 34 | + SAVE_KEYBINDS, |
| 35 | + (event) => { |
| 36 | + event.preventDefault(); |
| 37 | + save(); |
| 38 | + }, |
| 39 | + [target], |
| 40 | + { capture: true, enabled }, |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +export function EditorSaveProvider({ children }: { children: ReactNode }) { |
| 45 | + const data = usePuck((s) => s.appState.data); |
| 46 | + const { documentId, documentName, isArchived, isDirty, addVersion } = useDocumentContext(); |
| 47 | + const { alert } = useDialogs(); |
| 48 | + const [isSaving, startTransition] = useTransition(); |
| 49 | + const canSave = !isArchived && !isSaving && isDirty; |
| 50 | + const saveStateRef = useRef({ |
| 51 | + canSave, |
| 52 | + isArchived, |
| 53 | + isSaving, |
| 54 | + isDirty, |
| 55 | + data, |
| 56 | + documentId, |
| 57 | + documentName, |
| 58 | + }); |
| 59 | + |
| 60 | + saveStateRef.current = { |
| 61 | + canSave, |
| 62 | + isArchived, |
| 63 | + isSaving, |
| 64 | + isDirty, |
| 65 | + data, |
| 66 | + documentId, |
| 67 | + documentName, |
| 68 | + }; |
| 69 | + |
| 70 | + const save = useCallback(() => { |
| 71 | + const { canSave, isArchived, isSaving, isDirty, data, documentId, documentName } = saveStateRef.current; |
| 72 | + |
| 73 | + if (!canSave) { |
| 74 | + if (!isArchived && !isSaving && !isDirty) { |
| 75 | + toast.info("No changes to save", { id: "editor-no-changes-to-save" }); |
| 76 | + } |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + startTransition(async () => { |
| 82 | + const result = await runAction(saveVersionAction({ documentId, content: data })); |
| 83 | + |
| 84 | + if (result.success === false) { |
| 85 | + await alert(result.error); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + addVersion(result.data.version); |
| 90 | + window.history.replaceState(window.history.state, "", getEditorUrl(documentId, documentName, result.data.version.id)); |
| 91 | + |
| 92 | + const previewUrl = getPreviewUrl(documentId, documentName, result.data.version.id); |
| 93 | + toast.success("Saved", { |
| 94 | + action: { |
| 95 | + label: "Preview", |
| 96 | + onClick: () => { |
| 97 | + window.open(previewUrl, "_blank"); |
| 98 | + }, |
| 99 | + }, |
| 100 | + }); |
| 101 | + }); |
| 102 | + }, [addVersion, alert, startTransition]); |
| 103 | + |
| 104 | + useSaveHotkey(save); |
| 105 | + |
| 106 | + return ( |
| 107 | + <EditorSaveContext.Provider value={{ canSave, isSaving, save }}> |
| 108 | + {children} |
| 109 | + </EditorSaveContext.Provider> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +export function useEditorSaveContext() { |
| 114 | + return useContext(EditorSaveContext); |
| 115 | +} |
| 116 | + |
| 117 | +export function useEditorSaveHotkey(target?: Document | null, enabled = true) { |
| 118 | + const { save } = useEditorSaveContext(); |
| 119 | + |
| 120 | + useSaveHotkey(save, target, enabled); |
| 121 | +} |
0 commit comments