Skip to content

Commit bd9201f

Browse files
authored
Feat: Sync pod name among users in real-time (#132)
1 parent 9b88475 commit bd9201f

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

ui/src/components/Canvas.tsx

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const ScopeNode = memo<Props>(({ data, id, isConnectable }) => {
8484
});
8585
const selected = useStore(store, (state) => state.pods[id]?.selected);
8686
const role = useStore(store, (state) => state.role);
87+
const inputRef = useRef<HTMLInputElement>(null);
8788

8889
const deleteNodeById = useCallback(
8990
(id: string) => {
@@ -108,10 +109,17 @@ const ScopeNode = memo<Props>(({ data, id, isConnectable }) => {
108109
}
109110
}, []);
110111

111-
React.useEffect(() => {
112+
useEffect(() => {
112113
setTarget(ref.current);
113114
}, []);
114115

116+
useEffect(() => {
117+
setPodName({ id, name: data.name || "" });
118+
if (inputRef?.current) {
119+
inputRef.current.value = data.name;
120+
}
121+
}, [data.name, id, setPodName]);
122+
115123
return (
116124
<Box
117125
ref={ref}
@@ -179,12 +187,19 @@ const ScopeNode = memo<Props>(({ data, id, isConnectable }) => {
179187
}}
180188
>
181189
<InputBase
182-
defaultValue={pod.name || "Scope"}
190+
className="nodrag"
191+
defaultValue={data.name || "Scope"}
183192
onBlur={(e) => {
184193
const name = e.target.value;
185-
if (name === pod.name) return;
186-
setPodName({ id, name });
194+
if (name === data.name) return;
195+
const node = nodesMap.get(id);
196+
if (node) {
197+
nodesMap.set(id, { ...node, data: { ...node.data, name } });
198+
}
199+
// setPodName({ id, name });
187200
}}
201+
inputRef={inputRef}
202+
disabled={role === RoleType.GUEST}
188203
inputProps={{
189204
style: {
190205
padding: "0px",
@@ -409,6 +424,7 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
409424
const role = useStore(store, (state) => state.role);
410425
const width = useStore(store, (state) => state.pods[id]?.width);
411426
const isPodFocused = useStore(store, (state) => state.pods[id]?.focus);
427+
const inputRef = useRef<HTMLInputElement>(null);
412428

413429
const showResult = useStore(
414430
store,
@@ -438,6 +454,14 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
438454
useEffect(() => {
439455
setTarget(ref.current);
440456
}, []);
457+
458+
useEffect(() => {
459+
setPodName({ id, name: data.name });
460+
if (inputRef?.current) {
461+
inputRef.current.value = data.name || "";
462+
}
463+
}, [data.name, setPodName, id]);
464+
441465
if (!pod) return null;
442466

443467
// onsize is banned for a guest, FIXME: ugly code
@@ -506,11 +530,17 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
506530
}}
507531
>
508532
<InputBase
509-
defaultValue={pod.name}
533+
inputRef={inputRef}
534+
className="nodrag"
535+
defaultValue={data.name || ""}
536+
disabled={role === RoleType.GUEST}
510537
onBlur={(e) => {
511538
const name = e.target.value;
512-
if (name === pod.name) return;
513-
setPodName({ id, name });
539+
if (name === data.name) return;
540+
const node = nodesMap.get(id);
541+
if (node) {
542+
nodesMap.set(id, { ...node, data: { ...node.data, name } });
543+
}
514544
}}
515545
inputProps={{
516546
style: {
@@ -677,6 +707,7 @@ export function Canvas() {
677707
data: {
678708
// label: `ID: ${id}, parent: ${pods[id].parent}, pos: ${pods[id].x}, ${pods[id].y}`,
679709
label: id,
710+
name: pod.name,
680711
},
681712
// position: { x: 100, y: 100 },
682713
position: { x: pod.x, y: pod.y },
@@ -799,6 +830,7 @@ export function Canvas() {
799830
style,
800831
data: {
801832
label: id,
833+
name: "",
802834
},
803835
level: 0,
804836
extent: "parent",
@@ -870,7 +902,7 @@ export function Canvas() {
870902
});
871903
}
872904
},
873-
[nodesMap, setNodes, userColor]
905+
[setNodes, userColor]
874906
);
875907

876908
const onNodeDragStop = useCallback(

0 commit comments

Comments
 (0)