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: Enable features via url (#2459)

Giuseppe21cbb09f0ea9
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 3e83dd1972..6ea5a013db 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -321,7 +321,9 @@ export const Builder = ({
}}
/>
</Workspace>
- {isFeatureEnabled("ai") && <AiCommandBar />}
+ {isFeatureEnabled("ai") && isPreviewMode === false && (
+ <AiCommandBar />
+ )}
</Main>
<SidePanel gridArea="sidebar" isPreviewMode={isPreviewMode}>
<SidebarLeft publish={publish} />
diff --git a/apps/builder/app/builder/features/inspector/inspector.tsx b/apps/builder/app/builder/features/inspector/inspector.tsx
index c3ab8dcd5c..5165d3605e 100644
--- a/apps/builder/app/builder/features/inspector/inspector.tsx
+++ b/apps/builder/app/builder/features/inspector/inspector.tsx
@@ -28,8 +28,6 @@ import { NavigatorTree } from "~/builder/shared/navigator-tree";
import type { Settings } from "~/builder/shared/client-settings";
import { MetaIcon } from "~/builder/shared/meta-icon";
import { getInstanceLabel } from "~/shared/instance-utils";
-import { isFeatureEnabled } from "@webstudio-is/feature-flags";
-import { CommandsBar } from "../ai";
const InstanceInfo = ({ instance }: { instance: Instance }) => {
const metas = useStore(registeredComponentMetasStore);
@@ -100,7 +98,6 @@ export const Inspector = ({ publish, navigatorLayout }: InspectorProps) => {
const availableTabs = [
isStyleTabVisible ? "style" : undefined,
"settings",
- isFeatureEnabled("ai") ? "ai" : undefined,
].filter((tab) => tab);
return (
@@ -122,9 +119,6 @@ export const Inspector = ({ publish, navigatorLayout }: InspectorProps) => {
<PanelTabsTrigger value="style">Style</PanelTabsTrigger>
)}
<PanelTabsTrigger value="settings">Settings</PanelTabsTrigger>
- {isFeatureEnabled("ai") && (
- <PanelTabsTrigger value="ai">AI</PanelTabsTrigger>
- )}
</PanelTabsList>
<PanelTabsContent value="style" css={contentStyle} tabIndex={-1}>
<InstanceInfo instance={selectedInstance} />
@@ -145,22 +139,6 @@ export const Inspector = ({ publish, navigatorLayout }: InspectorProps) => {
/>
</ScrollArea>
</PanelTabsContent>
- {isFeatureEnabled("ai") ? (
- <PanelTabsContent value="ai" css={contentStyle} tabIndex={-1}>
- <Flex direction="column" gap="4" css={{ padding: 10 }}>
- {isFeatureEnabled("ai") ? (
- <Flex direction="column" gap="2">
- <Text variant="titles">Commands Bar</Text>
- <Text>
- You can edit styles, remove instances or ask to generate
- something
- </Text>
- <CommandsBar />
- </Flex>
- ) : null}
- </Flex>
- </PanelTabsContent>
- ) : null}
</Flex>
</PanelTabs>
</FloatingPanelProvider>
diff --git a/apps/builder/app/root.tsx b/apps/builder/app/root.tsx
index 7aab5593c2..7c081e9dc6 100644
--- a/apps/builder/app/root.tsx
+++ b/apps/builder/app/root.tsx
@@ -4,16 +4,21 @@ import { setEnv } from "@webstudio-is/feature-flags";
import { withSentry, ErrorBoundary } from "@sentry/remix";
import env from "./shared/env";
import type { ComponentProps } from "react";
+import { useSetFeatures } from "./shared/use-set-features";
setEnv(env.FEATURES as string);
type OutletProps = ComponentProps<typeof Outlet>;
-const RootWithErrorBoundary = (props: OutletProps) => (
- <ErrorBoundary>
- <Outlet {...props} />
- </ErrorBoundary>
-);
+const RootWithErrorBoundary = (props: OutletProps) => {
+ useSetFeatures();
+
+ return (
+ <ErrorBoundary>
+ <Outlet {...props} />
+ </ErrorBoundary>
+ );
+};
export default withSentry(
// withSentryRouteTracing() expects a type from component that is not necessary true.
diff --git a/apps/builder/app/routes/dashboard._index.tsx b/apps/builder/app/routes/dashboard._index.tsx
index c5e01d84e5..8e0bca907d 100644
--- a/apps/builder/app/routes/dashboard._index.tsx
+++ b/apps/builder/app/routes/dashboard._index.tsx
@@ -1,4 +1,4 @@
-import type { ComponentProps } from "react";
+import { type ComponentProps } from "react";
import { useLoaderData, useRouteError } from "@remix-run/react";
import type { LoaderArgs } from "@remix-run/node";
import { redirect } from "@remix-run/node";
diff --git a/apps/builder/app/routes/rest.ai.detect.ts b/apps/builder/app/routes/rest.ai.detect.ts
index f93342e31a..bd91a35ddd 100644
--- a/apps/builder/app/routes/rest.ai.detect.ts
+++ b/apps/builder/app/routes/rest.ai.detect.ts
@@ -8,7 +8,6 @@ import {
copywriter,
operations,
} from "@webstudio-is/ai/index.server";
-import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import env from "~/env/env.server";
import { createContext } from "~/shared/context.server";
@@ -18,18 +17,7 @@ export const RequestParamsSchema = z.object({
});
export const action = async ({ request }: ActionArgs) => {
- if (isFeatureEnabled("ai") === false) {
- return {
- id: "ai",
- ...createErrorResponse({
- error: "featureDisabled",
- status: 503,
- message: "The feature is not available",
- debug: "aiCopy feature disabled",
- }),
- llmMessages: [],
- };
- }
+ // @todo Reinstate isFeatureEnabled('ai')
if (env.OPENAI_KEY === undefined) {
return {
diff --git a/apps/builder/app/routes/rest.ai.ts b/apps/builder/app/routes/rest.ai.ts
index e641890e9d..82e0e4001d 100644
--- a/apps/builder/app/routes/rest.ai.ts
+++ b/apps/builder/app/routes/rest.ai.ts
@@ -14,7 +14,6 @@ import {
operations as clientOperations,
queryImagesAndMutateTemplate,
} from "@webstudio-is/ai";
-import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import env from "~/env/env.server";
import { createContext } from "~/shared/context.server";
import { authorizeProject } from "@webstudio-is/trpc-interface/index.server";
@@ -40,18 +39,7 @@ export const RequestParamsSchema = z.object({
export const maxDuration = 180; // seconds
export const action = async ({ request }: ActionArgs) => {
- if (isFeatureEnabled("ai") === false) {
- return {
- id: "ai",
- ...createErrorResponse({
- error: "featureDisabled",
- status: 503,
- message: "The feature is not available",
- debug: "aiCopy feature disabled",
- }),
- llmMessages: [],
- };
- }
+ // @todo Reinstate isFeatureEnabled('ai')
if (env.OPENAI_KEY === undefined) {
return {
diff --git a/apps/builder/app/shared/use-set-features.ts b/apps/builder/app/shared/use-set-features.ts
new file mode 100644
index 0000000000..79d30cbadd
--- /dev/null
+++ b/apps/builder/app/shared/use-set-features.ts
@@ -0,0 +1,20 @@
+import { useSearchParams } from "@remix-run/react";
+import { parse, readLocal, setLocal } from "@webstudio-is/feature-flags";
+import { useEffect } from "react";
+
+// Allows to set feature flags via URL parameter features=name1,name2,name3
+export const useSetFeatures = () => {
+ const [searchParams] = useSearchParams();
+ useEffect(() => {
+ const features = searchParams.get("features");
+ if (features) {
+ const currentFlags = readLocal();
+ for (const flag of parse(features)) {
+ if (currentFlags.includes(flag) === false) {
+ currentFlags.push(flag);
+ }
+ }
+ setLocal(currentFlags.join(","));
+ }
+ }, [searchParams]);
+};
diff --git a/packages/feature-flags/src/feature.ts b/packages/feature-flags/src/feature.ts
index 081c2ef356..789abb7433 100644
--- a/packages/feature-flags/src/feature.ts
+++ b/packages/feature-flags/src/feature.ts
@@ -4,11 +4,20 @@ let env = "";
type Name = keyof typeof flags;
-const parse = (flags?: string | null): Array<Name> =>
+export const parse = (flags?: string | null): Array<Name> =>
// Supports both, space and comma separated items
(flags ?? "").split(/\s|,/).filter(Boolean) as Array<Name>;
-const readLocal = (): Array<Name> => {
+export const setLocal = (features: string) => {
+ if (flags) {
+ const parsed = parse(features).filter(
+ (flag) => flag in flags
+ ) as Array<Name>;
+ localStorage.setItem("features", parsed.join(","));
+ }
+};
+
+export const readLocal = (): Array<Name> => {
try {
const flags = localStorage.getItem("features");
return parse(flags);