Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes node title not changing #536

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-rockets-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-editor": patch
---

fix token title not changes
52 changes: 33 additions & 19 deletions packages/graph-editor/src/components/panels/nodeSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,48 @@
);
});

const NodeTitle = ({ selectedNode }: { selectedNode: Node }) => {
const NodeTitle = observer(({ selectedNode }: { selectedNode: Node }) => {
SorsOps marked this conversation as resolved.
Show resolved Hide resolved
const [localTitle, setLocalTitle] = React.useState(
(selectedNode.annotations[title] as string) || '',
);

// Update local state when the annotation changes
React.useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The observer should trigger the rerender anyway, we shouldn't need to update the local state

setLocalTitle((selectedNode.annotations[title] as string) || '');
}, [selectedNode.annotations[title]]);

Check warning on line 92 in packages/graph-editor/src/components/panels/nodeSettings/index.tsx

View workflow job for this annotation

GitHub Actions / Verify changes

React Hook React.useEffect has a missing dependency: 'selectedNode.annotations'. Either include it or remove the dependency array. If 'setLocalTitle' needs the current value of 'selectedNode.annotations', you can also switch to useReducer instead of useState and read 'selectedNode.annotations' in the reducer

Check warning on line 92 in packages/graph-editor/src/components/panels/nodeSettings/index.tsx

View workflow job for this annotation

GitHub Actions / Verify changes

React Hook React.useEffect has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked

const onChangeTitle = useCallback(
(e) => {
selectedNode.annotations[title] = e.target.value;
(e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value;
setLocalTitle(newValue);
selectedNode.setAnnotation(title, newValue);
},
[selectedNode.annotations],
[selectedNode],
);

return (
<Stack direction="column" gap={2}>
<Label>Title</Label>
<TextInput
onChange={onChangeTitle}
value={selectedNode.annotations[title] as string}
/>
<TextInput onChange={onChangeTitle} value={localTitle} />
</Stack>
);
};
});

const NodeDescription = observer(({ selectedNode }: { selectedNode: Node }) => {
const [localDescription, setLocalDescription] = React.useState(
(selectedNode.annotations[description] as string) || '',
);

// Update local state when the annotation changes
React.useEffect(() => {
setLocalDescription(
(selectedNode.annotations[description] as string) || '',
);
}, [selectedNode.annotations[description]]);

Check warning on line 121 in packages/graph-editor/src/components/panels/nodeSettings/index.tsx

View workflow job for this annotation

GitHub Actions / Verify changes

React Hook React.useEffect has a missing dependency: 'selectedNode.annotations'. Either include it or remove the dependency array. If 'setLocalDescription' needs the current value of 'selectedNode.annotations', you can also switch to useReducer instead of useState and read 'selectedNode.annotations' in the reducer

Check warning on line 121 in packages/graph-editor/src/components/panels/nodeSettings/index.tsx

View workflow job for this annotation

GitHub Actions / Verify changes

React Hook React.useEffect has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked

const NodeDescription = ({
selectedNode,
annotations,
}: {
selectedNode: Node;
annotations: Record<string, unknown>;
}) => {
const onChangeDesc = useCallback(
(newString: string) => {
setLocalDescription(newString);
selectedNode.setAnnotation(description, newString);
},
[selectedNode],
Expand All @@ -120,11 +134,11 @@
<Textarea
placeholder={selectedNode.factory.description}
onChange={onChangeDesc}
value={annotations[description] as string}
value={localDescription}
/>
</Stack>
);
};
});

const NodeSettings = ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could actually do an observer here in the node Settings once to auto rerender the attributes here

selectedNode,
Expand All @@ -144,7 +158,7 @@
{selectedNode?.factory.type}
</Text>
<NodeTitle selectedNode={selectedNode} />
<NodeDescription selectedNode={selectedNode} annotations={annotations} />
<NodeDescription selectedNode={selectedNode} />
<Label>Annotations</Label>
<Annotations annotations={annotations} />
</Stack>
Expand Down
Loading