From a4b8883473b91f852d37afc890988a9294dc0cdd Mon Sep 17 00:00:00 2001 From: Pabloader Date: Sat, 2 Nov 2024 13:10:31 +0000 Subject: [PATCH] Prompts editor --- src/games/ai/components/autoTextarea.tsx | 34 +++++ .../ai/components/header/header.module.css | 16 +- src/games/ai/components/header/header.tsx | 66 ++++++-- src/games/ai/components/input.tsx | 9 +- src/games/ai/components/message/message.tsx | 7 +- src/games/ai/components/minichat/minichat.tsx | 9 +- src/games/ai/const.ts | 141 +----------------- src/games/ai/contexts/llm.tsx | 66 +++++--- src/games/ai/contexts/state.tsx | 40 ++++- src/games/ai/dom.ts | 12 -- 10 files changed, 193 insertions(+), 207 deletions(-) create mode 100644 src/games/ai/components/autoTextarea.tsx diff --git a/src/games/ai/components/autoTextarea.tsx b/src/games/ai/components/autoTextarea.tsx new file mode 100644 index 0000000..b56dcfc --- /dev/null +++ b/src/games/ai/components/autoTextarea.tsx @@ -0,0 +1,34 @@ +import { useEffect, useRef, useState } from "preact/hooks"; +import type { JSX } from "preact/jsx-runtime" + +export const AutoTextarea = (props: JSX.HTMLAttributes) => { + const { value } = props; + const ref = useRef(null); + const [isVisible, setVisible] = useState(false); + + useEffect(() => { + if (ref.current) { + const area = ref.current; + + area.style.height = '0'; // reset + area.style.height = `${area.scrollHeight}px`; + } + }, [value, isVisible]); + + useEffect(() => { + if (ref.current) { + const observer = new IntersectionObserver(([entry]) => { + setVisible(entry.isIntersecting); + if (entry.isIntersecting) { + observer.disconnect(); + } + }); + + observer.observe(ref.current); + + return () => observer.disconnect(); + } + }, [ref.current]); + + return