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: Ai placeholder + select prompt text after inserting prompt from history (#2499)

Oleg Isonen5f6b8cad6a06
diff --git a/apps/builder/app/builder/features/ai/ai-command-bar.tsx b/apps/builder/app/builder/features/ai/ai-command-bar.tsx
index 82ae0b91cf..ce837cbbe6 100644
--- a/apps/builder/app/builder/features/ai/ai-command-bar.tsx
+++ b/apps/builder/app/builder/features/ai/ai-command-bar.tsx
@@ -49,11 +49,21 @@ import { fetchResult } from "./ai-fetch-result";
import { useEffectEvent } from "./hooks/effect-event";
import { AiApiException, RateLimitException } from "./api-exceptions";
import { useClientSettings } from "~/builder/shared/client-settings";
+import { flushSync } from "react-dom";
type PartialButtonProps<T = ComponentPropsWithoutRef<typeof Button>> = {
[key in keyof T]?: T[key];
};
+const useSelectText = () => {
+ const ref = useRef<HTMLTextAreaElement>(null);
+ const selectText = () => {
+ ref.current?.focus();
+ ref.current?.select();
+ };
+ return [ref, selectText] as const;
+};
+
const initialPrompts = [
"Create a hero section with a heading, subheading in white, CTA button and an image of a mountain in the background, add light blur to the image",
"Create a two column feature section with a heading and subheading in the left column, and an image that covers the right column",
@@ -82,6 +92,7 @@ export const AiCommandBar = ({ isPreviewMode }: { isPreviewMode: boolean }) => {
const getValue = useEffectEvent(() => {
return value;
});
+ const [textAreaRef, selectPrompt] = useSelectText();
const {
start,
@@ -265,14 +276,21 @@ export const AiCommandBar = ({ isPreviewMode }: { isPreviewMode: boolean }) => {
};
const handlePropmptClick = (prompt: string) => {
- setValue(prompt);
+ if (textAreaRef.current?.disabled) {
+ return;
+ }
+ // We can't select text right away because value will be set using setState.
+ flushSync(() => {
+ setValue(prompt);
+ });
+ selectPrompt();
};
if (isAiCommandBarVisible === false) {
return;
}
- let textAreaPlaceholder = "Enter value...";
+ let textAreaPlaceholder = "Welcome to Webstudio AI alpha!";
let textAreaValue = value;
let textAreaDisabled = false;
@@ -371,6 +389,7 @@ export const AiCommandBar = ({ isPreviewMode }: { isPreviewMode: boolean }) => {
>
<ScrollArea css={{ maxHeight: theme.spacing[29] }}>
<AutogrowTextArea
+ ref={textAreaRef}
autoFocus
disabled={textAreaDisabled}
placeholder={textAreaPlaceholder}
diff --git a/apps/builder/app/canvas/instance-selected-react.ts b/apps/builder/app/canvas/instance-selected-react.ts
index 0cf51bf88f..91918f7217 100644
--- a/apps/builder/app/canvas/instance-selected-react.ts
+++ b/apps/builder/app/canvas/instance-selected-react.ts
@@ -1,27 +1,9 @@
-import { useCallback, useEffect, useState } from "react";
+import { useEffect } from "react";
import { subscribeSelected } from "./instance-selected";
-
-/**
- * Debounce task execution until useEffect
- */
-const useDebounceEffect = () => {
- const [updateCallback, setUpdateCallback] = useState(() => () => {
- /* empty */
- });
-
- useEffect(() => {
- // Because of how our styles works we need to update after React render to be sure that
- // all styles are applied
- updateCallback();
- }, [updateCallback]);
-
- return useCallback((task: () => void) => {
- setUpdateCallback(() => task);
- }, []);
-};
+import { useEffectQueue } from "~/shared/hook-utils/use-effect-queue";
export const useSelectedInstance = () => {
- const execTaskInEffect = useDebounceEffect();
+ const execTaskInEffect = useEffectQueue();
useEffect(() => {
return subscribeSelected(execTaskInEffect);
diff --git a/apps/builder/app/shared/hook-utils/use-effect-queue.ts b/apps/builder/app/shared/hook-utils/use-effect-queue.ts
new file mode 100644
index 0000000000..334fb2a6dd
--- /dev/null
+++ b/apps/builder/app/shared/hook-utils/use-effect-queue.ts
@@ -0,0 +1,23 @@
+import { useCallback, useEffect, useState } from "react";
+
+/**
+ * Debounce task execution until useEffect
+ * Example:
+ * const scheduleEffect = useEffectQueue();
+ * scheduleEffect(() => { ... })
+ */
+export const useEffectQueue = () => {
+ const [updateCallback, setUpdateCallback] = useState(() => () => {
+ /* empty */
+ });
+
+ useEffect(() => {
+ // Because of how our styles works we need to update after React render to be sure that
+ // all styles are applied
+ updateCallback();
+ }, [updateCallback]);
+
+ return useCallback((task: () => void) => {
+ setUpdateCallback(() => task);
+ }, []);
+};