synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
70 lines (69 loc) • 2.08 kB
JavaScript
import { useMutation as p } from "@tanstack/react-query";
import "../../context/FullContextProvider.js";
import { useSynapseContext as F } from "../../context/SynapseContext.js";
import "../../context/DocumentMetadataContext.js";
import { getFileEntityIdWithSameName as E } from "./getFileEntityIdWithSameName.js";
import { useCreatePathsAndGetParentId as g } from "./useCreatePathsAndGetParentId.js";
function b(a) {
const { synapseClient: l } = F(), { mutateAsync: u } = g();
return p({
...a,
mutationFn: async (i) => {
const f = i.filter(
(t) => "existingEntityId" in t
), o = [];
for (const t of i)
try {
if ("rootContainerId" in t) {
const { file: e, rootContainerId: d } = t;
o.push(
await u({
file: e,
rootContainerId: d
})
);
}
} catch (e) {
throw new Error(
`Unable to create target folder structure for file ${t.file.name}${Object.hasOwn(e, "message") ? `: ${e.message}` : null}`,
{ cause: e }
);
}
const n = await Promise.allSettled(
o.map(
(t) => E(
t.file.name,
t.parentId,
l,
"The file could not be uploaded."
).then((e) => ({
...t,
existingEntityId: e
}))
)
), r = n.filter((t) => t.status === "rejected");
if (r.length > 0)
throw new Error(
`Files could not be uploaded:
${r.map((t) => t.reason.message).join(`
`)}`
);
const s = n.filter((t) => t.status === "fulfilled").map((t) => t.value), c = [
...s.filter(
(t) => t.existingEntityId == null
),
...f
], m = s.filter(
(t) => t.existingEntityId != null
);
return {
filesReadyForUpload: c,
filesToPromptForNewVersion: m
};
}
});
}
export {
b as usePrepareFileEntityUpload
};
//# sourceMappingURL=usePrepareFileEntityUpload.js.map