Clean up editor code and reorganize contents (#478)

This commit is contained in:
Jeremy 2024-11-01 15:31:54 +05:30 committed by GitHub
parent b3aa3cb18a
commit 5190b109c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 475 additions and 444 deletions

View file

@ -5,7 +5,6 @@ import { useSWRConfig } from 'swr';
import { Suggestion } from '@/db/schema';
import { UICanvas } from './canvas';
import useWindowSize from './use-window-size';
type StreamingDelta = {
type: 'text-delta' | 'title' | 'id' | 'suggestion' | 'clear' | 'finish';
@ -17,7 +16,7 @@ export function useCanvasStream({
setCanvas,
}: {
streamingData: JSONValue[] | undefined;
setCanvas: Dispatch<SetStateAction<UICanvas | null>>;
setCanvas: Dispatch<SetStateAction<UICanvas>>;
}) {
const { mutate } = useSWRConfig();
const [optimisticSuggestions, setOptimisticSuggestions] = useState<
@ -32,9 +31,6 @@ export function useCanvasStream({
}
}, [optimisticSuggestions, mutate]);
const { width: windowWidth = 1920, height: windowHeight = 1080 } =
useWindowSize();
useEffect(() => {
const mostRecentDelta = streamingData?.at(-1);
if (!mostRecentDelta) return;
@ -42,23 +38,19 @@ export function useCanvasStream({
const delta = mostRecentDelta as StreamingDelta;
setCanvas((draftCanvas) => {
if (!draftCanvas) {
return {
content: '',
title: '',
isVisible: false,
documentId: delta.type === 'id' ? (delta.content as string) : '',
status: 'idle',
boundingBox: {
top: windowHeight / 4,
left: windowWidth / 4,
width: 250,
height: 50,
},
};
}
switch (delta.type) {
case 'id':
return {
...draftCanvas,
documentId: delta.content as string,
};
case 'title':
return {
...draftCanvas,
title: delta.content as string,
};
case 'text-delta':
return {
...draftCanvas,
@ -70,12 +62,6 @@ export function useCanvasStream({
status: 'streaming',
};
case 'title':
return {
...draftCanvas,
title: delta.content as string,
};
case 'suggestion':
setTimeout(() => {
setOptimisticSuggestions((currentSuggestions) => [