|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import { NodeForm } from "../NodeForm"; |
| 4 | +import { Node } from "@/entities/node"; |
| 5 | +import { useYDocStore } from "@/shared/model"; |
| 6 | +import { Popover } from "@/shared/ui"; |
| 7 | + |
| 8 | +interface NodePanelProps { |
| 9 | + currentPage: string; |
| 10 | +} |
| 11 | + |
| 12 | +export function NodePanel({ currentPage }: NodePanelProps) { |
| 13 | + const [currentColor, setCurrentColor] = useState("#ffffff"); |
| 14 | + const { ydoc } = useYDocStore(); |
| 15 | + |
| 16 | + const changeNodeColor = (color: string) => { |
| 17 | + const nodesMap = ydoc.getMap("nodes"); |
| 18 | + const id = currentPage.toString(); |
| 19 | + |
| 20 | + const existingNode = nodesMap.get(id) as Node; |
| 21 | + nodesMap.set(id, { |
| 22 | + ...existingNode, |
| 23 | + data: { ...existingNode.data, color }, |
| 24 | + }); |
| 25 | + setCurrentColor(color); |
| 26 | + }; |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const nodesMap = ydoc.getMap("nodes"); |
| 30 | + const currentNode = nodesMap.get(currentPage.toString()) as Node; |
| 31 | + setCurrentColor(currentNode.data.color as string); |
| 32 | + }, [currentPage]); |
| 33 | + |
| 34 | + return ( |
| 35 | + <Popover placement="bottom" align="start" offset={{ x: -11, y: 16 }}> |
| 36 | + <Popover.Trigger> |
| 37 | + <button |
| 38 | + className="flex h-7 w-7 items-center justify-center rounded-md border-[1px] border-neutral-300" |
| 39 | + style={{ backgroundColor: currentColor }} |
| 40 | + /> |
| 41 | + </Popover.Trigger> |
| 42 | + <Popover.Content className="rounded-lg border bg-white p-3 shadow-md"> |
| 43 | + <NodeForm changeNodeColor={changeNodeColor} /> |
| 44 | + </Popover.Content> |
| 45 | + </Popover> |
| 46 | + ); |
| 47 | +} |
0 commit comments