|
| 1 | +import { Trans, useTranslation } from "react-i18next"; |
| 2 | +import { useParams } from "react-router-dom"; |
| 3 | +import { |
| 4 | + useProjectMetadataQuery, |
| 5 | + useUpdateAboutPageContentsMutation, |
| 6 | +} from "../generated/graphql"; |
| 7 | +import { ProseMirror, useProseMirror } from "use-prosemirror"; |
| 8 | +import { aboutPage as editorConfig } from "../editor/config"; |
| 9 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 10 | +import { EditorView } from "prosemirror-view"; |
| 11 | +import { Node, Slice } from "prosemirror-model"; |
| 12 | +import { EditorState, Transaction } from "prosemirror-state"; |
| 13 | +import EditorMenuBar from "../editor/EditorMenuBar"; |
| 14 | +import languages from "../lang/supported"; |
| 15 | +import { useDebouncedFn } from "beautiful-react-hooks"; |
| 16 | +import useDebounce from "../useDebounce"; |
| 17 | + |
| 18 | +const { schema, plugins } = editorConfig; |
| 19 | + |
| 20 | +export default function AboutPageSettings() { |
| 21 | + const { t, i18n } = useTranslation("admin"); |
| 22 | + const { slug } = useParams<{ slug: string }>(); |
| 23 | + const { data, loading } = useProjectMetadataQuery({ |
| 24 | + variables: { slug }, |
| 25 | + }); |
| 26 | + const [mutate, mutationState] = useUpdateAboutPageContentsMutation(); |
| 27 | + const [state, setState] = useProseMirror({ schema }); |
| 28 | + const [changes, setChanges] = useState(false); |
| 29 | + const viewRef = useRef<{ view: EditorView }>(); |
| 30 | + const [lang, setLang] = useState("EN"); |
| 31 | + |
| 32 | + const debouncedDoc = useDebounce(state.doc, 500); |
| 33 | + console.log( |
| 34 | + loading, |
| 35 | + JSON.stringify( |
| 36 | + data?.project?.aboutPageContents?.[lang]?.content?.[0]?.content |
| 37 | + ) |
| 38 | + ); |
| 39 | + |
| 40 | + // TODO: Problem here with cached projectmetadata setting the contents of the |
| 41 | + // editor, and then being updated by the network. stale content shows up after |
| 42 | + // refresh. |
| 43 | + useEffect(() => { |
| 44 | + const doc = data?.project?.aboutPageContents?.[lang]; |
| 45 | + return; |
| 46 | + if (state && doc && state.doc) { |
| 47 | + try { |
| 48 | + console.log("dispatching", state.doc); |
| 49 | + const node = Node.fromJSON(schema, doc); |
| 50 | + // const t = state.tr; |
| 51 | + // state.tr.replace( |
| 52 | + // 0, |
| 53 | + // state.doc.content.size, |
| 54 | + // new Slice(node.content, 0, 0) |
| 55 | + // ); |
| 56 | + |
| 57 | + // t.doc = node; |
| 58 | + // console.log("t", t, node); |
| 59 | + // t.replace(0, state.doc.nodeSize, new Slice(node.content, 0, 0)); |
| 60 | + // // t.replaceRangeWith(0, state.doc.nodeSize, node); |
| 61 | + // console.log(t); |
| 62 | + // viewRef.current?.view.dispatch(t); |
| 63 | + const newState = EditorState.create({ |
| 64 | + ...state, |
| 65 | + schema: state.schema, |
| 66 | + plugins: state.plugins, |
| 67 | + doc: node, |
| 68 | + selection: state.selection, |
| 69 | + }); |
| 70 | + onChange(newState); |
| 71 | + } catch (e) { |
| 72 | + // do nothing |
| 73 | + // console.error(e); |
| 74 | + throw e; |
| 75 | + } |
| 76 | + } |
| 77 | + }, [data?.project?.aboutPageContents, lang]); |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + if (!loading) { |
| 81 | + const startingDocument = data?.project?.aboutPageContents?.[lang]; |
| 82 | + try { |
| 83 | + const doc = startingDocument |
| 84 | + ? Node.fromJSON(schema, startingDocument) |
| 85 | + : undefined; |
| 86 | + // initial render |
| 87 | + const state = EditorState.create({ |
| 88 | + schema: schema, |
| 89 | + plugins, |
| 90 | + doc, |
| 91 | + }); |
| 92 | + setState(state); |
| 93 | + } catch (e) { |
| 94 | + const doc = startingDocument |
| 95 | + ? Node.fromJSON(schema, { |
| 96 | + type: "doc", |
| 97 | + content: [ |
| 98 | + { |
| 99 | + type: "paragraph", |
| 100 | + content: [{ type: "text", text: "Problem parsing metadata" }], |
| 101 | + }, |
| 102 | + { |
| 103 | + type: "paragraph", |
| 104 | + content: [ |
| 105 | + { |
| 106 | + type: "text", |
| 107 | + text: e.toString(), |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + ], |
| 112 | + }) |
| 113 | + : undefined; |
| 114 | + // initial render |
| 115 | + const state = EditorState.create({ |
| 116 | + schema: schema, |
| 117 | + plugins, |
| 118 | + doc, |
| 119 | + }); |
| 120 | + setState(state); |
| 121 | + } |
| 122 | + } |
| 123 | + }, [loading, setState, lang]); |
| 124 | + |
| 125 | + const [hasChanges, setHasChanges] = useState(false); |
| 126 | + |
| 127 | + const onChange = useCallback( |
| 128 | + (state: EditorState) => { |
| 129 | + setState(state); |
| 130 | + setHasChanges(true); |
| 131 | + }, |
| 132 | + [setState] |
| 133 | + ); |
| 134 | + |
| 135 | + useEffect(() => { |
| 136 | + if (debouncedDoc && hasChanges) { |
| 137 | + mutate({ |
| 138 | + variables: { |
| 139 | + content: debouncedDoc?.toJSON(), |
| 140 | + lang, |
| 141 | + slug, |
| 142 | + }, |
| 143 | + }); |
| 144 | + setHasChanges(false); |
| 145 | + } |
| 146 | + }, [debouncedDoc]); |
| 147 | + |
| 148 | + return ( |
| 149 | + <> |
| 150 | + <div className="relative"> |
| 151 | + <div className="shadow sm:rounded-md sm:overflow-hidden"> |
| 152 | + <div className="px-4 py-5 bg-white sm:p-6"> |
| 153 | + <h3 className="text-lg font-medium leading-6 text-gray-900"> |
| 154 | + {t("About Page")} |
| 155 | + </h3> |
| 156 | + {/* {mutationState.error && <p>{mutationState.error.message}</p>} */} |
| 157 | + <p className="mt-1 text-sm leading-5 text-gray-500"> |
| 158 | + <Trans ns="admin"> |
| 159 | + When enabled, the about page will be accessible from the project |
| 160 | + sidebar. It will also be presented to users when they first |
| 161 | + visit the project. If your project supports multiple languages, |
| 162 | + use the dropdown to switch between languages and provide |
| 163 | + translated content. |
| 164 | + </Trans> |
| 165 | + </p> |
| 166 | + <div className="py-4"> |
| 167 | + <div className="bg-gray-50 rounded shadow"> |
| 168 | + <EditorMenuBar |
| 169 | + view={viewRef.current?.view} |
| 170 | + state={state} |
| 171 | + schema={schema} |
| 172 | + > |
| 173 | + <div className="border-r-1 w-1 border-gray-300 border-r h-5 mx-2"></div> |
| 174 | + <select className="text-sm ml-2 px-3 py-0.5 rounded border-gray-300"> |
| 175 | + <option value="EN">{t("English")}</option> |
| 176 | + {data?.project?.supportedLanguages?.map((l) => { |
| 177 | + const lang = languages.find((lang) => lang.code === l); |
| 178 | + if (!lang) { |
| 179 | + return null; |
| 180 | + } |
| 181 | + return ( |
| 182 | + <option key={l} value={l!}> |
| 183 | + {lang.localName || lang.name} |
| 184 | + </option> |
| 185 | + ); |
| 186 | + })} |
| 187 | + </select> |
| 188 | + </EditorMenuBar> |
| 189 | + </div> |
| 190 | + |
| 191 | + <div className="pt-4 flex-1 overflow-y-auto"> |
| 192 | + <ProseMirror |
| 193 | + className="metadata small-variant" |
| 194 | + state={state} |
| 195 | + onChange={onChange} |
| 196 | + // @ts-ignore |
| 197 | + ref={viewRef} |
| 198 | + /> |
| 199 | + </div> |
| 200 | + </div> |
| 201 | + </div> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + </> |
| 205 | + ); |
| 206 | +} |
0 commit comments