|
1 | 1 | import randomWords from "random-words";
|
2 |
| -import React, { useEffect, useRef } from "react"; |
| 2 | +import { v4 as uuid } from "uuid"; |
| 3 | +import React, { useMemo, useState, useEffect, useCallback } from "react"; |
3 | 4 | import { connectToDB, getConnection } from "./sharedb";
|
| 5 | +import { useTransition } from "./react-experimental"; |
| 6 | +import { |
| 7 | + Link, |
| 8 | + BrowserRouter as Router, |
| 9 | + useHistory, |
| 10 | + useLocation, |
| 11 | +} from "react-router-dom"; |
4 | 12 |
|
5 |
| -const loadNewFlow = () => { |
6 |
| - window.location.href = [window.location.origin, randomWords()].join("#"); |
7 |
| - // should react to url change rather than full reload |
8 |
| - window.location.reload(); |
| 13 | +interface Node { |
| 14 | + text: string; |
| 15 | +} |
| 16 | + |
| 17 | +type Flow = { |
| 18 | + nodes: Record<string, Node>; |
| 19 | + edges: Array<[string | null, string]>; |
9 | 20 | };
|
10 | 21 |
|
11 |
| -const Flow: React.FC<{ id: string }> = ({ id }) => { |
12 |
| - const [state, setState] = React.useState<any>(); |
13 |
| - const docRef = useRef(null); |
| 22 | +// Custom hook for talking to a flow in ShareDB |
| 23 | +function useFlow(config: { |
| 24 | + id: string; |
| 25 | +}): { |
| 26 | + state: Flow | null; |
| 27 | + addNode: () => void; |
| 28 | + removeNode: (id: string) => void; |
| 29 | + reset: (flow: Flow) => void; |
| 30 | + isPending: boolean; |
| 31 | +} { |
| 32 | + // Setup |
14 | 33 |
|
15 |
| - useEffect(() => { |
16 |
| - // should probably useContext or something rather than |
17 |
| - // this useRef stuff |
18 |
| - docRef.current = getConnection(id); |
19 |
| - const doc = docRef.current; |
| 34 | + const [startTransition, isPending] = useTransition(); |
| 35 | + |
| 36 | + const [state, setState] = useState<Flow | null>(null); |
| 37 | + |
| 38 | + const doc = useMemo(() => getConnection(config.id), [config.id]); |
20 | 39 |
|
| 40 | + useEffect(() => { |
21 | 41 | const cloneStateFromShareDB = () =>
|
22 |
| - setState(JSON.parse(JSON.stringify(doc.data))); |
| 42 | + startTransition(() => { |
| 43 | + setState(JSON.parse(JSON.stringify(doc.data))); |
| 44 | + }); |
23 | 45 |
|
24 | 46 | connectToDB(doc).then(() => {
|
25 | 47 | cloneStateFromShareDB();
|
26 | 48 | doc.on("op", cloneStateFromShareDB);
|
27 | 49 | });
|
28 | 50 |
|
29 | 51 | return () => {
|
30 |
| - docRef.current.destroy(); |
| 52 | + setState(null); |
| 53 | + doc.destroy(); |
31 | 54 | };
|
32 |
| - }, []); |
| 55 | + }, [doc, startTransition]); |
| 56 | + |
| 57 | + // Methods |
| 58 | + |
| 59 | + const addNode = useCallback(() => { |
| 60 | + doc.submitOp([{ p: ["nodes", uuid()], oi: { text: randomWords() } }]); |
| 61 | + }, [doc]); |
| 62 | + |
| 63 | + const removeNode = useCallback( |
| 64 | + (id) => { |
| 65 | + doc.submitOp([{ p: ["nodes", id], od: {} }]); |
| 66 | + }, |
| 67 | + [doc] |
| 68 | + ); |
| 69 | + |
| 70 | + const reset = useCallback( |
| 71 | + (flow) => { |
| 72 | + doc.submitOp([{ p: [], od: doc.data, oi: flow }]); |
| 73 | + }, |
| 74 | + [doc] |
| 75 | + ); |
| 76 | + |
| 77 | + // Public API |
| 78 | + |
| 79 | + return { |
| 80 | + state, |
| 81 | + addNode, |
| 82 | + removeNode, |
| 83 | + reset, |
| 84 | + isPending, |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +const Flow: React.FC<{ id: string }> = ({ id }) => { |
| 89 | + const flow = useFlow({ id }); |
| 90 | + |
| 91 | + if (flow.state === null) { |
| 92 | + return <p>Loading...</p>; |
| 93 | + } |
33 | 94 |
|
34 | 95 | return (
|
35 |
| - <div> |
36 |
| - {state?.nodes && |
37 |
| - Object.keys(state.nodes).map((k) => ( |
38 |
| - <Node key={k} doc={docRef.current} id={k} /> |
| 96 | + <> |
| 97 | + <main> |
| 98 | + <button |
| 99 | + onClick={() => { |
| 100 | + flow.addNode(); |
| 101 | + }} |
| 102 | + > |
| 103 | + Add |
| 104 | + </button> |
| 105 | + <button |
| 106 | + onClick={() => { |
| 107 | + fetch("/flow.json") |
| 108 | + .then((res) => res.json()) |
| 109 | + .then((flowData) => { |
| 110 | + flow.reset(flowData); |
| 111 | + }); |
| 112 | + }} |
| 113 | + > |
| 114 | + Import flow |
| 115 | + </button> |
| 116 | + <button |
| 117 | + onClick={() => { |
| 118 | + flow.reset({ |
| 119 | + nodes: {}, |
| 120 | + edges: [], |
| 121 | + }); |
| 122 | + }} |
| 123 | + > |
| 124 | + Reset |
| 125 | + </button> |
| 126 | + {Object.keys(flow.state.nodes).map((k) => ( |
| 127 | + <NodeView |
| 128 | + key={k} |
| 129 | + onRemove={flow.removeNode} |
| 130 | + id={k} |
| 131 | + node={flow.state.nodes[k]} |
| 132 | + /> |
39 | 133 | ))}
|
| 134 | + </main> |
| 135 | + {flow.isPending && <div className="overlay" />} |
| 136 | + </> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +const NodeView = React.memo( |
| 141 | + ({ |
| 142 | + id, |
| 143 | + node, |
| 144 | + onRemove, |
| 145 | + }: { |
| 146 | + id: string; |
| 147 | + node: Node; |
| 148 | + onRemove: (id: string) => void; |
| 149 | + }) => ( |
| 150 | + <div className="node"> |
40 | 151 | <button
|
| 152 | + className="remove-button" |
41 | 153 | onClick={() => {
|
42 |
| - // add a node |
43 |
| - docRef.current.submitOp([{ p: ["nodes", randomWords()], oi: {} }]); |
| 154 | + onRemove(id); |
44 | 155 | }}
|
45 | 156 | >
|
46 |
| - ADD |
| 157 | + × |
47 | 158 | </button>
|
48 |
| - |
49 |
| - <button onClick={loadNewFlow}>New flow</button> |
| 159 | + <p> |
| 160 | + {node.text || "unset"} {Math.round(Math.random() * 1000)} |
| 161 | + </p> |
50 | 162 | </div>
|
| 163 | + ), |
| 164 | + (prevProps, nextProps) => |
| 165 | + prevProps.id === nextProps.id && |
| 166 | + prevProps.onRemove === nextProps.onRemove && |
| 167 | + JSON.stringify(prevProps.node) === JSON.stringify(nextProps.node) |
| 168 | +); |
| 169 | + |
| 170 | +const SimpleLink = ({ to }: { to: string }) => { |
| 171 | + const location = useLocation(); |
| 172 | + return ( |
| 173 | + <Link to={to}>{location.hash === to ? <strong>{to}</strong> : to}</Link> |
51 | 174 | );
|
52 | 175 | };
|
53 | 176 |
|
54 |
| -const Node = React.memo(({ id, doc }: any) => ( |
55 |
| - <h1 |
56 |
| - onClick={() => { |
57 |
| - // remove the node |
58 |
| - doc.submitOp([{ p: ["nodes", id], od: doc.data.nodes[id] }]); |
59 |
| - }} |
60 |
| - > |
61 |
| - {id} {Math.round(Math.random() * 1000)} |
62 |
| - </h1> |
63 |
| -)); |
64 |
| - |
65 | 177 | const App = () => {
|
66 |
| - let [, id] = window.location.href.split("#"); |
67 |
| - if (!id) loadNewFlow(); |
| 178 | + const history = useHistory(); |
| 179 | + const location = useLocation(); |
| 180 | + |
| 181 | + const id = useMemo(() => { |
| 182 | + if (location.hash.length < 2) { |
| 183 | + return null; |
| 184 | + } |
| 185 | + return location.hash.slice(1); |
| 186 | + }, [location]); |
| 187 | + |
| 188 | + // If there is no ID readable from the hash, redirect to a freshly created one |
| 189 | + useEffect(() => { |
| 190 | + if (id === null) { |
| 191 | + history.push(`#${randomWords()}`); |
| 192 | + } |
| 193 | + }, [id, history]); |
68 | 194 |
|
69 |
| - return <Flow id={id} />; |
| 195 | + if (id === null) { |
| 196 | + return <p>Redirecting...</p>; |
| 197 | + } |
| 198 | + |
| 199 | + return ( |
| 200 | + <div> |
| 201 | + <nav> |
| 202 | + <SimpleLink to="#direct" /> |
| 203 | + <SimpleLink to="#captain" /> |
| 204 | + <button |
| 205 | + onClick={() => { |
| 206 | + history.push(`#${randomWords()}`); |
| 207 | + }} |
| 208 | + > |
| 209 | + New flow |
| 210 | + </button> |
| 211 | + </nav> |
| 212 | + <Flow id={id} /> |
| 213 | + </div> |
| 214 | + ); |
| 215 | +}; |
| 216 | + |
| 217 | +const Container = () => { |
| 218 | + return ( |
| 219 | + <Router> |
| 220 | + <App /> |
| 221 | + </Router> |
| 222 | + ); |
70 | 223 | };
|
71 | 224 |
|
72 |
| -export default App; |
| 225 | +export default Container; |
0 commit comments