@xyo-network/react-archivist
Version:
Common React library for all XYO projects that use React
429 lines (411 loc) • 16.5 kB
JavaScript
// src/components/Card/Card.tsx
import { Card } from "@mui/material";
import { ModuleCardActions } from "@xyo-network/react-module";
// src/components/Card/CardContent.tsx
import { useAsyncEffect as useAsyncEffect2 } from "@xylabs/react-async-effect";
import { FlexGrowRow } from "@xylabs/react-flexbox";
import { ModuleWrapper } from "@xyo-network/module-wrapper";
import { ModuleCardContent } from "@xyo-network/react-module";
import { useWallet } from "@xyo-network/react-wallet";
import { useState as useState3 } from "react";
// src/components/Card/components/ArchivistParent.tsx
import { ListItem, Typography } from "@mui/material";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var ArchivistParent = ({ archivistType, parentArchivists }) => /* @__PURE__ */ jsx(Fragment, { children: parentArchivists?.map((address) => {
return /* @__PURE__ */ jsx(ListItem, { children: /* @__PURE__ */ jsxs(Typography, { variant: "subtitle2", children: [
archivistType,
":",
" ",
/* @__PURE__ */ jsx(Typography, { variant: "caption", sx: { ml: 0.5 }, children: address })
] }) }, address);
}) });
// src/components/Card/components/ArchivistParents.tsx
import { ArrowRightRounded as ArrowRightRoundedIcon } from "@mui/icons-material";
import {
Collapse,
IconButton,
List,
Paper,
Typography as Typography2
} from "@mui/material";
import { FlexCol } from "@xylabs/react-flexbox";
import { useState } from "react";
import { Fragment as Fragment2, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
var ArchivistParents = ({ config, ...props }) => {
const [parentArchivistCollapse, setParentArchivistCollapse] = useState(false);
const {
commit,
read,
write
} = config?.parents ?? {};
return /* @__PURE__ */ jsx2(Fragment2, { children: commit || read || write ? /* @__PURE__ */ jsxs2(FlexCol, { alignItems: "start", ...props, children: [
/* @__PURE__ */ jsxs2("span", { onClick: () => setParentArchivistCollapse(!parentArchivistCollapse), children: [
/* @__PURE__ */ jsx2(
Typography2,
{
variant: "subtitle2",
sx: { cursor: "pointer", display: "inline-block" },
children: "Parents"
}
),
/* @__PURE__ */ jsx2(IconButton, { size: "small", children: /* @__PURE__ */ jsx2(ArrowRightRoundedIcon, { sx: { rotate: parentArchivistCollapse ? "90deg" : "0deg", transition: "all .25s" } }) })
] }),
/* @__PURE__ */ jsx2(Collapse, { in: parentArchivistCollapse, children: /* @__PURE__ */ jsx2(Paper, { elevation: 2, children: /* @__PURE__ */ jsxs2(List, { children: [
/* @__PURE__ */ jsx2(ArchivistParent, { archivistType: "Commit", parentArchivists: commit }),
/* @__PURE__ */ jsx2(ArchivistParent, { archivistType: "Read", parentArchivists: read }),
/* @__PURE__ */ jsx2(ArchivistParent, { archivistType: "Write", parentArchivists: write })
] }) }) })
] }) : null });
};
// src/components/Card/components/Stats/ArchivistStats.tsx
import { Badge, Tooltip } from "@mui/material";
import { FlexRow } from "@xylabs/react-flexbox";
import { BsFileEarmarkCode } from "react-icons/bs";
import {
VscOrganization,
VscSymbolMethod,
VscSymbolNamespace
} from "react-icons/vsc";
import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
var ArchivistStats = ({
addresses,
boundWitnesses,
payloads,
schemas
}) => {
return /* @__PURE__ */ jsxs3(FlexRow, { gap: 2, mr: 0.5, children: [
payloads?.length ? /* @__PURE__ */ jsx3(Tooltip, { title: "Payloads", children: /* @__PURE__ */ jsx3(Badge, { badgeContent: payloads?.length, color: "primary", children: /* @__PURE__ */ jsx3(VscSymbolNamespace, { size: 20 }) }) }) : null,
boundWitnesses?.length ? /* @__PURE__ */ jsx3(Tooltip, { title: "Blocks", children: /* @__PURE__ */ jsx3(Badge, { badgeContent: boundWitnesses?.length, color: "primary", children: /* @__PURE__ */ jsx3(VscSymbolMethod, { size: 20 }) }) }) : null,
Object.entries(schemas ?? {}).length > 0 ? /* @__PURE__ */ jsx3(Tooltip, { title: "Schemas", children: /* @__PURE__ */ jsx3(Badge, { badgeContent: Object.entries(schemas ?? {}).length, color: "primary", children: /* @__PURE__ */ jsx3(BsFileEarmarkCode, { size: 20 }) }) }) : null,
Object.entries(addresses ?? {}).length > 0 ? /* @__PURE__ */ jsx3(Tooltip, { title: "Addresses", children: /* @__PURE__ */ jsx3(Badge, { badgeContent: Object.entries(addresses ?? {}).length, color: "primary", children: /* @__PURE__ */ jsx3(VscOrganization, { size: 20 }) }) }) : null
] });
};
// src/components/Card/components/Stats/MemoryArchivistStats.tsx
import { useAsyncEffect } from "@xylabs/react-async-effect";
import { ArchivistAllQuerySchema } from "@xyo-network/archivist-model";
import { asBoundWitness, BoundWitnessSchema } from "@xyo-network/boundwitness-model";
import {
useCallback,
useEffect,
useMemo,
useState as useState2
} from "react";
import { jsx as jsx4 } from "react/jsx-runtime";
var MemoryArchivistsStats = ({ archivist }) => {
const [all, setAll] = useState2();
const getAll = useCallback(async (archivist2) => {
const all2 = await archivist2?.all?.();
setAll(all2);
}, []);
useEffect(() => {
const listeners = [];
if (archivist?.queries.includes(ArchivistAllQuerySchema)) {
const insertListener = archivist.on("inserted", async () => {
await getAll(archivist);
});
listeners.push(insertListener);
const clearListener = archivist.on("cleared", async () => {
await getAll(archivist);
});
listeners.push(clearListener);
}
return () => {
for (const listener of listeners) listener?.();
};
}, [archivist, getAll]);
useAsyncEffect(
async () => {
if (archivist?.queries.includes(ArchivistAllQuerySchema)) {
await getAll(archivist);
} else {
setAll(null);
}
},
[archivist, getAll]
);
const payloads = useMemo(() => all === null ? [] : all?.filter((payload) => payload.schema !== BoundWitnessSchema), [all]);
const boundWitnesses = useMemo(() => all === null ? [] : all?.filter((payload) => payload.schema === BoundWitnessSchema), [all]);
const addresses = useMemo(() => {
const addressCounts = {};
if (all) {
for (const payload of all) {
const w = asBoundWitness(payload);
if (w?.addresses) {
for (const address of w.addresses) {
addressCounts[address] = (addressCounts[address] ?? 0) + 1;
}
}
}
}
return addressCounts;
}, [all]);
const schemas = useMemo(() => {
const schemaCounts = {};
if (all) {
for (const payload of all) {
schemaCounts[payload.schema] = (schemaCounts[payload.schema] ?? 0) + 1;
}
}
return schemaCounts;
}, [all]);
return /* @__PURE__ */ jsx4(ArchivistStats, { addresses, boundWitnesses, payloads, schemas });
};
// src/components/Card/CardContent.tsx
import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
var ArchivistCardContent = ({
children,
mod,
...props
}) => {
const [config, setConfig] = useState3();
const [wallet] = useWallet();
useAsyncEffect2(
async (mounted) => {
if (wallet && mod) {
const wrapper = ModuleWrapper.wrap(mod, wallet);
const payloads = await wrapper?.state();
if (mounted()) {
setConfig(payloads?.[0]);
}
} else {
setConfig(void 0);
}
},
[mod, wallet]
);
return /* @__PURE__ */ jsx5(ModuleCardContent, { mod, ...props, children: /* @__PURE__ */ jsxs4(FlexGrowRow, { flexWrap: "wrap", justifyContent: "start", gap: 2, children: [
children,
/* @__PURE__ */ jsx5(ArchivistParents, { config })
] }) });
};
// src/components/Card/CardHeader.tsx
import { MemoryArchivistConfigSchema } from "@xyo-network/archivist-memory";
import { ModuleCardHeader } from "@xyo-network/react-module";
import { useMemo as useMemo2 } from "react";
import { Fragment as Fragment3, jsx as jsx6 } from "react/jsx-runtime";
var ArchivistStats2 = (archivist) => {
switch (archivist?.config.schema) {
case MemoryArchivistConfigSchema: {
return /* @__PURE__ */ jsx6(MemoryArchivistsStats, { archivist });
}
default: {
return /* @__PURE__ */ jsx6(Fragment3, {});
}
}
};
var ArchivistCardHeader = ({
title,
mod,
...props
}) => {
const Stats = useMemo2(() => ArchivistStats2(mod), [mod]);
return /* @__PURE__ */ jsx6(ModuleCardHeader, { mod, title: title ?? mod?.config.name ?? "Archivist", action: Stats, ...props });
};
// src/components/Card/Card.tsx
import { jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
var ArchivistCard = ({
children,
mod,
...props
}) => {
return /* @__PURE__ */ jsxs5(Card, { ...props, children: [
/* @__PURE__ */ jsx7(ArchivistCardHeader, { mod }),
/* @__PURE__ */ jsx7(ArchivistCardContent, { mod }),
children,
/* @__PURE__ */ jsx7(ModuleCardActions, { mod })
] });
};
// src/components/Details/Details.tsx
import { ButtonGroup, Typography as Typography3 } from "@mui/material";
import { useAsyncEffect as useAsyncEffect3 } from "@xylabs/react-async-effect";
import { ButtonEx } from "@xylabs/react-button";
import { FlexCol as FlexCol2 } from "@xylabs/react-flexbox";
import { ArchivistClearQuerySchema, ArchivistCommitQuerySchema } from "@xyo-network/archivist-model";
import { QueryBoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
import { useState as useState6 } from "react";
// src/hooks/node/useArchivistFromNode.tsx
import { asArchivistInstance } from "@xyo-network/archivist-model";
import { useModuleFromNode } from "@xyo-network/react-node";
var useArchivistFromNode = (nameOrAddressOrInstance, config) => {
const [mod, error] = useModuleFromNode(nameOrAddressOrInstance, config);
const instance = asArchivistInstance(mod);
if (mod && !instance) {
const error2 = new Error(`Resolved module is not a ArchivistInstance [${mod.config?.schema}:${mod.config?.name}:${mod.address}]`);
console.error(error2.message);
return [void 0, error2];
}
return [instance, error];
};
// src/hooks/node/useArchivistsFromNode.tsx
import { isArchivistInstance } from "@xyo-network/archivist-model";
import { useModulesFromNode } from "@xyo-network/react-node";
var useArchivistsFromNode = (ids, config) => {
const [modules, error] = useModulesFromNode(ids, config);
if (error) {
return [null, error];
}
return modules ? [
// eslint-disable-next-line unicorn/no-array-reduce
modules.reduce((prev, mod) => {
if (isArchivistInstance(mod)) {
prev.push(mod);
}
return prev;
}, []),
void 0
] : [modules, error];
};
// src/hooks/node/useWeakArchivistFromNode.tsx
import { isArchivistInstance as isArchivistInstance2 } from "@xyo-network/archivist-model";
import { useWeakModuleFromNode } from "@xyo-network/react-node";
var useWeakArchivistFromNode = (nameOrAddressOrInstance, config) => {
return useWeakModuleFromNode(nameOrAddressOrInstance, { identity: isArchivistInstance2, ...config });
};
// src/hooks/node/useWeakArchivistsFromNode.tsx
import { exists } from "@xylabs/exists";
import { asArchivistInstance as asArchivistInstance2 } from "@xyo-network/archivist-model";
import { useWeakModulesFromNode } from "@xyo-network/react-node";
var useWeakArchivistsFromNode = (ids, config) => {
const [modules, error] = useWeakModulesFromNode(ids, config);
if (error) {
return [null, error];
}
return [
modules?.map((mod) => {
const instance = asArchivistInstance2(mod?.deref());
if (instance) {
return new WeakRef(instance);
}
}).filter(exists) ?? [],
void 0
];
};
// src/hooks/queries/useArchivistAll.tsx
import { usePromise } from "@xylabs/react-promise";
import { useRefresh } from "@xyo-network/react-module";
import { useState as useState4 } from "react";
var useArchivistAll = (archivist) => {
const [error, setError] = useState4();
const [enabled, refresh] = useRefresh();
const payloads = usePromise(async () => {
try {
if (enabled) {
return await archivist?.all?.();
}
} catch (ex) {
const error2 = ex;
setError(error2);
}
}, [archivist, enabled]);
return [payloads, error, refresh];
};
// src/hooks/queries/useArchivistGet.tsx
import { usePromise as usePromise2 } from "@xylabs/react-promise";
import { useRefresh as useRefresh2 } from "@xyo-network/react-module";
var useArchivistGet = (archivist, hashes) => {
const [enabled, refresh] = useRefresh2();
const [payloads, error] = usePromise2(async () => {
if (enabled && archivist && hashes) {
return await archivist.get(hashes);
}
}, [archivist, hashes, enabled]);
return [payloads, error, refresh];
};
// src/hooks/queries/useWeakArchivistAll.tsx
import { usePromise as usePromise3 } from "@xylabs/react-promise";
import { useRefresh as useRefresh3 } from "@xyo-network/react-module";
import { useState as useState5 } from "react";
var useWeakArchivistAll = (archivist) => {
const [error, setError] = useState5();
const [enabled, refresh] = useRefresh3();
const payloads = usePromise3(async () => {
try {
if (enabled) {
return await archivist?.deref()?.all?.();
}
} catch (ex) {
const error2 = ex;
setError(error2);
}
}, [archivist, enabled]);
return [payloads, error, refresh];
};
// src/hooks/queries/useWeakArchivistGet.tsx
import { usePromise as usePromise4 } from "@xylabs/react-promise";
import { useRefresh as useRefresh4 } from "@xyo-network/react-module";
var useWeakArchivistGet = (archivist, hashes) => {
const [enabled, refresh] = useRefresh4();
const [payloads, error] = usePromise4(async () => {
const archivistInstance = archivist?.deref();
if (enabled && archivistInstance && hashes) {
return await archivistInstance.get(hashes);
}
}, [archivist, hashes, enabled]);
return [payloads, error, refresh];
};
// src/components/Details/Details.tsx
import { jsx as jsx8, jsxs as jsxs6 } from "react/jsx-runtime";
var testQueryCommit = { schema: ArchivistCommitQuerySchema };
var testQueryCommitBoundWitnessBuilder = new QueryBoundWitnessBuilder().query(testQueryCommit);
var testQueryClear = { schema: ArchivistClearQuerySchema };
var testQueryClearBoundWitnessBuilder = new QueryBoundWitnessBuilder().query(testQueryClear);
var ArchivistDetails = ({ address, ...props }) => {
const [archivist] = useWeakArchivistFromNode(address);
const [payloads, setPayloads] = useState6();
const [refresh, setRefresh] = useState6(0);
const [queryableCommit, setQueryableCommit] = useState6(false);
const [queryableClear, setQueryableClear] = useState6(false);
useAsyncEffect3(
async () => {
const instance = archivist?.deref();
if (instance) {
const [commitBW] = await testQueryCommitBoundWitnessBuilder.build();
const [clearBW] = await testQueryClearBoundWitnessBuilder.build();
setQueryableCommit(await instance?.queryable(commitBW, [testQueryCommit]));
setQueryableClear(await instance?.queryable(clearBW, [testQueryClear]));
}
},
[archivist]
);
useAsyncEffect3(
async (mounted) => {
const payloads2 = await archivist?.deref()?.all?.();
if (mounted()) {
setPayloads(payloads2);
}
},
[archivist, refresh]
);
return /* @__PURE__ */ jsxs6(FlexCol2, { ...props, children: [
/* @__PURE__ */ jsx8(Typography3, { children: `Payloads: ${payloads ? payloads.length : "-"}` }),
/* @__PURE__ */ jsxs6(ButtonGroup, { children: [
/* @__PURE__ */ jsx8(ButtonEx, { disabled: payloads?.length === 0 || !archivist || !queryableCommit, onClick: () => archivist?.deref()?.commit?.(), children: "Commit" }),
/* @__PURE__ */ jsx8(ButtonEx, { disabled: !archivist || !queryableClear, onClick: () => archivist?.deref()?.clear?.(), children: "Clear" }),
/* @__PURE__ */ jsx8(
ButtonEx,
{
disabled: !archivist,
onClick: () => {
setRefresh(refresh + 1);
},
children: "Refresh"
}
)
] })
] });
};
export {
ArchivistCard,
ArchivistCardContent,
ArchivistCardHeader,
ArchivistDetails,
useArchivistAll,
useArchivistFromNode,
useArchivistGet,
useArchivistsFromNode,
useWeakArchivistAll,
useWeakArchivistFromNode,
useWeakArchivistGet,
useWeakArchivistsFromNode
};
//# sourceMappingURL=index.mjs.map