Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

refactor: define resource control explicitly (#3360)

Bogdan Chadkin8bf04f66a9b2
diff --git a/apps/builder/app/builder/features/settings-panel/resource-panel.tsx b/apps/builder/app/builder/features/settings-panel/resource-panel.tsx
index 1e7a1c5dfb..a133f2ae30 100644
--- a/apps/builder/app/builder/features/settings-panel/resource-panel.tsx
+++ b/apps/builder/app/builder/features/settings-panel/resource-panel.tsx
@@ -670,6 +670,7 @@ export const SystemResourceForm = forwardRef<
const newResource: Resource = {
id: resource?.id ?? nanoid(),
name: nameField.value,
+ control: "system",
url: localResource.value,
method,
headers: [],
diff --git a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx b/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
index 32674b74a8..d1fe89ad9f 100644
--- a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variable-popover.tsx
@@ -22,6 +22,7 @@ import {
FloatingPanelPopoverContent,
FloatingPanelPopoverTitle,
FloatingPanelPopoverTrigger,
+ Grid,
InputErrorsTooltip,
InputField,
Label,
@@ -33,12 +34,7 @@ import {
Tooltip,
theme,
} from "@webstudio-is/design-system";
-import {
- executeExpression,
- isLocalResource,
- transpileExpression,
-} from "@webstudio-is/sdk";
-import type { DataSource } from "@webstudio-is/sdk";
+import { type DataSource, transpileExpression } from "@webstudio-is/sdk";
import {
ExpressionEditor,
formatValue,
@@ -115,6 +111,87 @@ type VariableType =
| "system-resource"
| "parameter";
+const TypeField = ({
+ value,
+ onChange,
+}: {
+ value: VariableType;
+ onChange: (value: VariableType) => void;
+}) => {
+ const { allowDynamicData } = useStore($userPlanFeatures);
+ const optionsList: Array<{
+ value: VariableType;
+ disabled?: boolean;
+ label: ReactNode;
+ description: string;
+ }> = [
+ {
+ value: "string",
+ label: "String",
+ description: "Any alphanumeric text.",
+ },
+ {
+ value: "number",
+ label: "Number",
+ description: "Any number, can be used in math expressions.",
+ },
+ {
+ value: "boolean",
+ label: "Boolean",
+ description: "A boolean is a true/false switch.",
+ },
+ {
+ value: "json",
+ label: "JSON",
+ description: "Any JSON value",
+ },
+ {
+ value: "resource",
+ disabled: allowDynamicData === false,
+ label: (
+ <Flex direction="row" gap="2" align="center">
+ Resource
+ {allowDynamicData === false && <ProBadge>Pro</ProBadge>}
+ </Flex>
+ ),
+ description:
+ "A Resource is a configuration for secure data fetching. You can safely use secrets in any field.",
+ },
+ {
+ value: "system-resource",
+ disabled: allowDynamicData === false,
+ label: (
+ <Flex direction="row" gap="2" align="center">
+ System Resource
+ {allowDynamicData === false && <ProBadge>Pro</ProBadge>}
+ </Flex>
+ ),
+ description: "A System Resource is a configuration for Webstudio data.",
+ },
+ ];
+ const options = new Map(optionsList.map((option) => [option.value, option]));
+
+ return (
+ <Grid gap="1">
+ <Label>Type</Label>
+ <Select
+ options={Array.from(options.keys())}
+ getLabel={(option: VariableType) => options.get(option)?.label}
+ getItemProps={(option) => ({
+ disabled: options.get(option)?.disabled,
+ })}
+ getDescription={(option) => (
+ <Box css={{ width: theme.spacing[25] }}>
+ {options.get(option)?.description}
+ </Box>
+ )}
+ value={value}
+ onChange={onChange}
+ />
+ </Grid>
+ );
+};
+
type PanelApi = ComposedFields & {
save: () => void;
};
@@ -363,7 +440,6 @@ const VariablePanel = forwardRef<
variable?: DataSource;
}
>(({ variable }, ref) => {
- const { allowDynamicData } = useStore($userPlanFeatures);
const resources = useStore($resources);
const nameField = useField({
@@ -390,19 +466,16 @@ const VariablePanel = forwardRef<
);
const [variableType, setVariableType] = useState<VariableType>(() => {
- if (
- variable?.type === "resource" &&
- isLocalResource(
- String(executeExpression(resources.get(variable.resourceId)?.url) ?? "")
- )
- ) {
- return "system-resource";
+ if (variable?.type === "resource") {
+ const resource = resources.get(variable.resourceId);
+ if (resource?.control === "system") {
+ return "system-resource";
+ }
+ return "resource";
}
-
- if (variable?.type === "parameter" || variable?.type === "resource") {
+ if (variable?.type === "parameter") {
return variable.type;
}
-
if (variable?.type === "variable") {
const type = variable.value.type;
if (type === "string" || type === "number" || type === "boolean") {
@@ -413,72 +486,6 @@ const VariablePanel = forwardRef<
return "string";
});
- const typeOptions: Map<
- VariableType,
- { label: ReactNode; description: string }
- > = new Map([
- ["string", { label: "String", description: "Any alphanumeric text." }],
- [
- "number",
- {
- label: "Number",
- description: "Any number, can be used in math expressions.",
- },
- ],
- [
- "boolean",
- { label: "Boolean", description: "A boolean is a true/false switch." },
- ],
- ["json", { label: "JSON", description: "Any JSON value" }],
- [
- "resource",
- {
- label: (
- <Flex direction="row" gap="2" align="center">
- Resource
- {allowDynamicData === false && <ProBadge>Pro</ProBadge>}
- </Flex>
- ),
- description:
- "A Resource is a configuration for secure data fetching. You can safely use secrets in any field.",
- },
- ],
- [
- "system-resource",
- {
- label: (
- <Flex direction="row" gap="2" align="center">
- System Resource
- {allowDynamicData === false && <ProBadge>Pro</ProBadge>}
- </Flex>
- ),
- description: "A System Resource is a configuration for Webstudio data.",
- },
- ],
- ]);
-
- const typeFieldElement = (
- <Flex direction="column" gap="1">
- <Label>Type</Label>
- <Select
- options={Array.from(typeOptions.keys())}
- getLabel={(option: VariableType) => typeOptions.get(option)?.label}
- getItemProps={(option) => ({
- disabled: option === "resource" && allowDynamicData === false,
- })}
- getDescription={(option) => {
- return (
- <Box css={{ width: theme.spacing[25] }}>
- {typeOptions.get(option)?.description}
- </Box>
- );
- }}
- value={variableType}
- onChange={setVariableType}
- />
- </Flex>
- );
-
if (variableType === "parameter") {
return (
<>
@@ -491,7 +498,7 @@ const VariablePanel = forwardRef<
return (
<>
{nameFieldElement}
- {typeFieldElement}
+ <TypeField value={variableType} onChange={setVariableType} />
<StringForm ref={ref} variable={variable} nameField={nameField} />
</>
);
@@ -500,7 +507,7 @@ const VariablePanel = forwardRef<
return (
<>
{nameFieldElement}
- {typeFieldElement}
+ <TypeField value={variableType} onChange={setVariableType} />
<NumberForm ref={ref} variable={variable} nameField={nameField} />
</>
);
@@ -509,7 +516,7 @@ const VariablePanel = forwardRef<
return (
<>
{nameFieldElement}
- {typeFieldElement}
+ <TypeField value={variableType} onChange={setVariableType} />
<BooleanForm ref={ref} variable={variable} nameField={nameField} />
</>
);
@@ -518,7 +525,7 @@ const VariablePanel = forwardRef<
return (
<>
{nameFieldElement}
- {typeFieldElement}
+ <TypeField value={variableType} onChange={setVariableType} />
<JsonForm ref={ref} variable={variable} nameField={nameField} />
</>
);
@@ -528,7 +535,7 @@ const VariablePanel = forwardRef<
return (
<>
{nameFieldElement}
- {typeFieldElement}
+ <TypeField value={variableType} onChange={setVariableType} />
<ResourceForm ref={ref} variable={variable} nameField={nameField} />
</>
);
@@ -538,7 +545,7 @@ const VariablePanel = forwardRef<
return (
<>
{nameFieldElement}
- {typeFieldElement}
+ <TypeField value={variableType} onChange={setVariableType} />
<SystemResourceForm
ref={ref}
variable={variable}
@@ -647,13 +654,9 @@ export const VariablePopoverTrigger = forwardRef<
actions={
variable?.type === "resource" && (
<>
- {isLocalResource(
- String(
- executeExpression(
- resources.get(variable.resourceId)?.url
- ) ?? ""
- )
- ) === false && (
+ {/* allow to copy curl only for default resource control */}
+ {resources.get(variable.resourceId)?.control ===
+ undefined && (
<Tooltip
content="Copy resource as cURL command"
side="bottom"
diff --git a/packages/sdk/src/schema/resources.ts b/packages/sdk/src/schema/resources.ts
index 3d89b8e78f..057f68ffb6 100644
--- a/packages/sdk/src/schema/resources.ts
+++ b/packages/sdk/src/schema/resources.ts
@@ -18,6 +18,7 @@ const Header = z.object({
export const Resource = z.object({
id: ResourceId,
name: z.string(),
+ control: z.optional(z.union([z.literal("system"), z.literal("graphql")])),
method: Method,
// expression
url: z.string(),