diff --git a/src/components/AsciiLine/AsciiLine.tsx b/src/components/AsciiLine/AsciiLine.tsx index fd1d313..ea0f370 100644 --- a/src/components/AsciiLine/AsciiLine.tsx +++ b/src/components/AsciiLine/AsciiLine.tsx @@ -1,4 +1,4 @@ -import { useCharWidth } from '@/hooks'; +import { useCharDimensions } from '@/hooks'; import clsx from 'clsx'; export interface AsciiLineProps { @@ -12,7 +12,7 @@ export const AsciiLine = ({ withEndCap, capChar = `+`, }: Readonly) => { - const { charWidth } = useCharWidth(); + const { width: charWidth } = useCharDimensions(); return ( ( null, ); + useEffect(() => { const currentRef = wrapperRef.current; // find target width in chars @@ -37,7 +38,8 @@ export const AsciiProgressBar = ({ setProgressCharCount(0); const { width } = entries[0].contentRect; setWidthInChars(0); - const charWidth = getCharWidth(); + // purposely not using the hook here since we don't want to trigger a re-render + const { width: charWidth } = getCharDimensions(); const widthInChars = Math.floor(width / charWidth) - 3 - // account for left and right end caps (+1 for percentage padding on the right) diff --git a/src/components/Shell/Shell.tsx b/src/components/Shell/Shell.tsx index 2265959..d0a1152 100644 --- a/src/components/Shell/Shell.tsx +++ b/src/components/Shell/Shell.tsx @@ -17,8 +17,10 @@ import { PromptHistoryEntry, PromptResult, } from '@/context/PromptHistoryContext/types'; +import { useCharDimensions } from '@/hooks'; import { CustomEvents, RunEvent, SearchParams } from '@/util/types'; import { useLocation } from '@reach/router'; +import clsx from 'clsx'; import { useTranslation } from 'gatsby-plugin-react-i18next'; import { ChangeEventHandler, @@ -44,6 +46,7 @@ export const Shell = ({ initialPrompt, }: Readonly) => { const textAreaRef = useRef(null); + const textAreaCopyRef = useRef(null); const { promptHistory, dispatch } = useContext(PromptHistoryContext); const [currentPrompt, setCurrentPrompt] = useState(``); @@ -51,7 +54,9 @@ export const Shell = ({ const [standalone, setStandalone] = useState(null); const [currentDescHistoryIndex, setCurrentDescHistoryIndex] = useState(-1); // -1 means current prompt is not in history const [tabSuggestions, setTabSuggestions] = useState([]); + const [isMainFlexCol, setIsMainFlexCol] = useState(false); + const { height: charHeight } = useCharDimensions(); const location = useLocation(); const { t } = useTranslation(); @@ -136,6 +141,7 @@ export const Shell = ({ }); if (!(`ontouchstart` in window)) { + console.log(`focusing textarea`); textAreaRef.current?.focus(); } }, @@ -160,10 +166,6 @@ export const Shell = ({ }); }, [dispatch, promptHistory, standalone]); - useEffect(() => { - textAreaRef.current?.scrollIntoView(); - }, [currentPrompt]); - useEffect(() => { window.addEventListener(CustomEvents.run, handleRunEvent); window.addEventListener( @@ -195,16 +197,20 @@ export const Shell = ({ e, ) => { setCurrentPrompt(e.target.value); - if (!textAreaRef.current) return; + if (!textAreaRef.current || !textAreaCopyRef.current) return; textAreaRef.current.style.height = `auto`; // reset height textAreaRef.current.style.height = `${textAreaRef.current.scrollHeight}px`; // auto grow the textarea to fit the text + setIsMainFlexCol( + textAreaCopyRef.current.scrollHeight > charHeight && + e.target.value !== ``, + ); }; const handleUserTextAreaKeyDown: KeyboardEventHandler = ( event, ) => { setTabSuggestions([]); - + textAreaRef.current?.scrollIntoView(); if (event.key.length === 1) { setCurrentDescHistoryIndex(-1); // only reset history index if user is typing setTmpPrompt(currentPrompt + event.key); @@ -292,6 +298,7 @@ export const Shell = ({ flex-col hover:cursor-text relative + break-words w-full`} onClickCapture={(e) => { if ( @@ -311,10 +318,10 @@ export const Shell = ({ if (hideEntry) return null; return ( -

+ {fullPrompt} -

+
{responses.map((response, j) => ( {response.result !== `` ? ( @@ -327,29 +334,39 @@ export const Shell = ({ ); })} -
-            
+          
+