Skip to content

Commit e9bb33c

Browse files
committed
Node data type
1 parent c267563 commit e9bb33c

File tree

5 files changed

+120
-21
lines changed

5 files changed

+120
-21
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@types/node": "^12.0.0",
1313
"@types/react": "^16.9.0",
1414
"@types/react-dom": "^16.9.0",
15+
"@types/uuid": "^8.0.0",
1516
"comlink": "^4.3.0",
1617
"express": "^4.17.1",
1718
"nanoid": "^3.1.10",
@@ -24,6 +25,7 @@
2425
"reconnecting-websocket": "^4.4.0",
2526
"sharedb": "^1.4.0",
2627
"typescript": "~3.7.2",
28+
"uuid": "^8.2.0",
2729
"ws": "^7.3.1"
2830
},
2931
"scripts": {

src/App.tsx

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import randomWords from "random-words";
2+
import { v4 as uuid } from "uuid";
23
import React, { useEffect, useMemo } from "react";
34
import { connectToDB, getConnection } from "./sharedb";
45
import {
@@ -8,8 +9,12 @@ import {
89
useLocation,
910
} from "react-router-dom";
1011

12+
interface Node {
13+
text: string;
14+
}
15+
1116
type Flow = {
12-
nodes: Record<string, {}>;
17+
nodes: Record<string, Node>;
1318
edges: Array<[string | null, string]>;
1419
};
1520

@@ -46,7 +51,7 @@ function useFlow(config: {
4651
// Methods
4752

4853
const addNode = React.useCallback(() => {
49-
doc.submitOp([{ p: ["nodes", randomWords()], oi: {} }]);
54+
doc.submitOp([{ p: ["nodes", uuid()], oi: { text: randomWords() } }]);
5055
}, [doc]);
5156

5257
const removeNode = React.useCallback(
@@ -81,16 +86,13 @@ const Flow: React.FC<{ id: string }> = ({ id }) => {
8186
}
8287

8388
return (
84-
<div>
85-
{Object.keys(flow.state.nodes).map((k) => (
86-
<Node key={k} onRemove={flow.removeNode} id={k} />
87-
))}
89+
<main>
8890
<button
8991
onClick={() => {
9092
flow.addNode();
9193
}}
9294
>
93-
ADD
95+
Add
9496
</button>
9597
<button
9698
onClick={() => {
@@ -101,25 +103,67 @@ const Flow: React.FC<{ id: string }> = ({ id }) => {
101103
});
102104
}}
103105
>
104-
RESET
106+
Import flow
105107
</button>
106-
</div>
108+
<button
109+
onClick={() => {
110+
flow.reset({
111+
nodes: {},
112+
edges: [],
113+
});
114+
}}
115+
>
116+
Reset
117+
</button>
118+
{Object.keys(flow.state.nodes).map((k) => (
119+
<NodeView
120+
key={k}
121+
onRemove={flow.removeNode}
122+
id={k}
123+
node={flow.state.nodes[k]}
124+
/>
125+
))}
126+
</main>
107127
);
108128
};
109129

110-
const Node = React.memo(
111-
({ id, onRemove }: { id: string; onRemove: (id: string) => void }) => (
112-
<h1
113-
onClick={() => {
114-
// remove the node
115-
onRemove(id);
116-
}}
117-
>
118-
{id} {Math.round(Math.random() * 1000)}
119-
</h1>
120-
)
130+
const NodeView = React.memo(
131+
({
132+
id,
133+
node,
134+
onRemove,
135+
}: {
136+
id: string;
137+
node: Node;
138+
onRemove: (id: string) => void;
139+
}) => (
140+
<div className="node">
141+
<button
142+
className="remove-button"
143+
onClick={() => {
144+
onRemove(id);
145+
}}
146+
>
147+
×
148+
</button>
149+
<p>
150+
{node.text || "unset"} {Math.round(Math.random() * 1000)}
151+
</p>
152+
</div>
153+
),
154+
(prevProps, nextProps) =>
155+
prevProps.id === nextProps.id &&
156+
prevProps.onRemove === nextProps.onRemove &&
157+
JSON.stringify(prevProps.node) === JSON.stringify(nextProps.node)
121158
);
122159

160+
const SimpleLink = ({ to }: { to: string }) => {
161+
const location = useLocation();
162+
return (
163+
<Link to={to}>{location.hash === to ? <strong>{to}</strong> : to}</Link>
164+
);
165+
};
166+
123167
const App = () => {
124168
const history = useHistory();
125169
const location = useLocation();
@@ -145,7 +189,8 @@ const App = () => {
145189
return (
146190
<div>
147191
<nav>
148-
<Link to="#direct">#direct</Link> <Link to="#captain">#captain</Link>{" "}
192+
<SimpleLink to="#direct" />
193+
<SimpleLink to="#captain" />
149194
<button
150195
onClick={() => {
151196
history.push(`#${randomWords()}`);

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import ReactDOM from "react-dom";
33
import App from "./App";
44
import * as serviceWorker from "./serviceWorker";
5+
import "./style.css";
56

67
ReactDOM.render(
78
<React.StrictMode>

src/style.css

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
nav {
2+
padding: 20px;
3+
border-bottom: 1px solid #cecece;
4+
}
5+
6+
main {
7+
padding: 20px;
8+
}
9+
10+
button,
11+
a {
12+
display: inline-block;
13+
margin-right: 8px;
14+
}
15+
16+
.node {
17+
display: flex;
18+
align-items: center;
19+
max-width: 360px;
20+
border-radius: 4px;
21+
margin-top: 20px;
22+
padding: 0 10px;
23+
box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.2);
24+
}
25+
26+
.remove-button {
27+
border: 0;
28+
background: none;
29+
color: red;
30+
width: 30px;
31+
height: 30px;
32+
font-size: 24px;
33+
flex: 0 0 30px;
34+
display: flex;
35+
align-items: center;
36+
justify-content: center;
37+
}
38+
39+
.remove-button:hover {
40+
background-color: #dedede;
41+
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,11 @@
16461646
"@types/testing-library__dom" "*"
16471647
pretty-format "^25.1.0"
16481648

1649+
"@types/uuid@^8.0.0":
1650+
version "8.0.0"
1651+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.0.0.tgz#165aae4819ad2174a17476dbe66feebd549556c0"
1652+
integrity sha512-xSQfNcvOiE5f9dyd4Kzxbof1aTrLobL278pGLKOZI6esGfZ7ts9Ka16CzIN6Y8hFHE1C7jIBZokULhK1bOgjRw==
1653+
16491654
"@types/yargs-parser@*":
16501655
version "15.0.0"
16511656
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
@@ -10940,6 +10945,11 @@ uuid@^3.0.1, uuid@^3.3.2:
1094010945
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
1094110946
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
1094210947

10948+
uuid@^8.2.0:
10949+
version "8.2.0"
10950+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.2.0.tgz#cb10dd6b118e2dada7d0cd9730ba7417c93d920e"
10951+
integrity sha512-CYpGiFTUrmI6OBMkAdjSDM0k5h8SkkiTP4WAjQgDgNB1S3Ou9VBEvr6q0Kv2H1mMk7IWfxYGpMH5sd5AvcIV2Q==
10952+
1094310953
v8-compile-cache@^2.0.3:
1094410954
version "2.1.0"
1094510955
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz#e14de37b31a6d194f5690d67efc4e7f6fc6ab30e"

0 commit comments

Comments
 (0)