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: Search instances in commands panel (#5529)

Oleg Isonen0793cdc42fc3
diff --git a/apps/builder/app/builder/features/command-panel/groups/index.ts b/apps/builder/app/builder/features/command-panel/groups/index.ts
index edca3d1a33..8b90957b4d 100644
--- a/apps/builder/app/builder/features/command-panel/groups/index.ts
+++ b/apps/builder/app/builder/features/command-panel/groups/index.ts
@@ -31,6 +31,11 @@ import {
CssVariablesGroup,
type CssVariableOption,
} from "./css-variables-group";
+import {
+ $instanceOptions,
+ InstancesGroup,
+ type InstanceOption,
+} from "./instances-group";
export type Option =
| ComponentOption
@@ -41,7 +46,8 @@ export type Option =
| TokenOption
| DuplicateTokenOption
| DataVariableOption
- | CssVariableOption;
+ | CssVariableOption
+ | InstanceOption;
export type OptionByType<T extends Option["type"]> = Extract<
Option,
@@ -58,6 +64,7 @@ export const $allOptions = computed(
$tokenOptions,
$dataVariableOptions,
$cssVariableOptions,
+ $instanceOptions,
],
(
componentOptions,
@@ -67,7 +74,8 @@ export const $allOptions = computed(
commandOptions,
tokenOptions,
dataVariableOptions,
- cssVariableOptions
+ cssVariableOptions,
+ instanceOptions
) => [
...commandOptions,
...componentOptions,
@@ -77,6 +85,7 @@ export const $allOptions = computed(
...tokenOptions,
...dataVariableOptions,
...cssVariableOptions,
+ ...instanceOptions,
]
);
@@ -96,6 +105,7 @@ export const groups: {
duplicateToken: DuplicateTokensGroup,
dataVariable: DataVariablesGroup,
cssVariable: CssVariablesGroup,
+ instance: InstancesGroup,
};
export type {
@@ -108,6 +118,7 @@ export type {
DuplicateTokenOption,
DataVariableOption,
CssVariableOption,
+ InstanceOption,
};
export {
@@ -120,4 +131,5 @@ export {
DuplicateTokensGroup,
DataVariablesGroup,
CssVariablesGroup,
+ InstancesGroup,
};
diff --git a/apps/builder/app/builder/features/command-panel/groups/instances-group.tsx b/apps/builder/app/builder/features/command-panel/groups/instances-group.tsx
new file mode 100644
index 0000000000..1defe325e7
--- /dev/null
+++ b/apps/builder/app/builder/features/command-panel/groups/instances-group.tsx
@@ -0,0 +1,86 @@
+import {
+ CommandGroup,
+ CommandGroupHeading,
+ CommandItem,
+ Text,
+} from "@webstudio-is/design-system";
+import { computed } from "nanostores";
+import { parseComponentName } from "@webstudio-is/sdk";
+import type { Instance } from "@webstudio-is/sdk";
+import { $instances, $pages } from "~/shared/sync/data-stores";
+import { getInstanceLabel } from "~/builder/shared/instance-label";
+import { $awareness, findAwarenessByInstanceId } from "~/shared/awareness";
+import { closeCommandPanel, $isCommandPanelOpen } from "../command-state";
+import type { BaseOption } from "../shared/types";
+import { setActiveSidebarPanel } from "~/builder/shared/nano-states";
+import { humanizeString } from "~/shared/string-utils";
+
+export type InstanceOption = BaseOption & {
+ type: "instance";
+ instance: Instance;
+};
+
+export const $instanceOptions = computed(
+ [$isCommandPanelOpen, $instances, $pages],
+ (isOpen, instances, pages) => {
+ if (!isOpen || !pages) {
+ return [];
+ }
+ const instanceOptions: InstanceOption[] = [];
+ for (const instance of instances.values()) {
+ const label = getInstanceLabel(instance);
+ // Include instance label and component name in search terms
+ instanceOptions.push({
+ terms: ["instances", label, instance.component],
+ type: "instance",
+ instance,
+ });
+ }
+ return instanceOptions;
+ }
+);
+
+export const InstancesGroup = ({ options }: { options: InstanceOption[] }) => {
+ return (
+ <CommandGroup
+ name="instance"
+ heading={<CommandGroupHeading>Instances</CommandGroupHeading>}
+ actions={["select"]}
+ >
+ {options.map(({ instance }) => {
+ const label = getInstanceLabel(instance);
+ const [_namespace, componentName] = parseComponentName(
+ instance.component
+ );
+ const componentLabel = humanizeString(componentName);
+ return (
+ <CommandItem
+ key={instance.id}
+ value={instance.id}
+ onSelect={() => {
+ closeCommandPanel();
+ const pages = $pages.get();
+ const instances = $instances.get();
+ if (pages && instances) {
+ const awareness = findAwarenessByInstanceId(
+ pages,
+ instances,
+ instance.id
+ );
+ if (awareness) {
+ $awareness.set(awareness);
+ setActiveSidebarPanel("auto");
+ }
+ }
+ }}
+ >
+ <Text variant="labelsTitleCase">{label}</Text>
+ <Text color="moreSubtle" truncate css={{ maxWidth: "30ch" }}>
+ {componentLabel}
+ </Text>
+ </CommandItem>
+ );
+ })}
+ </CommandGroup>
+ );
+};