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.

feat: Delete unused data variables command (#5489)

Oleg Isonen7fce2be32110
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 3499033a6a..813101e7ea 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -60,6 +60,7 @@ import { Loading, LoadingBackground } from "./shared/loading";
import { mergeRefs } from "@react-aria/utils";
import { CommandPanel } from "./features/command-panel";
import { DeleteUnusedTokensDialog } from "~/builder/shared/style-source-utils";
+import { DeleteUnusedDataVariablesDialog } from "~/builder/shared/data-variable-utils";
import {
initCopyPaste,
@@ -441,6 +442,7 @@ export const Builder = ({
<BlockingAlerts />
<CommandPanel />
<DeleteUnusedTokensDialog />
+ <DeleteUnusedDataVariablesDialog />
<RemoteDialog />
</div>
</TooltipProvider>
diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.ts
index 061b6fb1ac..7986ae5c76 100644
--- a/apps/builder/app/builder/shared/commands.ts
+++ b/apps/builder/app/builder/shared/commands.ts
@@ -58,6 +58,7 @@ import { $instanceTags } from "../features/style-panel/shared/model";
import { reactPropsToStandardAttributes } from "@webstudio-is/react-sdk";
import { isSyncIdle } from "./sync/sync-server";
import { openDeleteUnusedTokensDialog } from "~/builder/shared/style-source-utils";
+import { openDeleteUnusedDataVariablesDialog } from "~/builder/shared/data-variable-utils";
export const $styleSourceInputElement = atom<HTMLInputElement | undefined>();
@@ -668,5 +669,13 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
openDeleteUnusedTokensDialog();
},
},
+
+ {
+ name: "deleteUnusedDataVariables",
+ label: "Delete unused data variables",
+ handler: () => {
+ openDeleteUnusedDataVariablesDialog();
+ },
+ },
],
});
diff --git a/apps/builder/app/builder/shared/data-variable-utils.tsx b/apps/builder/app/builder/shared/data-variable-utils.tsx
index 20b2829f4a..73967e3a30 100644
--- a/apps/builder/app/builder/shared/data-variable-utils.tsx
+++ b/apps/builder/app/builder/shared/data-variable-utils.tsx
@@ -1,5 +1,7 @@
import { useState, useEffect } from "react";
-import { computed } from "nanostores";
+import { atom, computed } from "nanostores";
+import { useStore } from "@nanostores/react";
+import { toast } from "@webstudio-is/design-system";
import {
Dialog,
DialogContent,
@@ -23,6 +25,12 @@ import {
import { serverSyncStore } from "~/shared/sync";
import { findVariableUsagesByInstance } from "~/shared/data-variables";
+const $isDeleteUnusedDataVariablesDialogOpen = atom(false);
+
+export const openDeleteUnusedDataVariablesDialog = () => {
+ $isDeleteUnusedDataVariablesDialogOpen.set(true);
+};
+
export type DataVariableError = {
type: "required" | "duplicate";
message: string;
@@ -100,6 +108,41 @@ export const DeleteDataVariableDialog = ({
);
};
+export const deleteUnusedDataVariables = () => {
+ const dataSources = $dataSources.get();
+ const usedVariablesInInstances = $usedVariablesInInstances.get();
+ const unusedVariableIds: DataSource["id"][] = [];
+
+ for (const dataSource of dataSources.values()) {
+ if (dataSource.type === "variable") {
+ const usages = usedVariablesInInstances.get(dataSource.id);
+ if (usages === undefined || usages.size === 0) {
+ unusedVariableIds.push(dataSource.id);
+ }
+ }
+ }
+
+ if (unusedVariableIds.length === 0) {
+ return 0;
+ }
+
+ serverSyncStore.createTransaction(
+ [$dataSources, $resources],
+ (dataSources, resources) => {
+ for (const variableId of unusedVariableIds) {
+ const dataSource = dataSources.get(variableId);
+ // Cleanup resource when variable is deleted
+ if (dataSource?.type === "resource") {
+ resources.delete(dataSource.resourceId);
+ }
+ dataSources.delete(variableId);
+ }
+ }
+ );
+
+ return unusedVariableIds.length;
+};
+
export const validateDataVariableName = (
name: string,
variableId?: DataSource["id"],
@@ -232,3 +275,92 @@ export const RenameDataVariableDialog = ({
</Dialog>
);
};
+
+export const DeleteUnusedDataVariablesDialog = () => {
+ const open = useStore($isDeleteUnusedDataVariablesDialogOpen);
+ const usedVariablesInInstances = useStore($usedVariablesInInstances);
+ const dataSources = useStore($dataSources);
+
+ const handleClose = () => {
+ $isDeleteUnusedDataVariablesDialogOpen.set(false);
+ };
+
+ const unusedVariables: Array<{ id: string; name: string }> = [];
+ for (const dataSource of dataSources.values()) {
+ if (dataSource.type === "variable") {
+ const usages = usedVariablesInInstances.get(dataSource.id);
+ if (usages === undefined || usages.size === 0) {
+ unusedVariables.push({ id: dataSource.id, name: dataSource.name });
+ }
+ }
+ }
+
+ return (
+ <Dialog
+ open={open}
+ onOpenChange={(isOpen) => {
+ if (isOpen === false) {
+ handleClose();
+ }
+ }}
+ >
+ <DialogContent
+ onKeyDown={(event) => {
+ event.stopPropagation();
+ }}
+ >
+ <DialogTitle>Delete unused data variables</DialogTitle>
+ <Flex gap="3" direction="column" css={{ padding: theme.panel.padding }}>
+ {unusedVariables.length === 0 ? (
+ <Text>There are no unused data variables to delete.</Text>
+ ) : (
+ <>
+ <Text>
+ Delete {unusedVariables.length} unused data{" "}
+ {unusedVariables.length === 1 ? "variable" : "variables"} from
+ the project?
+ </Text>
+ <Text
+ variant="mono"
+ css={{
+ maxHeight: 200,
+ overflowY: "auto",
+ backgroundColor: theme.colors.backgroundPanel,
+ borderRadius: theme.borderRadius[4],
+ wordBreak: "break-word",
+ }}
+ >
+ {unusedVariables.map((variable) => variable.name).join(", ")}
+ </Text>
+ </>
+ )}
+ <Flex direction="rowReverse" gap="2">
+ {unusedVariables.length > 0 && (
+ <Button
+ color="destructive"
+ onClick={() => {
+ const deletedCount = deleteUnusedDataVariables();
+ handleClose();
+ if (deletedCount === 0) {
+ toast.info("No unused data variables to delete");
+ } else {
+ toast.success(
+ `Deleted ${deletedCount} unused data ${deletedCount === 1 ? "variable" : "variables"}`
+ );
+ }
+ }}
+ >
+ Delete
+ </Button>
+ )}
+ <DialogClose>
+ <Button color="ghost">
+ {unusedVariables.length > 0 ? "Cancel" : "Close"}
+ </Button>
+ </DialogClose>
+ </Flex>
+ </Flex>
+ </DialogContent>
+ </Dialog>
+ );
+};