|
1 | 1 | import usePageStore from "@/store/usePageStore"; |
2 | 2 | import Editor from "./editor"; |
3 | | -import { usePage } from "@/hooks/usePages"; |
| 3 | +import { |
| 4 | + usePage, |
| 5 | + useUpdatePage, |
| 6 | + useOptimisticUpdatePage, |
| 7 | +} from "@/hooks/usePages"; |
| 8 | +import EditorLayout from "./layout/EditorLayout"; |
| 9 | +import EditorTitle from "./editor/EditorTitle"; |
| 10 | +import { EditorInstance } from "novel"; |
| 11 | +import { useEffect, useRef, useState } from "react"; |
| 12 | +import { useDebouncedCallback } from "use-debounce"; |
| 13 | +import { WebsocketProvider } from "y-websocket"; |
| 14 | +import * as Y from "yjs"; |
| 15 | +import SaveStatus from "./editor/ui/SaveStatus"; |
4 | 16 |
|
5 | 17 | export default function EditorView() { |
6 | 18 | const { currentPage } = usePageStore(); |
7 | | - const { page } = usePage(currentPage); |
| 19 | + const { page, isLoading } = usePage(currentPage); |
| 20 | + const [editorKey, setEditorKey] = useState(0); |
| 21 | + const [saveStatus, setSaveStatus] = useState<"saved" | "unsaved">("saved"); |
8 | 22 |
|
9 | | - if (!page) { |
10 | | - return <div></div>; |
| 23 | + const ydoc = useRef<Y.Doc>(); |
| 24 | + const provider = useRef<WebsocketProvider>(); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (currentPage === null) return; |
| 28 | + |
| 29 | + if (provider.current) { |
| 30 | + provider.current.disconnect(); |
| 31 | + provider.current.destroy(); |
| 32 | + } |
| 33 | + if (ydoc.current) { |
| 34 | + ydoc.current.destroy(); |
| 35 | + } |
| 36 | + |
| 37 | + const doc = new Y.Doc(); |
| 38 | + const wsProvider = new WebsocketProvider( |
| 39 | + "ws://localhost:1234", |
| 40 | + `document-${currentPage}`, |
| 41 | + doc, |
| 42 | + ); |
| 43 | + |
| 44 | + ydoc.current = doc; |
| 45 | + provider.current = wsProvider; |
| 46 | + |
| 47 | + setEditorKey((prev) => prev + 1); |
| 48 | + |
| 49 | + return () => { |
| 50 | + wsProvider.disconnect(); |
| 51 | + wsProvider.destroy(); |
| 52 | + doc.destroy(); |
| 53 | + }; |
| 54 | + }, [currentPage]); |
| 55 | + |
| 56 | + const pageTitle = page?.title ?? "제목없음"; |
| 57 | + const pageContent = page?.content ?? {}; |
| 58 | + |
| 59 | + const updatePageMutation = useUpdatePage(); |
| 60 | + const optimisticUpdatePageMutation = useOptimisticUpdatePage({ |
| 61 | + id: currentPage ?? 0, |
| 62 | + }); |
| 63 | + |
| 64 | + const handleTitleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 65 | + if (currentPage === null) return; |
| 66 | + |
| 67 | + setSaveStatus("unsaved"); |
| 68 | + |
| 69 | + optimisticUpdatePageMutation.mutate( |
| 70 | + { |
| 71 | + pageData: { title: e.target.value, content: pageContent }, |
| 72 | + }, |
| 73 | + { |
| 74 | + onSuccess: () => setSaveStatus("saved"), |
| 75 | + onError: () => setSaveStatus("unsaved"), |
| 76 | + }, |
| 77 | + ); |
| 78 | + }; |
| 79 | + |
| 80 | + const handleEditorUpdate = useDebouncedCallback( |
| 81 | + async ({ editor }: { editor: EditorInstance }) => { |
| 82 | + if (currentPage === null) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const json = editor.getJSON(); |
| 87 | + |
| 88 | + setSaveStatus("unsaved"); |
| 89 | + updatePageMutation.mutate( |
| 90 | + { id: currentPage, pageData: { title: pageTitle, content: json } }, |
| 91 | + { |
| 92 | + onSuccess: () => setSaveStatus("saved"), |
| 93 | + onError: () => setSaveStatus("unsaved"), |
| 94 | + }, |
| 95 | + ); |
| 96 | + }, |
| 97 | + 500, |
| 98 | + ); |
| 99 | + |
| 100 | + if (isLoading || !page || currentPage === null) { |
| 101 | + return ( |
| 102 | + <div> |
| 103 | + {isLoading && <div>"isLoading"</div>} |
| 104 | + {!page && <div>"!page"</div>} |
| 105 | + {currentPage === null && <div>"currrentPage === null"</div>} |
| 106 | + </div> |
| 107 | + ); |
11 | 108 | } |
12 | 109 |
|
| 110 | + if (!ydoc.current || !provider.current) return <div>로딩중</div>; |
| 111 | + |
13 | 112 | return ( |
14 | | - <Editor key={page.id} initialContent={page.content} pageId={page.id} /> |
| 113 | + <EditorLayout> |
| 114 | + <SaveStatus saveStatus={saveStatus} /> |
| 115 | + <EditorTitle title={pageTitle} onTitleChange={handleTitleChange} /> |
| 116 | + <Editor |
| 117 | + key={editorKey} |
| 118 | + initialContent={pageContent} |
| 119 | + pageId={currentPage} |
| 120 | + ydoc={ydoc.current} |
| 121 | + provider={provider.current} |
| 122 | + onEditorUpdate={handleEditorUpdate} |
| 123 | + /> |
| 124 | + </EditorLayout> |
15 | 125 | ); |
16 | 126 | } |
0 commit comments