mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(ui): add CanvasManagerProviderGate
This context waits to render its children its until the canvas manager is available. Then its children have access to the manager directly via hook.
This commit is contained in:
parent
0a2afed08b
commit
44a3f61580
@ -0,0 +1,25 @@
|
||||
import { useStore } from '@nanostores/react';
|
||||
import { $canvasManager, type CanvasManager } from 'features/controlLayers/konva/CanvasManager';
|
||||
import type { PropsWithChildren } from 'react';
|
||||
import { createContext, memo, useContext } from 'react';
|
||||
import { assert } from 'tsafe';
|
||||
|
||||
const CanvasManagerContext = createContext<CanvasManager | null>(null);
|
||||
|
||||
export const useCanvasManager = (): CanvasManager => {
|
||||
const canvasManager = useContext(CanvasManagerContext);
|
||||
assert(canvasManager, 'useCanvasManagerContext must be used within a CanvasManagerContext');
|
||||
return canvasManager;
|
||||
};
|
||||
|
||||
export const CanvasManagerProviderGate = memo(({ children }: PropsWithChildren) => {
|
||||
const canvasManager = useStore($canvasManager);
|
||||
|
||||
if (!canvasManager) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <CanvasManagerContext.Provider value={canvasManager}>{children}</CanvasManagerContext.Provider>;
|
||||
});
|
||||
|
||||
CanvasManagerProviderGate.displayName = 'CanvasManagerProviderGate';
|
Loading…
Reference in New Issue
Block a user