|
1 | | -import React, { type ReactNode } from "react"; |
| 1 | +import React, { useEffect, useRef, useState, type ReactNode } from "react"; |
2 | 2 | import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels"; |
3 | 3 | import CodeEditor, { CodeEditorProps } from "../code_editor/main"; |
4 | 4 | import { MdxContent } from "#/mdx/presentation/mdx_content"; |
5 | 5 | import { MdxNoteBibliographyByContent } from "#/bibliography/presentation/note_bibliography/bib_by_mdx_content"; |
6 | 6 | import { MdxProviderGroup } from "#/mdx/presentation/mdx_provider_group"; |
7 | 7 |
|
8 | 8 | interface SplitViewScaffoldProps |
9 | | - extends Omit<CodeEditorProps, "themes" | "isModal" | "vimMode"> { |
10 | | - autoSaveId?: string; |
| 9 | + extends Omit<CodeEditorProps, "themes" | "isModal" | "vimMode"> { |
| 10 | + autoSaveId?: string; |
| 11 | + debounceSeconds: AppState["code"]["previewDebounce"]; |
11 | 12 | } |
12 | 13 |
|
13 | | -const SplitViewContainer = ({ |
14 | | - onChange, |
15 | | - value, |
16 | | - language, |
17 | | - autoSaveId = "splitview-panels", |
18 | | - ...props |
19 | | -}: SplitViewScaffoldProps): ReactNode => { |
20 | | - return ( |
21 | | - <PanelGroup autoSaveId={autoSaveId} direction="horizontal"> |
22 | | - <Panel id="editor-panel" order={1} defaultSize={50}> |
23 | | - <CodeEditor |
24 | | - {...props} |
25 | | - language={language} |
26 | | - value={value} |
27 | | - onChange={onChange} |
28 | | - /> |
29 | | - </Panel> |
30 | | - <PanelResizeHandle /> |
31 | | - <Panel |
32 | | - id="editor-output-panel" |
33 | | - className="bg-background" |
34 | | - order={2} |
35 | | - defaultSize={50} |
36 | | - > |
37 | | - <div |
38 | | - id="mdx-page-container" |
39 | | - className="w-full h-full overflow-y-auto overflow-x-hidden py-16 px-8" |
40 | | - > |
41 | | - <MdxProviderGroup> |
42 | | - <MdxContent |
43 | | - removeGrayMatter |
44 | | - className="p-6 max-h-full contents" |
45 | | - mdx={value} |
46 | | - /> |
47 | | - </MdxProviderGroup> |
48 | | - <MdxNoteBibliographyByContent mdx={value} /> |
49 | | - </div> |
50 | | - </Panel> |
51 | | - </PanelGroup> |
52 | | - ); |
53 | | -}; |
| 14 | +import { AppState } from "@/state/initial_state"; |
| 15 | +import { connect } from "react-redux"; |
| 16 | + |
| 17 | +const connector = connect((state: AppState) => ({ |
| 18 | + debounceSeconds: state.code.previewDebounce, |
| 19 | +})); |
| 20 | + |
| 21 | +const SplitViewContainer = connector( |
| 22 | + ({ |
| 23 | + onChange, |
| 24 | + value, |
| 25 | + language, |
| 26 | + autoSaveId = "splitview-panels", |
| 27 | + debounceSeconds, |
| 28 | + ...props |
| 29 | + }: SplitViewScaffoldProps): ReactNode => { |
| 30 | + const timer = useRef<NodeJS.Timeout | null>(null); |
| 31 | + const [debouncedValue, setDebouncedValue] = useState(value); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (timer.current) { |
| 35 | + clearTimeout(timer.current); |
| 36 | + } |
| 37 | + timer.current = setTimeout(() => { |
| 38 | + setDebouncedValue(value); |
| 39 | + }, debounceSeconds * 1000); |
| 40 | + }, [value]); |
| 41 | + |
| 42 | + return ( |
| 43 | + <PanelGroup autoSaveId={autoSaveId} direction="horizontal"> |
| 44 | + <Panel id="editor-panel" order={1} defaultSize={50}> |
| 45 | + <CodeEditor |
| 46 | + {...props} |
| 47 | + language={language} |
| 48 | + value={value} |
| 49 | + onChange={onChange} |
| 50 | + /> |
| 51 | + </Panel> |
| 52 | + <PanelResizeHandle /> |
| 53 | + <Panel |
| 54 | + id="editor-output-panel" |
| 55 | + className="bg-background" |
| 56 | + order={2} |
| 57 | + defaultSize={50} |
| 58 | + > |
| 59 | + <div |
| 60 | + id="mdx-page-container" |
| 61 | + className="w-full h-full overflow-y-auto overflow-x-hidden py-16 px-8" |
| 62 | + > |
| 63 | + <MdxProviderGroup> |
| 64 | + <MdxContent |
| 65 | + removeGrayMatter |
| 66 | + className="p-6 max-h-full contents" |
| 67 | + mdx={debouncedValue} |
| 68 | + /> |
| 69 | + </MdxProviderGroup> |
| 70 | + <MdxNoteBibliographyByContent mdx={debouncedValue} /> |
| 71 | + </div> |
| 72 | + </Panel> |
| 73 | + </PanelGroup> |
| 74 | + ); |
| 75 | + } |
| 76 | +); |
54 | 77 |
|
55 | 78 | SplitViewContainer.displayName = "SplitViewScaffold"; |
56 | 79 |
|
|
0 commit comments