Skip to content

Commit f712740

Browse files
authored
fix: Fix delete unused css vars and unused detection (#5505)
## Description - deleting all unused command only worked if the properties were on the selected instance - variables used inside of calc calls and similar were not found ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent f63b5ca commit f712740

11 files changed

Lines changed: 3641 additions & 590 deletions

File tree

apps/builder/app/builder/features/command-panel/groups/css-variables-group.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import type { CssProperty } from "@webstudio-is/css-engine";
1515
import {
1616
DeleteCssVariableDialog,
1717
RenameCssVariableDialog,
18-
$usedCssVariablesInInstances,
1918
$cssVariableInstancesByVariable,
2019
$cssVariableDefinitionsByVariable,
20+
$unusedCssVariables,
2121
} from "~/builder/shared/css-variable-utils";
2222
import { deleteProperty } from "~/builder/features/style-panel/shared/use-style-data";
2323
import { InstanceList, showInstance } from "../shared/instance-list";
@@ -38,14 +38,14 @@ export type CssVariableOption = BaseOption & {
3838
};
3939

4040
export const $cssVariableOptions = computed(
41-
[$cssVariableDefinitionsByVariable, $usedCssVariablesInInstances],
42-
(definitionsByVariable, usedVariablesInInstances) => {
41+
[$cssVariableDefinitionsByVariable, $unusedCssVariables],
42+
(definitionsByVariable, unusedVariables) => {
4343
const cssVariableOptions: CssVariableOption[] = [];
4444

4545
// Create options for each defined CSS variable on each instance
4646
for (const [property, instanceIds] of definitionsByVariable) {
4747
for (const instanceId of instanceIds) {
48-
const usages = usedVariablesInInstances.get(property) ?? 0;
48+
const usages = unusedVariables.has(property) ? 0 : 1; // 0 if unused, 1+ otherwise
4949
cssVariableOptions.push({
5050
terms: [
5151
"css variables",
@@ -130,7 +130,12 @@ export const CssVariablesGroup = ({
130130
{formatUsageCount(usages)}
131131
</Text>
132132
</Text>
133-
<Text as="span" color="moreSubtle">
133+
<Text
134+
as="span"
135+
css={{ maxWidth: "20ch" }}
136+
truncate
137+
color="moreSubtle"
138+
>
134139
{getInstanceLabel(instanceId)}
135140
</Text>
136141
</CommandItem>

apps/builder/app/builder/features/command-panel/shared/instance-list.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ import {
1313
Kbd,
1414
} from "@webstudio-is/design-system";
1515
import type { Instance } from "@webstudio-is/sdk";
16-
import {
17-
$instances,
18-
$pages,
19-
$registeredComponentMetas,
20-
} from "~/shared/nano-states";
16+
import { $instances, $pages } from "~/shared/nano-states";
2117
import { getInstanceLabel } from "~/builder/shared/instance-label";
2218
import { $awareness, findAwarenessByInstanceId } from "~/shared/awareness";
19+
import { buildInstancePath } from "~/shared/instance-utils";
2320
import { $commandContent } from "../command-state";
2421
import { $activeInspectorPanel } from "~/builder/shared/nano-states";
2522

2623
export type InstanceOption = {
2724
label: string;
2825
id: string;
26+
path: string[];
2927
};
3028

3129
type InstanceListProps = {
@@ -35,17 +33,19 @@ type InstanceListProps = {
3533

3634
export const InstanceList = ({ instanceIds, onSelect }: InstanceListProps) => {
3735
const instances = $instances.get();
38-
const metas = $registeredComponentMetas.get();
36+
const pages = $pages.get();
3937
const usedInInstances: InstanceOption[] = [];
4038
for (const instanceId of instanceIds) {
4139
const instance = instances.get(instanceId);
42-
const meta = metas.get(instance?.component ?? "");
43-
if (instance && meta) {
44-
usedInInstances.push({
45-
label: getInstanceLabel(instance),
46-
id: instance.id,
47-
});
40+
if (!instance || !pages) {
41+
continue;
4842
}
43+
const path = buildInstancePath(instanceId, pages, instances);
44+
usedInInstances.push({
45+
label: getInstanceLabel(instance),
46+
id: instance.id,
47+
path,
48+
});
4949
}
5050
const [search, setSearch] = useState("");
5151

@@ -86,7 +86,7 @@ export const InstanceList = ({ instanceIds, onSelect }: InstanceListProps) => {
8686
<Text color="subtle">No instances found</Text>
8787
</Flex>
8888
) : (
89-
matches.map(({ id, label }) => (
89+
matches.map(({ id, label, path }) => (
9090
<CommandItem
9191
key={id}
9292
value={id}
@@ -95,6 +95,13 @@ export const InstanceList = ({ instanceIds, onSelect }: InstanceListProps) => {
9595
}}
9696
>
9797
<Text variant="labelsTitleCase">{label}</Text>
98+
<Text
99+
color="moreSubtle"
100+
truncate
101+
css={{ maxWidth: "30ch" }}
102+
>
103+
/{path.join("/")}
104+
</Text>
98105
</CommandItem>
99106
))
100107
)}
@@ -105,7 +112,7 @@ export const InstanceList = ({ instanceIds, onSelect }: InstanceListProps) => {
105112
<CommandGroupFooter>
106113
<Flex grow>
107114
<Button tabIndex={-1} color="ghost" onClick={goBack}>
108-
Back <Kbd value={["backspace"]} />
115+
<Kbd value={["backspace"]} /> Back
109116
</Button>
110117
</Flex>
111118
</CommandGroupFooter>

0 commit comments

Comments
 (0)