feat(react): add useConnection

This commit is contained in:
moklick
2023-12-23 17:13:35 +01:00
parent c1b53f3a36
commit 3d1834b138
4 changed files with 109 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
import { shallow } from 'zustand/shallow';
import { useStore } from './useStore';
import type { ReactFlowStore } from '../types/store';
const selector = (s: ReactFlowStore) => ({
startHandle: s.connectionStartHandle,
endHandle: s.connectionEndHandle,
status: s.connectionStatus,
position: s.connectionStartHandle ? s.connectionPosition : null,
});
/**
* Hook for accessing the ongoing connection.
*
* @returns ongoing connection: startHandle, endHandle, status, position
*/
export function useConnection(): {
startHandle: ReactFlowStore['connectionStartHandle'];
endHandle: ReactFlowStore['connectionEndHandle'];
status: ReactFlowStore['connectionStatus'];
position: ReactFlowStore['connectionPosition'] | null;
} {
const ongoingConnection = useStore(selector, shallow);
return ongoingConnection;
}