UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

158 lines (157 loc) 4.77 kB
import { useState as i, useMemo as f, useCallback as v, useEffect as k } from "react"; import { useQueryClient as G } from "@tanstack/react-query"; import { matchQuery as _ } from "@tanstack/query-core"; import { SortBy as Q, Direction as H } from "@sage-bionetworks/synapse-types"; import { EntityType as s } from "@sage-bionetworks/synapse-client"; import "../../../synapse-client/SynapseClient.js"; import "@sage-bionetworks/synapse-client/generated/models/ErrorResponseCode"; import "@sage-bionetworks/synapse-client/generated/models/TwoFactorAuthErrorResponse"; import "@sage-bionetworks/synapse-client/util/SynapseClientError"; import { convertToEntityType as L } from "../../../utils/functions/EntityTypeUtils.js"; import "../../../utils/SynapseConstants.js"; import "lodash-es"; import "@sage-bionetworks/synapse-client/util/synapseClientFetch"; import "../../../utils/PermissionLevelToAccessType.js"; import { useSynapseContext as R } from "../../../utils/context/SynapseContext.js"; import "use-deep-compare-effect"; import "react/jsx-runtime"; import "@mui/material"; import "../../../utils/hooks/useCookiePreferences.js"; import "../../../utils/hooks/useSourceAppConfigs.js"; import "universal-cookie"; import "../../../utils/AppUtils/session/ApplicationSessionContext.js"; import "../../../utils/context/FullContextProvider.js"; import "../../../utils/context/DocumentMetadataContext.js"; import '../../../style/components/_spinner.css';/* empty css */ import "lodash-es/isEmpty"; import "lodash-es/isEqual"; import "lodash-es/xorWith"; import "react-router"; import "../../../utils/types/IsType.js"; import { useGetEntityChildren as z } from "../../../synapse-queries/entity/useGetEntityChildren.js"; import { useGetEntityHeader as J } from "../../../synapse-queries/entity/useGetEntityHeaders.js"; const U = [ s.folder, s.file, s.link, s.recordset ], xe = (e, x, l) => { const [P, E] = i({}), [a, g] = i({}), [w, K] = i(/* @__PURE__ */ new Set()), [q, C] = i(/* @__PURE__ */ new Set()), [j, h] = i({}), [A, D] = i({}), [c, B] = i([]), u = G(), { keyFactory: o } = R(), F = f( () => o.getEntityQueryKey(e), [o, e] ), { sortBy: b, sortDirection: S } = f(() => { if (!c.length) return {}; const n = c[0]; let t; switch (n.id) { case "name": t = Q.NAME; break; case "createdOn": t = Q.CREATED_ON; break; case "modifiedOn": t = Q.MODIFIED_ON; break; default: return {}; } return { sortBy: t, sortDirection: n.desc ? H.DESC : H.ASC }; }, [c]), T = f(() => ({ parentId: e, includeTypes: U, sortBy: b, sortDirection: S }), [e, b, S]), O = f( () => o.getEntityChildrenQueryKey(T, !1), [o, T] ), p = v(() => { u.removeQueries({ queryKey: O, exact: !0 }), g({}), C(/* @__PURE__ */ new Set()), E({}), h({}), D({}), K(/* @__PURE__ */ new Set()); }, [u, O]), { data: y } = J(e), M = !!y && !q.has(e), { data: m } = z(T, { enabled: M }); return k(() => { p(); }, [p, c]), k(() => { const n = u.getQueryCache().subscribe((t) => { if (t?.type === "updated" && t.action?.type === "invalidate") { const d = Object.keys(a); for (const r of d) { const N = o.getEntityQueryKey(r); if (_({ queryKey: N, exact: !1 }, t.query)) { p(); return; } } } }); return () => { n(); }; }, [u, F, p, a, o]), k(() => { if (y && m && !a[e]) { const n = m.page.map( (r) => ({ entityHeader: r, parentId: e, depth: l ? 1 : 0, isLeaf: !(L(r.type) === s.project || L(r.type) === s.folder) }) ), t = Object.fromEntries( n.map((r) => [r.entityHeader.id, r]) ); g({ [e]: { entityHeader: y, depth: l ? 0 : -1, isLeaf: !1, children: n }, ...t }); const d = m.nextPageToken; h({ [e]: d }), d || C(/* @__PURE__ */ new Set([e])), x && E((r) => ({ ...r, [e]: !0 })); } }, [ y, m, e, x, l, a ]), { // State expanded: P, setExpanded: E, tree: a, setTree: g, loadingIds: w, setLoadingIds: K, loadedChildren: q, setLoadedChildren: C, nextPageTokens: j, setNextPageTokens: h, loadingPageTokens: A, setLoadingPageTokens: D, sorting: c, setSorting: B, // Derived values sortBy: b, sortDirection: S, // Data rootHeader: y, rootChildren: m, // Actions resetTreeData: p }; }; export { xe as useEntityTreeState }; //# sourceMappingURL=useEntityTreeState.js.map