diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx index b6e22ed55..2896d5310 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInput.tsx @@ -1,7 +1,8 @@ +import { Checkbox } from "@follow/components/ui/checkbox/index.jsx" import type { LexicalRichEditorRef } from "@follow/components/ui/lexical-rich-editor/index.js" import { LexicalRichEditor } from "@follow/components/ui/lexical-rich-editor/index.js" import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js" -import { cn, stopPropagation } from "@follow/utils" +import { cn, nextFrame, stopPropagation } from "@follow/utils" import type { VariantProps } from "class-variance-authority" import { cva } from "class-variance-authority" import { noop } from "es-toolkit" @@ -13,6 +14,7 @@ import { memo, use, useCallback, useImperativeHandle, useRef, useState } from "r import { AIChatContextBar } from "~/modules/ai-chat/components/layouts/AIChatContextBar" import { FileUploadPlugin, MentionPlugin } from "../../editor" +import { useTimelineSummarySession } from "../../hooks/useTimelineSummarySession" import { AIPanelRefsContext } from "../../store/AIChatContext" import { useChatActions, useChatScene, useChatStatus } from "../../store/hooks" import { AIChatSendButton } from "./AIChatSendButton" @@ -40,115 +42,158 @@ const chatInputVariants = cva( interface ChatInputProps extends VariantProps { onSend: (message: EditorState | string, editor: LexicalEditor | null) => void ref?: Ref + isWelcomeScreen?: boolean } -export const ChatInput = memo(({ onSend, variant, ref: forwardedRef }: ChatInputProps) => { - const status = useChatStatus() - const chatActions = useChatActions() +export const ChatInput = memo( + ({ onSend, variant, ref: forwardedRef, isWelcomeScreen = false }: ChatInputProps) => { + const status = useChatStatus() + const chatActions = useChatActions() - const stop = useCallback(() => { - chatActions.stop() - }, [chatActions]) + const stop = useCallback(() => { + chatActions.stop() + }, [chatActions]) - const editorRef = useRef(null) + const editorRef = useRef(null) - useImperativeHandle( - forwardedRef, - () => editorRef.current, - // eslint-disable-next-line react-hooks/exhaustive-deps - [editorRef.current], - ) + useImperativeHandle( + forwardedRef, + () => editorRef.current, + // eslint-disable-next-line react-hooks/exhaustive-deps + [editorRef.current], + ) - const aiPanelRefs = use(AIPanelRefsContext) - if (editorRef.current) { - aiPanelRefs.inputRef.current = editorRef.current - } - - const [isEmpty, setIsEmpty] = useState(true) - const [currentEditor, setCurrentEditor] = useState(null) - - const isProcessing = status === "submitted" || status === "streaming" - - const handleSend = useCallback(() => { - if (currentEditor && editorRef.current && !editorRef.current.isEmpty()) { - onSend(currentEditor.getEditorState(), currentEditor) - editorRef.current.clear() + const aiPanelRefs = use(AIPanelRefsContext) + if (editorRef.current) { + aiPanelRefs.inputRef.current = editorRef.current } - }, [currentEditor, onSend]) - const handleKeyDown = useCallback( - (event: KeyboardEvent) => { - if (event.key === "Enter" && !event.shiftKey) { - event.preventDefault() - if (isProcessing) { - return false + const [isEmpty, setIsEmpty] = useState(true) + const [currentEditor, setCurrentEditor] = useState(null) + + const isProcessing = status === "submitted" || status === "streaming" + + const handleEditorChange = useCallback((editorState: EditorState, editor: LexicalEditor) => { + setCurrentEditor(editor) + // Update isEmpty state based on editor content + editorState.read(() => { + const root = $getRoot() + const textContent = root.getTextContent().trim() + setIsEmpty(textContent === "") + }) + }, []) + + const scene = useChatScene() + + // Determine if timeline summary can be reused and get today's session id + const { canReuseTimelineSummary, todayTimelineSummaryId } = useTimelineSummarySession() + + const [reuseSummary, setReuseSummary] = useState(true) + + const handleSend = useCallback(async () => { + if (currentEditor && editorRef.current && !editorRef.current.isEmpty()) { + if (isWelcomeScreen && canReuseTimelineSummary && reuseSummary) { + try { + await chatActions.switchToChat(todayTimelineSummaryId) + } catch { + // ignore switch errors + } } - handleSend() - return true + + const editorState = currentEditor?.getEditorState() + nextFrame(() => { + onSend(editorState, currentEditor) + }) + editorRef.current.clear() } - return false - }, - [handleSend, isProcessing], - ) + }, [ + currentEditor, + onSend, + isWelcomeScreen, + canReuseTimelineSummary, + reuseSummary, + chatActions, + todayTimelineSummaryId, + ]) - const handleEditorChange = useCallback((editorState: EditorState, editor: LexicalEditor) => { - setCurrentEditor(editor) - // Update isEmpty state based on editor content - editorState.read(() => { - const root = $getRoot() - const textContent = root.getTextContent().trim() - setIsEmpty(textContent === "") - }) - }, []) + const handleSendClick = useCallback(() => { + void handleSend() + }, [handleSend]) - const scene = useChatScene() + const handleKeyDown = useCallback( + (event: KeyboardEvent) => { + if (event.key === "Enter" && !event.shiftKey) { + event.preventDefault() + if (isProcessing) { + return false + } + void handleSend() + return true + } + return false + }, + [handleSend, isProcessing], + ) - return ( -
- {/* Input Area */} -
- - - -
- -
-
- - {/* Context Bar - only shown in non-onboarding scene, positioned below the input area */} - {scene !== "onboarding" && ( -
-
-
- onSend(prompt, null)} - /> -
- + {/* Input Area */} +
+ + + +
+
- )} -
- ) -}) + + {/* Context Bar - only shown in non-onboarding scene, positioned below the input area */} + {scene !== "onboarding" && ( +
+
+
+ onSend(prompt, null)} + /> +
+
+ {isWelcomeScreen && canReuseTimelineSummary && ( + + )} + +
+
+
+ )} +
+ ) + }, +) ChatInput.displayName = "ChatInput" diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx index 02bdd2142..bebc8a3ed 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx @@ -371,7 +371,11 @@ const ChatInterfaceContent = ({ centerInputOnEmpty }: ChatInterfaceProps) => { )} > {error && } - +
AI can make mistakes, please verify critical information.
diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx index 1b53e3d42..9450eb372 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx @@ -1,21 +1,18 @@ import { Folo } from "@follow/components/icons/folo.js" import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js" -import { FeedViewType } from "@follow/constants" import { useElementWidth } from "@follow/hooks" import { clsx } from "@follow/utils" import type { EditorState } from "lexical" import { AnimatePresence, m } from "motion/react" -import { useEffect, useMemo, useRef } from "react" +import { useEffect, useRef } from "react" import { useTranslation } from "react-i18next" import { useAISettingValue } from "~/atoms/settings/ai" -import { ROUTE_ENTRY_PENDING } from "~/constants" -import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { AISpline } from "~/modules/ai-chat/components/3d-models/AISpline" -import { AI_CHAT_SPECIAL_ID_PREFIX } from "../../constants" import { useAttachScrollBeyond } from "../../hooks/useAttachScrollBeyond" import { useMainEntryId } from "../../hooks/useMainEntryId" +import { useTimelineSummarySession } from "../../hooks/useTimelineSummarySession" import { useChatActions, useChatError, useChatStatus, useMessages } from "../../store/hooks" import { AIMessageParts } from "../message/AIMessageParts" import { DefaultWelcomeContent, EntrySummaryCard } from "../welcome" @@ -30,31 +27,15 @@ export const WelcomeScreen = ({ onSend, centerInputOnEmpty }: WelcomeScreenProps const { t } = useTranslation("ai") const aiSettings = useAISettingValue() const mainEntryId = useMainEntryId() - const { view, isAllFeeds, entryId } = useRouteParamsSelector((s) => ({ - view: s.view, - isAllFeeds: s.isAllFeeds, - entryId: s.entryId, - })) - - const realEntryId = entryId === ROUTE_ENTRY_PENDING ? "" : entryId - const isAllView = view === FeedViewType.All && isAllFeeds && !realEntryId - - const hasEntryContext = !!mainEntryId + const { todayTimelineSummaryId, canReuseTimelineSummary, hasEntryContext } = + useTimelineSummarySession() const enabledShortcuts = aiSettings.shortcuts?.filter((shortcut) => shortcut.enabled) || [] - const shouldFetchTimelineSummary = isAllView && !hasEntryContext + const shouldFetchTimelineSummary = canReuseTimelineSummary const { handleScroll } = useAttachScrollBeyond() const showTimelineSummary = shouldFetchTimelineSummary - const todayTimelineSummaryId = useMemo(() => { - const now = new Date() - - const day = now.getDate() - const month = now.getMonth() + 1 - const year = now.getFullYear() - - return `${AI_CHAT_SPECIAL_ID_PREFIX.TIMELINE_SUMMARY}${year}-${month}-${day}` - }, []) + // todayTimelineSummaryId from hook return ( - {hasEntryContext ? ( + {hasEntryContext && mainEntryId ? ( ) : ( = R
- +
diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useTimelineSummarySession.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useTimelineSummarySession.ts new file mode 100644 index 000000000..9366110f6 --- /dev/null +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useTimelineSummarySession.ts @@ -0,0 +1,39 @@ +import { FeedViewType } from "@follow/constants" +import { useMemo } from "react" + +import { ROUTE_ENTRY_PENDING } from "~/constants" +import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" + +import { AI_CHAT_SPECIAL_ID_PREFIX } from "../constants" +import { useMainEntryId } from "./useMainEntryId" + +export const getTodayTimelineSummaryId = () => { + const now = new Date() + const day = now.getDate() + const month = now.getMonth() + 1 + const year = now.getFullYear() + return `${AI_CHAT_SPECIAL_ID_PREFIX.TIMELINE_SUMMARY}${year}-${month}-${day}` +} + +export const useTimelineSummarySession = () => { + const mainEntryId = useMainEntryId() + const { view, isAllFeeds, entryId } = useRouteParamsSelector((s) => ({ + view: s.view, + isAllFeeds: s.isAllFeeds, + entryId: s.entryId, + })) + + const realEntryId = entryId === ROUTE_ENTRY_PENDING ? "" : entryId + const isAllView = view === FeedViewType.All && isAllFeeds && !realEntryId + const hasEntryContext = !!mainEntryId + const canReuseTimelineSummary = isAllView && !hasEntryContext + + const todayTimelineSummaryId = useMemo(() => getTodayTimelineSummaryId(), []) + + return { + todayTimelineSummaryId, + canReuseTimelineSummary, + isAllView, + hasEntryContext, + } +} diff --git a/packages/internal/components/src/ui/checkbox/index.tsx b/packages/internal/components/src/ui/checkbox/index.tsx index 6ea171c08..53441d5e4 100644 --- a/packages/internal/components/src/ui/checkbox/index.tsx +++ b/packages/internal/components/src/ui/checkbox/index.tsx @@ -9,9 +9,16 @@ import * as React from "react" type CheckboxProps = React.ComponentProps & HTMLMotionProps<"button"> & { indeterminate?: boolean + size?: "sm" | "md" } -function Checkbox({ className, onCheckedChange, indeterminate, ...props }: CheckboxProps) { +function Checkbox({ + className, + onCheckedChange, + indeterminate, + size = "md", + ...props +}: CheckboxProps) { const [isChecked, setIsChecked] = React.useState(props?.checked ?? props?.defaultChecked ?? false) React.useEffect(() => { @@ -31,7 +38,8 @@ function Checkbox({ className, onCheckedChange, indeterminate, ...props }: Check @@ -83,7 +91,7 @@ function Checkbox({ className, onCheckedChange, indeterminate, ...props }: Check viewBox="0 0 24 24" strokeWidth="3.5" stroke="currentColor" - className="size-3.5" + className={size === "sm" ? "size-3" : "size-3.5"} initial="unchecked" animate={isChecked ? "checked" : "unchecked"} >