fix(ui): crash when using a notes node

This commit is contained in:
psychedelicious 2024-05-21 10:06:09 +10:00
parent 66c9f4708d
commit 1249d4a6e3
2 changed files with 22 additions and 6 deletions

View File

@ -1,14 +1,14 @@
import { createSelector } from '@reduxjs/toolkit';
import { useAppSelector } from 'app/store/storeHooks';
import { selectNodesSlice } from 'features/nodes/store/nodesSlice';
import { selectNodeData } from 'features/nodes/store/selectors';
import { useMemo } from 'react';
export const useNodeLabel = (nodeId: string) => {
const selector = useMemo(
() =>
createSelector(selectNodesSlice, (nodes) => {
return selectNodeData(nodes, nodeId)?.label ?? null;
createSelector(selectNodesSlice, (nodesSlice) => {
const node = nodesSlice.nodes.find((node) => node.id === nodeId);
return node?.data.label;
}),
[nodeId]
);

View File

@ -1,8 +1,24 @@
import { useNodeTemplate } from 'features/nodes/hooks/useNodeTemplate';
import { useStore } from '@nanostores/react';
import { createSelector } from '@reduxjs/toolkit';
import { useAppSelector } from 'app/store/storeHooks';
import { $templates, selectNodesSlice } from 'features/nodes/store/nodesSlice';
import { isInvocationNode } from 'features/nodes/types/invocation';
import { useMemo } from 'react';
export const useNodeTemplateTitle = (nodeId: string): string | null => {
const template = useNodeTemplate(nodeId);
const title = useMemo(() => template.title, [template.title]);
const templates = useStore($templates);
const selector = useMemo(
() =>
createSelector(selectNodesSlice, (nodesSlice) => {
const node = nodesSlice.nodes.find((node) => node.id === nodeId);
if (!isInvocationNode(node)) {
return null;
}
const template = templates[node.data.type];
return template?.title ?? null;
}),
[nodeId, templates]
);
const title = useAppSelector(selector);
return title;
};