Skip to content

Commit 54908eb

Browse files
authored
Merge pull request #65 from boostcampwm-2024/feature-fe-#51
NoteNode 타입 및 커스텀 컴포넌트 구현, 쿼리 결과로 노드 렌더링 구현
2 parents 0772860 + 04d72e7 commit 54908eb

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Handle, NodeProps, Position, type Node } from "@xyflow/react";
2+
import usePageStore from "@/store/usePageStore";
3+
4+
export type NoteNodeData = { title: string; id: number };
5+
export type NoteNodeType = Node<NoteNodeData, "note">;
6+
7+
export function NoteNode({ data }: NodeProps<NoteNodeType>) {
8+
const { setCurrentPage } = usePageStore();
9+
10+
const handleNodeClick = () => {
11+
const id = data.id;
12+
if (id === undefined || id === null) {
13+
return;
14+
}
15+
16+
setCurrentPage(id);
17+
};
18+
19+
return (
20+
<div
21+
className="rounded-md border-[1px] border-black bg-neutral-100 p-2"
22+
onClick={handleNodeClick}
23+
>
24+
<Handle
25+
type="source"
26+
id="left"
27+
position={Position.Left}
28+
isConnectable={true}
29+
/>
30+
<Handle
31+
type="source"
32+
id="top"
33+
position={Position.Top}
34+
isConnectable={true}
35+
/>
36+
<Handle
37+
type="source"
38+
id="right"
39+
position={Position.Right}
40+
isConnectable={true}
41+
/>
42+
<Handle
43+
type="source"
44+
id="bottom"
45+
position={Position.Bottom}
46+
isConnectable={true}
47+
/>
48+
{data.title}
49+
</div>
50+
);
51+
}

frontend/src/components/canvas/index.tsx

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cn } from "@/lib/utils";
22

3-
import { useCallback } from "react";
3+
import { useCallback, useMemo } from "react";
44
import {
55
ReactFlow,
66
MiniMap,
@@ -10,18 +10,49 @@ import {
1010
useEdgesState,
1111
addEdge,
1212
BackgroundVariant,
13+
ConnectionMode,
1314
type OnConnect,
1415
type Node,
1516
type Edge,
1617
} from "@xyflow/react";
1718

1819
import "@xyflow/react/dist/style.css";
1920

20-
const initialNodes: Node[] = [
21-
{ id: "1", position: { x: 0, y: 0 }, data: { label: "1" } },
22-
{ id: "2", position: { x: 0, y: 100 }, data: { label: "2" } },
21+
import { useEffect } from "react";
22+
import { usePages } from "@/hooks/usePages";
23+
import { type NoteNodeType, NoteNode } from "./NoteNode";
24+
25+
// 테스트용 초기값
26+
const initialNodes: NoteNodeType[] = [
27+
{
28+
id: "1",
29+
position: { x: 100, y: 100 },
30+
type: "note",
31+
data: {
32+
id: 0,
33+
title: "Node 1",
34+
},
35+
},
36+
{
37+
id: "2",
38+
position: { x: 400, y: 200 },
39+
type: "note",
40+
data: {
41+
id: 1,
42+
title: "Node 2",
43+
},
44+
},
45+
];
46+
47+
const initialEdges: Edge[] = [
48+
{
49+
id: "e1-2",
50+
source: "1",
51+
target: "2",
52+
sourceHandle: "top",
53+
targetHandle: "left",
54+
},
2355
];
24-
const initialEdges: Edge[] = [{ id: "e1-2", source: "1", target: "2" }];
2556

2657
const proOptions = { hideAttribution: true };
2758

@@ -30,14 +61,32 @@ interface CanvasProps {
3061
}
3162

3263
export default function Canvas({ className }: CanvasProps) {
33-
const [nodes, , onNodesChange] = useNodesState(initialNodes);
64+
const [nodes, setNodes, onNodesChange] = useNodesState<Node>(initialNodes);
3465
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
3566

67+
const { data } = usePages();
68+
const pages = data?.data;
69+
70+
useEffect(() => {
71+
if (!pages) {
72+
return;
73+
}
74+
75+
const newNodes = pages.map((page, index) => ({
76+
id: page.id.toString(),
77+
position: { x: 100 * index, y: 100 },
78+
data: { label: page.title, id: page.id },
79+
}));
80+
setNodes(newNodes);
81+
}, [pages, setNodes]);
82+
3683
const onConnect: OnConnect = useCallback(
3784
(params) => setEdges((eds) => addEdge(params, eds)),
3885
[setEdges],
3986
);
4087

88+
const nodeTypes = useMemo(() => ({ note: NoteNode }), []);
89+
4190
return (
4291
<div className={cn("", className)}>
4392
<ReactFlow
@@ -47,6 +96,8 @@ export default function Canvas({ className }: CanvasProps) {
4796
onEdgesChange={onEdgesChange}
4897
onConnect={onConnect}
4998
proOptions={proOptions}
99+
nodeTypes={nodeTypes}
100+
connectionMode={ConnectionMode.Loose}
50101
>
51102
<Controls />
52103
<MiniMap />

0 commit comments

Comments
 (0)