mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-02-13 12:25:02 -05:00
- Build selectors for each node in a react context so components can re-use the same selectors - Cache the selectors in the context
25 lines
762 B
TypeScript
25 lines
762 B
TypeScript
import { createSelector } from '@reduxjs/toolkit';
|
|
import { useAppSelector } from 'app/store/storeHooks';
|
|
import { useInvocationNodeContext } from 'features/nodes/components/flow/nodes/Invocation/context';
|
|
import { useMemo } from 'react';
|
|
|
|
export const useInputFieldNameSafe = (fieldName: string) => {
|
|
const ctx = useInvocationNodeContext();
|
|
|
|
const selector = useMemo(
|
|
() =>
|
|
createSelector(
|
|
[ctx.buildSelectInputFieldSafe(fieldName), ctx.buildSelectInputFieldTemplateSafe(fieldName)],
|
|
(fieldInstance, fieldTemplate) => {
|
|
const name = fieldInstance?.label || fieldTemplate?.title || fieldName;
|
|
return name;
|
|
}
|
|
),
|
|
[fieldName, ctx]
|
|
);
|
|
|
|
const name = useAppSelector(selector);
|
|
|
|
return name;
|
|
};
|