Skip to content

Commit 04b880d

Browse files
chore(ui): "useInputFieldLabel" -> "useInputFieldLabelSafe"
Also update docstrings
1 parent 64ff0b5 commit 04b880d

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

invokeai/frontend/web/src/features/nodes/components/flow/nodes/Invocation/fields/InputFieldTitle.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
useIsConnectionStartField,
1010
} from 'features/nodes/hooks/useFieldConnectionState';
1111
import { useInputFieldIsConnected } from 'features/nodes/hooks/useInputFieldIsConnected';
12-
import { useInputFieldLabel } from 'features/nodes/hooks/useInputFieldLabel';
12+
import { useInputFieldLabelSafe } from 'features/nodes/hooks/useInputFieldLabelSafe';
1313
import { useInputFieldTemplateTitle } from 'features/nodes/hooks/useInputFieldTemplateTitle';
1414
import { fieldLabelChanged } from 'features/nodes/store/nodesSlice';
1515
import { HANDLE_TOOLTIP_OPEN_DELAY, NO_FIT_ON_DOUBLE_CLICK_CLASS } from 'features/nodes/types/constants';
@@ -43,7 +43,7 @@ interface Props {
4343
export const InputFieldTitle = memo((props: Props) => {
4444
const { nodeId, fieldName, isInvalid, isDragging } = props;
4545
const inputRef = useRef<HTMLInputElement>(null);
46-
const label = useInputFieldLabel(nodeId, fieldName);
46+
const label = useInputFieldLabelSafe(nodeId, fieldName);
4747
const fieldTemplateTitle = useInputFieldTemplateTitle(nodeId, fieldName);
4848
const { t } = useTranslation();
4949
const isConnected = useInputFieldIsConnected(nodeId, fieldName);

invokeai/frontend/web/src/features/nodes/components/sidePanel/builder/NodeFieldElementLabel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Flex, FormLabel, Spacer } from '@invoke-ai/ui-library';
22
import { NodeFieldElementResetToInitialValueIconButton } from 'features/nodes/components/flow/nodes/Invocation/fields/NodeFieldElementResetToInitialValueIconButton';
3-
import { useInputFieldLabel } from 'features/nodes/hooks/useInputFieldLabel';
3+
import { useInputFieldLabelSafe } from 'features/nodes/hooks/useInputFieldLabelSafe';
44
import { useInputFieldTemplateOrThrow } from 'features/nodes/hooks/useInputFieldTemplateOrThrow';
55
import type { NodeFieldElement } from 'features/nodes/types/workflow';
66
import { memo, useMemo } from 'react';
77

88
export const NodeFieldElementLabel = memo(({ el }: { el: NodeFieldElement }) => {
99
const { data } = el;
1010
const { fieldIdentifier } = data;
11-
const label = useInputFieldLabel(fieldIdentifier.nodeId, fieldIdentifier.fieldName);
11+
const label = useInputFieldLabelSafe(fieldIdentifier.nodeId, fieldIdentifier.fieldName);
1212
const fieldTemplate = useInputFieldTemplateOrThrow(fieldIdentifier.nodeId, fieldIdentifier.fieldName);
1313

1414
const _label = useMemo(() => label || fieldTemplate.title, [label, fieldTemplate.title]);

invokeai/frontend/web/src/features/nodes/components/sidePanel/builder/NodeFieldElementLabelEditable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Flex, FormLabel, Input, Spacer } from '@invoke-ai/ui-library';
22
import { useAppDispatch } from 'app/store/storeHooks';
33
import { useEditable } from 'common/hooks/useEditable';
44
import { NodeFieldElementResetToInitialValueIconButton } from 'features/nodes/components/flow/nodes/Invocation/fields/NodeFieldElementResetToInitialValueIconButton';
5-
import { useInputFieldLabel } from 'features/nodes/hooks/useInputFieldLabel';
5+
import { useInputFieldLabelSafe } from 'features/nodes/hooks/useInputFieldLabelSafe';
66
import { useInputFieldTemplateOrThrow } from 'features/nodes/hooks/useInputFieldTemplateOrThrow';
77
import { fieldLabelChanged } from 'features/nodes/store/nodesSlice';
88
import type { NodeFieldElement } from 'features/nodes/types/workflow';
@@ -12,7 +12,7 @@ export const NodeFieldElementLabelEditable = memo(({ el }: { el: NodeFieldElemen
1212
const { data } = el;
1313
const { fieldIdentifier } = data;
1414
const dispatch = useAppDispatch();
15-
const label = useInputFieldLabel(fieldIdentifier.nodeId, fieldIdentifier.fieldName);
15+
const label = useInputFieldLabelSafe(fieldIdentifier.nodeId, fieldIdentifier.fieldName);
1616
const fieldTemplate = useInputFieldTemplateOrThrow(fieldIdentifier.nodeId, fieldIdentifier.fieldName);
1717
const inputRef = useRef<HTMLInputElement>(null);
1818

invokeai/frontend/web/src/features/nodes/hooks/useInputFieldLabel.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createSelector } from '@reduxjs/toolkit';
2+
import { useAppSelector } from 'app/store/storeHooks';
3+
import { selectFieldInputInstanceSafe, selectNodesSlice } from 'features/nodes/store/selectors';
4+
import { useMemo } from 'react';
5+
6+
/**
7+
* Gets the user-defined label of an input field for a given node.
8+
*
9+
* If the node doesn't exist or is not an invocation node, an empty string is returned.
10+
*
11+
* @param nodeId The ID of the node
12+
* @param fieldName The name of the field
13+
*/
14+
export const useInputFieldLabelSafe = (nodeId: string, fieldName: string): string => {
15+
const selector = useMemo(
16+
() =>
17+
createSelector(selectNodesSlice, (nodes) => selectFieldInputInstanceSafe(nodes, nodeId, fieldName)?.label ?? ''),
18+
[fieldName, nodeId]
19+
);
20+
21+
const label = useAppSelector(selector);
22+
23+
return label;
24+
};

0 commit comments

Comments
 (0)