|
| 1 | +import Select from "@atlaskit/select"; |
| 2 | +import { observer } from "mobx-react-lite"; |
| 3 | +import { useEffect, useRef, useState } from "react"; |
| 4 | +import { Inspector } from "react-inspector"; |
| 5 | +import { Rnd } from "react-rnd"; |
| 6 | +import { DocConnection } from "../../store/DocConnection"; |
| 7 | +import { SessionStore } from "../../store/local/SessionStore"; |
| 8 | +import { DocumentInfo } from "../../store/yjs-sync/DocumentCoordinator"; |
| 9 | +import { TypeCellRemote } from "../../store/yjs-sync/remote/TypeCellRemote"; |
| 10 | +import styles from "./DevTools.module.css"; |
| 11 | + |
| 12 | +export const DevTools = observer((props: { sessionStore: SessionStore }) => { |
| 13 | + const [offline, setOffline] = useState(false); |
| 14 | + const [flaky, setFlaky] = useState(false); |
| 15 | + const flakyInterval = useRef<ReturnType<typeof setInterval> | undefined>( |
| 16 | + undefined, |
| 17 | + ); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + TypeCellRemote.Offline = offline; |
| 21 | + }, [offline]); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + clearInterval(flakyInterval.current); |
| 25 | + if (flaky) { |
| 26 | + flakyInterval.current = setInterval(() => { |
| 27 | + setOffline((o) => !o); |
| 28 | + }, 5000); |
| 29 | + } |
| 30 | + }, [flaky]); |
| 31 | + |
| 32 | + const [selectedResource, setSelectedResource] = useState< |
| 33 | + string | undefined |
| 34 | + >(); |
| 35 | + |
| 36 | + const [onlyLoaded, setOnlyLoaded] = useState(true); |
| 37 | + |
| 38 | + if (!props.sessionStore.documentCoordinator) { |
| 39 | + return <div>Loading</div>; |
| 40 | + } |
| 41 | + |
| 42 | + let documentCoordinatorEntries = [ |
| 43 | + ...(props.sessionStore.documentCoordinator.documents.entries() as IterableIterator< |
| 44 | + [string, DocumentInfo] |
| 45 | + >), |
| 46 | + ].map(([key, value]) => { |
| 47 | + const resource = DocConnection.get(key, props.sessionStore); |
| 48 | + return { |
| 49 | + // id: key, |
| 50 | + ...value, |
| 51 | + loaded: props.sessionStore.documentCoordinator?.loadedDocuments.has(key), |
| 52 | + created_at: new Date(value.created_at), |
| 53 | + needs_save_since: value.needs_save_since |
| 54 | + ? new Date(value.needs_save_since) |
| 55 | + : undefined, |
| 56 | + type: resource?.tryDoc?.type, |
| 57 | + title: |
| 58 | + resource?.tryDoc?.type === "!richtext" |
| 59 | + ? resource?.tryDoc.doc.title |
| 60 | + : resource?.tryDoc?.title, |
| 61 | + }; |
| 62 | + }); |
| 63 | + |
| 64 | + if (onlyLoaded) { |
| 65 | + documentCoordinatorEntries = documentCoordinatorEntries.filter( |
| 66 | + (d) => d.loaded, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + const selectOptions = documentCoordinatorEntries |
| 71 | + .filter((d) => d.loaded) |
| 72 | + .map((d) => ({ |
| 73 | + label: d.title + " (" + d.id + ")", |
| 74 | + value: d.id, |
| 75 | + })); |
| 76 | + // const resourceEntries = [...cache.entries()].map(([key, value]) => { |
| 77 | + // return { |
| 78 | + // id: key, |
| 79 | + // identifier: value.identifier.toString(), |
| 80 | + // type: value.tryDoc?.type, |
| 81 | + // title: value.tryDoc?.title, |
| 82 | + // ydoc: JSON.stringify(value.tryDoc?.ydoc.toJSON()), |
| 83 | + // }; |
| 84 | + // }); |
| 85 | + |
| 86 | + const ydoc = selectedResource |
| 87 | + ? DocConnection.get( |
| 88 | + selectedResource, |
| 89 | + props.sessionStore, |
| 90 | + )?.tryDoc?.ydoc.toJSON() |
| 91 | + : undefined; |
| 92 | + |
| 93 | + return ( |
| 94 | + <Rnd |
| 95 | + style={{ zIndex: 19999000 }} |
| 96 | + default={{ |
| 97 | + x: 50, |
| 98 | + y: 50, |
| 99 | + width: window.innerWidth - 100, |
| 100 | + height: window.innerHeight - 100, |
| 101 | + }} |
| 102 | + dragHandleClassName={styles.header}> |
| 103 | + <div className={styles.devtools}> |
| 104 | + <div className={styles.header}>DevTools</div> |
| 105 | + <div className={styles.content}> |
| 106 | + <h1>Network</h1> |
| 107 | + <label> |
| 108 | + <input |
| 109 | + type="checkbox" |
| 110 | + onChange={(e) => { |
| 111 | + setFlaky(false); |
| 112 | + setOffline(e.target.checked); |
| 113 | + }} |
| 114 | + checked={offline} |
| 115 | + />{" "} |
| 116 | + Offline |
| 117 | + </label> |
| 118 | + <label> |
| 119 | + <input |
| 120 | + type="checkbox" |
| 121 | + onChange={(e) => setFlaky(e.target.checked)} |
| 122 | + checked={flaky} |
| 123 | + />{" "} |
| 124 | + Flaky (offline / online every 5s) |
| 125 | + </label> |
| 126 | + <h1>Documents</h1> |
| 127 | + <div className={styles.tableContainer}> |
| 128 | + <label> |
| 129 | + <input |
| 130 | + type="checkbox" |
| 131 | + onChange={(e) => setOnlyLoaded(e.target.checked)} |
| 132 | + checked={onlyLoaded} |
| 133 | + />{" "} |
| 134 | + Only show loaded |
| 135 | + </label> |
| 136 | + <Inspector |
| 137 | + className="test" |
| 138 | + table={true} |
| 139 | + data={documentCoordinatorEntries} |
| 140 | + /> |
| 141 | + </div> |
| 142 | + <h1>Resources</h1> |
| 143 | + <div className={styles.selectContainer}> |
| 144 | + <Select |
| 145 | + // cacheOptions |
| 146 | + // onChange={onChange} |
| 147 | + // loadOptions={searchUsers} |
| 148 | + // menuPosition="fixed" |
| 149 | + backspaceRemovesValue |
| 150 | + isClearable |
| 151 | + placeholder="Select resource…" |
| 152 | + onChange={(v) => setSelectedResource(v?.value)} |
| 153 | + options={selectOptions} |
| 154 | + /> |
| 155 | + </div> |
| 156 | + {selectedResource && <Inspector table={false} data={ydoc} />} |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + </Rnd> |
| 160 | + ); |
| 161 | +}); |
0 commit comments