UNPKG

@deep-foundation/deepcase

Version:

[![Gitpod](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/deep-foundation/deepcase) [![Discord](https://badgen.net/badge/icon/discord?icon=discord&label&color=purple)](https://discord.gg/deep-

231 lines 8.95 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { useAuthNode, useDeep } from '@deep-foundation/deeplinks/imports/client'; import { useLocalStore } from '@deep-foundation/store/local'; import { useQueryStore } from '@deep-foundation/store/query'; import { useEffect, useRef, useState, useMemo } from 'react'; import { layouts } from './cyto/layouts'; import { useMediaQuery as useMediaQueryChakra } from '@chakra-ui/react'; export const defaultLeftWidth = 10; export const defaultCardWidth = 300; export function useClickSelect() { return useLocalStore('click-select', true); } export function useTraveler() { return useQueryStore('traveler', false); } export function useContainer() { const [spaceId] = useSpaceId(); const store = useQueryStore('container', 0); return useMemo(() => [store[0] || spaceId, store[1]], [spaceId, store[0]]); } export function useForceGraph() { return useQueryStore('force-graph-type', '2d'); } export function useInsertingCytoStore() { return useQueryStore('dc-dg-ins', {}); } export function useUpdatingCytoStore() { return useQueryStore('dc-dg-upd', {}); } export function useScreenFind() { return useQueryStore('screen-find', ''); } export function useSpaceId() { const [linkId] = useAuthNode(); const store = useQueryStore('space-id', linkId); return useMemo(() => [store[0] || linkId, store[1]], [linkId, store[0]]); } export function useWindowSize() { return useLocalStore('window-size', { width: 800, height: 500 }); } ; export function useAutoFocusOnInsert() { return useQueryStore('autofocus-on-insert', true); } ; export function useShowExtra() { return useQueryStore('show-extra', false); } ; export function useCytoViewport() { const x = typeof (window) === 'object' ? window.innerWidth / 2 : 0; const y = typeof (window) === 'object' ? window.innerHeight / 2 : 0; return useLocalStore('cyto-viewport', { pan: { x, y }, zoom: 1 }); } ; export function useShowFocus() { return useQueryStore('show-focus', false); } ; export function useShowOpened() { return useQueryStore('show-opened', false); } ; export function useCytoHandlersSwitch() { return useQueryStore('cyto-handlers', false); } ; export function useBreadcrumbs() { return useQueryStore('breadcrumbs', false); } ; export function useLayoutAnimation() { return useQueryStore('layout-animation', false); } ; export function useReserved() { return useQueryStore('reserved', false); } ; export function useShowTypes() { return useQueryStore('show-types', true); } export function useBackgroundTransparent() { return useQueryStore('bg-transparent', false); } ; export function useFocusMethods() { const [spaceId] = useSpaceId(); const spaceIdRef = useRefAutofill(spaceId); const deep = useDeep(); return useMemo(() => { return { unfocus: (id) => __awaiter(this, void 0, void 0, function* () { console.log('unfocus', { spaceId, id }); const whereF = { type_id: deep.idLocal('@deep-foundation/core', 'Focus'), from_id: spaceIdRef.current, to_id: id }; const where = { _or: [ whereF, { type_id: deep.idLocal('@deep-foundation/core', 'Contain'), from_id: spaceIdRef.current, to: whereF }, ], }; console.log('unfocused', yield deep.delete(where)); }), focus: (id, value) => __awaiter(this, void 0, void 0, function* () { var _a; console.log('focus', { spaceId, id, value }); const q = yield deep.select({ type_id: deep.idLocal('@deep-foundation/core', 'Focus'), from_id: spaceIdRef.current, to_id: id, }); const focus = (_a = q === null || q === void 0 ? void 0 : q.data) === null || _a === void 0 ? void 0 : _a[0]; let focusId = focus === null || focus === void 0 ? void 0 : focus.id; if (!focusId) { const { data: [{ id: newFocusId }] } = yield deep.insert({ type_id: deep.idLocal('@deep-foundation/core', 'Focus'), from_id: spaceIdRef.current, to_id: id, object: { data: { value } }, in: { data: { type_id: deep.idLocal('@deep-foundation/core', 'Contain'), from_id: spaceIdRef.current } }, }); focusId = newFocusId; } else { if (focus.value) { yield deep.update({ link_id: { _eq: focusId }, }, { value }, { table: 'objects' }); } else { yield deep.insert({ link_id: focusId, value, }, { table: 'objects' }); } } console.log('focused', { spaceIdRefCurrent: spaceIdRef.current, id, value, focusId }); }) }; }, []); } ; export function useActiveMethods() { const [spaceId] = useSpaceId(); const deep = useDeep(); return useMemo(() => { return { deactive: function (id) { return __awaiter(this, void 0, void 0, function* () { console.log(yield deep.delete({ type_id: deep.idLocal('@deep-foundation/core', 'Active'), from_id: spaceId, to_id: id })); }); }, find: function (id) { return __awaiter(this, void 0, void 0, function* () { var _a; const q = yield deep.select({ type_id: deep.idLocal('@deep-foundation/core', 'Active'), from_id: spaceId, to_id: id, }); return (_a = q === null || q === void 0 ? void 0 : q.data) === null || _a === void 0 ? void 0 : _a[0]; }); }, active: function (id) { return __awaiter(this, void 0, void 0, function* () { const active = yield this.find(id); const { data: [{ id: newId }] } = yield deep.insert({ type_id: deep.idLocal('@deep-foundation/core', 'Active'), from_id: spaceId, to_id: id, in: { data: { type_id: deep.idLocal('@deep-foundation/core', 'Contain'), from_id: spaceId } }, }); }); }, toggle: function (id) { return __awaiter(this, void 0, void 0, function* () { const active = yield this.find(id); let oldId = active === null || active === void 0 ? void 0 : active.id; if (!oldId) yield this.active(id); else yield this.deactive(id); }); }, }; }, []); } ; export function useLayout() { const [layoutName, setLayoutName] = useLocalStore('layout', 'deep-d3-force'); return { setLayout(name) { setLayoutName(name); }, layout: layouts[layoutName], layoutName, }; } ; export function useRefAutofill(value) { const ref = useRef(value); ref.current = value; return ref; } export const useMediaQuery = function useMediaQuery(arg) { const [actualValue, isBrowser] = useMediaQueryChakra(arg); const [value, setValue] = useState(false); useEffect(() => setValue(actualValue), [actualValue, isBrowser]); return [value, isBrowser]; }; export const useAsyncState = function useAsyncState(defaultValue, init, depends = []) { const [value, setValue] = useState(defaultValue); useEffect(() => { init().then(setValue); }, depends); return value; }; //# sourceMappingURL=hooks.js.map