synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
283 lines (282 loc) • 9.85 kB
JavaScript
import { jsx as e, jsxs as o, Fragment as b } from "react/jsx-runtime";
import oe from "../../assets/icons/AddToList.js";
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 "@sage-bionetworks/synapse-types";
import "../../utils/functions/EntityTypeUtils.js";
import { getEndpoint as ne, BackendDestinationEnum as ie } from "../../utils/functions/getEndpoint.js";
import { omitBy as re } from "lodash-es";
import "@sage-bionetworks/synapse-client/util/synapseClientFetch";
import "@tanstack/react-query";
import "../../utils/PermissionLevelToAccessType.js";
import "../../utils/SynapseConstants.js";
import { useRef as ae, useState as c, useEffect as se, useMemo as F } from "react";
import "../../utils/context/SynapseContext.js";
import "use-deep-compare-effect";
import { Alert as E, Link as me, Box as P, Typography as I, Divider as le } from "@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 { SynapseSpinner as ce } from "../LoadingScreen/LoadingScreen.js";
import "@tanstack/query-core";
import de from "lodash-es/isEmpty";
import "lodash-es/isEqual";
import "lodash-es/xorWith";
import "react-router";
import "@sage-bionetworks/synapse-client";
import "../../utils/types/IsType.js";
import { useGetJson as pe, useUpdateViaJson as he } from "../../synapse-queries/entity/useEntity.js";
import { useGetSchemaBinding as fe } from "../../synapse-queries/entity/useEntityBoundSchema.js";
import { useGetSchema as j } from "../../synapse-queries/entity/useSchema.js";
import ue from "@rjsf/validator-ajv8";
import Se from "lodash-es/noop";
import { ConfirmationButtons as ye, ConfirmationDialog as ge } from "../ConfirmationDialog/ConfirmationDialog.js";
import { JsonSchemaForm as ve } from "../JsonSchemaForm/JsonSchemaForm.js";
import { getSchemaIdForConcreteType as be, getPossibleTopLevelPropertiesInObjectSchema as Fe, getJsonSchemaForForm as Ee, getUiSchemaForForm as we, shouldLiveValidate as xe, transformErrors as Ae, customTranslateString as Ce, getFriendlyPropertyName as Te, dropNullValues as De, dropNullishArrayValues as Ve } from "./AnnotationEditorUtils.js";
import { SynapseAnnotationsRJSFObjectField as Oe } from "./field/SynapseAnnotationsRJSFObjectField.js";
import { ObjectFieldTemplate as Pe } from "./template/ObjectFieldTemplate.js";
import Ie from "./template/SynapseAnnotationsWrapIfAdditionalTemplate.js";
import { TextWidget as je } from "./widget/TextWidget.js";
function B(u, n) {
let a = De(u);
return n && (a = Ve(a)), a;
}
function xt(u) {
const {
entityId: n,
schemaId: a,
validationSchema: L,
liveValidate: N,
onSuccess: J = () => {
},
onCancel: S,
formRef: R,
onChange: k = Se,
hideActions: $ = !1
} = u, W = ae(null), w = R ?? W, [x, y] = c(void 0), [A, _] = c(void 0), [q, C] = c(!1), [G, g] = c(!1), { data: m } = pe(
n,
void 0,
// Derived annotations will be precomputed and displayed as placeholders in the form
!1,
{
// Metadata is being edited, so don't refetch
staleTime: 1 / 0,
enabled: !!n,
throwOnError: !0
}
), r = m?.entityMetadata, d = m?.annotations, [p, h] = c(
void 0
);
se(() => {
if (m?.entity) {
const t = m.entity, l = d && Object.keys(d).length > 0 ? t : { ...t, newKey: [""] };
h(l);
}
}, [m?.entity, d]);
const { data: s, isLoading: H } = fe(
n,
{
enabled: !!n,
refetchOnWindowFocus: !1,
throwOnError: !0
}
), { data: U, isLoading: M } = j(a ?? s?.jsonSchemaVersionInfo.$id ?? "", {
enabled: !!a || !!s,
throwOnError: !0
}), v = L || U, T = be(
r?.concreteType ?? ""
), { data: f, isLoading: z } = j(T ?? "", {
enabled: !!T,
throwOnError: !0
}), D = F(
() => Fe(f ?? {}),
[f]
), K = F(
() => Ee(v, f),
[v, f]
), Q = F(
() => we(D),
[D]
), X = H || M || z, { mutate: Y, isPending: Z } = he({
onSuccess: () => {
J();
},
onError: (t) => {
_(t), C(!0);
}
});
function V() {
Y({
...B(p, !0),
...r
});
}
const O = N ?? xe(d, v), ee = r && de(
re(
p,
(t, i) => Object.keys(r).find((l) => l === i)
)
), te = s === null && ee;
return /* @__PURE__ */ e("div", { className: "JsonSchemaFormContainer", children: X ? /* @__PURE__ */ e("div", { className: "LoadingPlaceholder", children: /* @__PURE__ */ e(ce, { size: 30 }) }) : /* @__PURE__ */ o(b, { children: [
r && s && /* @__PURE__ */ o(E, { severity: "info", sx: { mb: 2 }, children: [
/* @__PURE__ */ e("b", { children: r.name }),
" requires scientific annotations specified by ",
/* @__PURE__ */ e("b", { children: s.jsonSchemaVersionInfo.$id }),
". ",
/* @__PURE__ */ e("b", { children: /* @__PURE__ */ e(
me,
{
href: `${ne(
ie.REPO_ENDPOINT
)}/repo/v1/schema/type/registered/${s.jsonSchemaVersionInfo.$id}`,
target: "_blank",
rel: "noopener noreferrer",
children: "View required schema (JSON)"
}
) })
] }),
te && /* @__PURE__ */ e(E, { severity: "info", children: /* @__PURE__ */ o(
P,
{
sx: {
display: "flex",
alignItems: "center",
gap: 0.5
},
children: [
/* @__PURE__ */ o(I, { variant: "smallText1", children: [
/* @__PURE__ */ e("b", { children: r.name }),
" has no annotations. Click the",
" "
] }),
/* @__PURE__ */ e(oe, {}),
/* @__PURE__ */ e(I, { variant: "smallText1", children: "button to annotate." })
]
}
) }),
/* @__PURE__ */ o(
ve,
{
validator: ue,
liveValidate: O,
noHtml5Validate: !0,
formRef: w,
disabled: Z,
translateString: Ce,
formContext: {
showDerivedAnnotationPlaceholder: !0,
descriptionVariant: "expand",
descriptionFormat: "table",
allowFreeSoloEnum: !0
},
experimental_defaultFormStateBehavior: {
emptyObjectFields: "skipDefaults"
},
fields: {
ObjectField: Oe
},
templates: {
ObjectFieldTemplate: Pe,
WrapIfAdditionalTemplate: Ie
},
widgets: {
TextWidget: je
},
schema: K,
uiSchema: Q,
transformErrors: Ae,
formData: p,
onChange: ({ formData: t }) => {
k(t), h(t), y(void 0);
},
onBlur: () => {
h(
// Clean the formData onBlur to remove null values that we will need to strip before submission
// This will ensure that the user gets accurate validation information since the data will match what the backend will receive
B(
p,
// Don't remove null values in arrays--the fields will disappear, which the user probably does not want
!1
)
);
},
onSubmit: ({ formData: t, errors: i }, l) => {
l.preventDefault(), i && i.length > 0 && y(i), C(!1), h(t), V();
},
onError: (t) => {
y(t), (x || O) && n && g(!0);
},
children: [
A && q && /* @__PURE__ */ o(E, { severity: "error", sx: { my: 2 }, children: [
"Annotations could not be updated: ",
A.reason
] }),
!$ && /* @__PURE__ */ o(b, { children: [
/* @__PURE__ */ e(le, { sx: { my: 2 } }),
/* @__PURE__ */ e(
P,
{
sx: {
display: "flex",
justifyContent: "space-between",
gridRowStart: 5
},
children: /* @__PURE__ */ e(
ye,
{
hasCancelButton: S !== void 0,
onCancel: () => {
S && S();
},
onConfirm: () => {
w.current.formElement.current.requestSubmit();
},
confirmButtonProps: {
children: n ? "Save" : "Validate"
}
}
)
}
)
] })
]
}
),
G && /* @__PURE__ */ e(
ge,
{
open: !0,
onConfirm: () => {
V(), g(!1);
},
onCancel: () => {
g(!1);
},
title: "Update Annotations",
content: /* @__PURE__ */ o(b, { children: [
/* @__PURE__ */ e("div", { children: "The following errors exist with the annotations you entered:" }),
/* @__PURE__ */ e("div", { children: /* @__PURE__ */ e("ul", { children: (x ?? []).map(
(t, i) => /* @__PURE__ */ o("li", { children: [
/* @__PURE__ */ e("b", { children: `${Te(t)}: ` }),
" ",
`${t.message}`
] }, i)
) }) }),
/* @__PURE__ */ e("div", { children: "Are you sure you want to save the invalid annotations?" })
] }),
confirmButtonProps: {
children: "Save"
}
}
)
] }) });
}
export {
xt as SchemaDrivenAnnotationEditor
};
//# sourceMappingURL=SchemaDrivenAnnotationEditor.js.map