sanity-plugin-computed-field
Version:
Sanity plugin that computes a field's value based on other fields or relationships with its document or external data. In other words, this field memoizes a value in a document using GROQ to lookup and custom javascript to compute the value from those GRO
139 lines (138 loc) • 6.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: !0 });
var jsxRuntime = require("react/jsx-runtime"), ui = require("@sanity/ui"), React = require("react"), sanity = require("sanity"), theme$1 = require("@sanity/ui/theme");
function _interopDefaultCompat(e) {
return e && typeof e == "object" && "default" in e ? e : { default: e };
}
var React__default = /* @__PURE__ */ _interopDefaultCompat(React);
const useQueryReducer = ({
reduceQueryResult,
documentQuerySelection,
handleValueChange,
value
}) => {
const [loading, setLoading] = React__default.default.useState(!1), client = sanity.useClient(), docId = sanity.useFormValue(["_id"]), _type = sanity.useFormValue(["_type"]), reducer = React__default.default.useCallback(
(queryResult) => reduceQueryResult(queryResult),
[reduceQueryResult]
);
return {
handleRegenerateValue: React__default.default.useCallback(async () => {
const query = `*[_type == '${_type}' && _id == '${docId}' || _id == '${docId.replace(
"drafts.",
""
)}'] {
_id,
${documentQuerySelection}
}`;
setLoading(!0);
const items = await client.fetch(query), draft = items.find(({ _id }) => _id.includes("drafts")), published = items.find(({ _id }) => !_id.includes("drafts")), newValue = await Promise.resolve(reducer({ draft, published }));
newValue !== value && handleValueChange(newValue), setLoading(!1);
}, [_type, docId, documentQuerySelection, client, reducer, value, handleValueChange]),
isRegenerating: loading
};
}, theme = theme$1.buildTheme(), InputControls = ({ buttonText, onClickButton, isLoading }) => /* @__PURE__ */ jsxRuntime.jsxs(ui.Inline, { space: [2, 3], children: [
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: onClickButton, title: buttonText, text: buttonText, mode: "ghost" }),
isLoading && /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, {})
] }), ComputedBooleanInput = (props) => {
const { schemaType, onChange, value } = props, { options } = schemaType, { reduceQueryResult, documentQuerySelection, buttonText = "Regenerate" } = options, handleValueChange = React__default.default.useCallback((val) => onChange(sanity.set(val)), [onChange]), { isRegenerating, handleRegenerateValue } = useQueryReducer({
reduceQueryResult,
documentQuerySelection,
handleValueChange,
value
});
return /* @__PURE__ */ jsxRuntime.jsx(ui.ThemeProvider, { theme, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Flex, { direction: "row", align: "center", children: [
props.renderDefault(props),
/* @__PURE__ */ jsxRuntime.jsx(ui.Card, { marginLeft: [2, 3], children: /* @__PURE__ */ jsxRuntime.jsx(
InputControls,
{
buttonText,
isLoading: isRegenerating,
onClickButton: handleRegenerateValue
}
) })
] }) });
}, ComputedNumberInput = (props) => {
const { schemaType, onChange, value } = props, { options } = schemaType, { reduceQueryResult, documentQuerySelection, buttonText = "Regenerate" } = options, handleValueChange = React__default.default.useCallback((val) => onChange(sanity.set(val)), [onChange]), { isRegenerating, handleRegenerateValue } = useQueryReducer({
reduceQueryResult,
documentQuerySelection,
handleValueChange,
value
});
return /* @__PURE__ */ jsxRuntime.jsx(ui.ThemeProvider, { theme, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Inline, { space: [2], children: [
props.renderDefault(props),
/* @__PURE__ */ jsxRuntime.jsx(
InputControls,
{
isLoading: isRegenerating,
onClickButton: handleRegenerateValue,
buttonText
}
)
] }) });
}, ComputedTextInput = (props) => {
var _a;
const { schemaType, onChange, value } = props, { options, type } = schemaType, { reduceQueryResult, documentQuerySelection, buttonText = "Regenerate" } = options, handleValueChange = React__default.default.useCallback((val) => onChange(sanity.set(val)), [onChange]), { isRegenerating, handleRegenerateValue } = useQueryReducer({
reduceQueryResult,
documentQuerySelection,
handleValueChange,
value
}), isString = ((_a = type == null ? void 0 : type.type) == null ? void 0 : _a.name) === "string";
return /* @__PURE__ */ jsxRuntime.jsx(ui.ThemeProvider, { theme, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Flex, { direction: isString ? "row" : "column", children: [
/* @__PURE__ */ jsxRuntime.jsx(ui.Container, { flex: 1, children: props.renderDefault(props) }),
/* @__PURE__ */ jsxRuntime.jsx(ui.Card, { paddingLeft: isString ? 2 : 0, paddingTop: isString ? 0 : 2, children: /* @__PURE__ */ jsxRuntime.jsx(
InputControls,
{
isLoading: isRegenerating,
onClickButton: handleRegenerateValue,
buttonText
}
) })
] }) });
}, computedBooleanSchema = sanity.defineType({
title: "Computed Boolean",
type: "boolean",
name: "computedBoolean",
components: {
input: ComputedBooleanInput
}
}), computedNumberSchema = sanity.defineType({
title: "Computed Number",
type: "number",
name: "computedNumber",
components: {
input: ComputedNumberInput
}
}), computedStringSchema = sanity.defineType({
title: "Computed String",
type: "string",
name: "computedString",
components: {
input: ComputedTextInput
}
}), computedTextSchema = sanity.defineType({
title: "Computed Text",
type: "text",
name: "computedText",
components: {
input: ComputedTextInput
}
}), sanityComputedField = sanity.definePlugin((config = {}) => ({
name: "sanity-plugin-computed-field",
schema: {
types: [
computedBooleanSchema,
computedStringSchema,
computedTextSchema,
computedNumberSchema
]
}
}));
exports.ComputedBooleanInput = ComputedBooleanInput;
exports.ComputedNumberInput = ComputedNumberInput;
exports.ComputedTextInput = ComputedTextInput;
exports.computedBooleanSchema = computedBooleanSchema;
exports.computedNumberSchema = computedNumberSchema;
exports.computedStringSchema = computedStringSchema;
exports.computedTextSchema = computedTextSchema;
exports.sanityComputedField = sanityComputedField;
//# sourceMappingURL=index.js.map