feat(ai): support AI chat close float panel
- Introduced a new interface for WebAISettings, extending AISettings to include panel style options. - Updated default settings creation to include a fixed panel style. - Refactored AI chat panel visibility management, allowing dynamic visibility based on panel style. - Implemented a new hook for managing AI panel visibility and integrated it into the floating panel component. - Removed deprecated context bar search service and replaced it with a unified feed entry search service for improved search functionality. These changes collectively enhance the configurability and usability of the AI chat interface, providing users with better control over their settings and layout preferences. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -4,32 +4,38 @@ import type { AISettings } from "@follow/shared/settings/interface"
|
||||
import { jotaiStore } from "@follow/utils"
|
||||
import { atom, useAtomValue } from "jotai"
|
||||
|
||||
export const createDefaultSettings = (): AISettings => defaultAISettings
|
||||
export interface WebAISettings extends AISettings {
|
||||
panelStyle: AIChatPanelStyle
|
||||
}
|
||||
|
||||
export const createDefaultSettings = (): WebAISettings => ({
|
||||
...defaultAISettings,
|
||||
panelStyle: AIChatPanelStyle.Fixed,
|
||||
})
|
||||
|
||||
export const {
|
||||
useSettingKey: useAISettingKey,
|
||||
useSettingSelector: useAISettingSelector,
|
||||
setSetting: setAISetting,
|
||||
clearSettings: clearAISettings,
|
||||
initializeDefaultSettings: initializeDefaultAISettings,
|
||||
initializeDefaultSettings,
|
||||
getSettings: getAISettings,
|
||||
useSettingValue: useAISettingValue,
|
||||
settingAtom: __aiSettingAtom,
|
||||
} = createSettingAtom("ai", createDefaultSettings)
|
||||
export const aiServerSyncWhiteListKeys = []
|
||||
// Local Setting for ai
|
||||
|
||||
////////// AI Panel Style
|
||||
export enum AIChatPanelStyle {
|
||||
Fixed = "fixed",
|
||||
Floating = "floating",
|
||||
}
|
||||
|
||||
const aiChatPanelStyleAtom = atom<AIChatPanelStyle>(AIChatPanelStyle.Floating)
|
||||
export const useAIChatPanelStyle = () => useAtomValue(aiChatPanelStyleAtom)
|
||||
export const useAIChatPanelStyle = () => useAISettingKey("panelStyle")
|
||||
export const setAIChatPanelStyle = (style: AIChatPanelStyle) => {
|
||||
jotaiStore.set(aiChatPanelStyleAtom, style)
|
||||
setAISetting("panelStyle", style)
|
||||
}
|
||||
export const getAIChatPanelStyle = () => jotaiStore.get(aiChatPanelStyleAtom)
|
||||
export const getAIChatPanelStyle = () => getAISettings().panelStyle
|
||||
|
||||
// Floating panel state atoms
|
||||
interface FloatingPanelState {
|
||||
@@ -54,3 +60,18 @@ export const setFloatingPanelState = (state: Partial<FloatingPanelState>) => {
|
||||
jotaiStore.set(floatingPanelStateAtom, { ...currentState, ...state })
|
||||
}
|
||||
export const getFloatingPanelState = () => jotaiStore.get(floatingPanelStateAtom)
|
||||
|
||||
////////// AI Panel Visibility
|
||||
|
||||
const aiPanelVisibilityAtom = atom<boolean>(false)
|
||||
export const useAIPanelVisibility = () => useAtomValue(aiPanelVisibilityAtom)
|
||||
export const setAIPanelVisibility = (visibility: boolean) => {
|
||||
jotaiStore.set(aiPanelVisibilityAtom, visibility)
|
||||
}
|
||||
export const getAIPanelVisibility = () => jotaiStore.get(aiPanelVisibilityAtom)
|
||||
|
||||
//// Enhance Init Ai Settings
|
||||
export const initializeDefaultAISettings = () => {
|
||||
initializeDefaultSettings()
|
||||
if (getAISettings().panelStyle === AIChatPanelStyle.Fixed) setAIPanelVisibility(true)
|
||||
}
|
||||
|
||||
+8
-9
@@ -1,4 +1,4 @@
|
||||
import { cn } from "@follow/utils/utils"
|
||||
import { clsx, cn } from "@follow/utils/utils"
|
||||
import type { FC } from "react"
|
||||
import { memo } from "react"
|
||||
|
||||
@@ -171,16 +171,17 @@ export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block })
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"group relative flex h-7 max-w-[calc(50%-0.5rem)] flex-shrink-0 items-center gap-2 overflow-hidden rounded-lg px-2.5",
|
||||
"group relative flex h-7 min-w-0 max-w-[calc(50%-0.5rem)] flex-shrink-0 items-center gap-2 overflow-hidden rounded-lg px-2.5",
|
||||
"bg-fill-tertiary border-border border",
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={
|
||||
className={clsx(
|
||||
"min-w-0",
|
||||
canRemove
|
||||
? "min-w-0 group-hover:[mask-image:linear-gradient(to_right,black_0%,black_calc(100%-3rem),rgba(0,0,0,0.8)_calc(100%-2rem),rgba(0,0,0,0.3)_calc(100%-1rem),transparent_100%)]"
|
||||
: void 0
|
||||
}
|
||||
? "group-hover:[mask-image:linear-gradient(to_right,black_0%,black_calc(100%-3rem),rgba(0,0,0,0.8)_calc(100%-2rem),rgba(0,0,0,0.3)_calc(100%-1rem),transparent_100%)]"
|
||||
: void 0,
|
||||
)}
|
||||
>
|
||||
<div className="flex min-w-0 flex-1 items-center gap-1.5">
|
||||
<div className="flex items-center gap-1">
|
||||
@@ -188,9 +189,7 @@ export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block })
|
||||
<span className="text-text-tertiary text-xs font-medium">{getBlockLabel()}</span>
|
||||
</div>
|
||||
|
||||
<span className={cn("text-text min-w-0 flex-1 truncate text-xs")}>
|
||||
{getDisplayContent()}
|
||||
</span>
|
||||
<span className={"text-text min-w-0 flex-1 truncate text-xs"}>{getDisplayContent()}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { ActionButton } from "@follow/components/ui/button/index.js"
|
||||
import { useCallback } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { AIChatPanelStyle, setAIPanelVisibility, useAIChatPanelStyle } from "~/atoms/settings/ai"
|
||||
import { useDialog } from "~/components/ui/modal/stacked/hooks"
|
||||
import { useBlockActions, useChatActions, useCurrentTitle } from "~/modules/ai-chat/store/hooks"
|
||||
import { useSettingModal } from "~/modules/settings/modal/use-setting-modal-hack"
|
||||
@@ -30,7 +31,7 @@ export const ChatHeader = () => {
|
||||
variant: "danger",
|
||||
onConfirm: () => {
|
||||
chatActions.newChat()
|
||||
blockActions.clearBlocks()
|
||||
blockActions.clearBlocks({ keepSpecialTypes: true })
|
||||
},
|
||||
})
|
||||
}, [chatActions, currentTitle, ask, t, blockActions])
|
||||
@@ -42,6 +43,7 @@ export const ChatHeader = () => {
|
||||
[chatActions],
|
||||
)
|
||||
|
||||
const panelStyle = useAIChatPanelStyle()
|
||||
const maskImage = `linear-gradient(to bottom, black 0%, black 75%, transparent 100%)`
|
||||
return (
|
||||
<div className="absolute inset-x-0 top-0 z-[1] h-12">
|
||||
@@ -73,6 +75,15 @@ export const ChatHeader = () => {
|
||||
</ActionButton>
|
||||
|
||||
<ChatMoreDropdown />
|
||||
|
||||
{panelStyle === AIChatPanelStyle.Floating && (
|
||||
<>
|
||||
<div className="bg-border h-5 w-px" />
|
||||
<ActionButton tooltip="Close" onClick={() => setAIPanelVisibility(false)}>
|
||||
<i className="i-mgc-close-cute-re text-text-secondary size-5" />
|
||||
</ActionButton>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
-8
@@ -155,14 +155,6 @@ export const ChatMoreDropdown = () => {
|
||||
</div>
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
<DropdownMenuSeparator className="my-1.5" />
|
||||
<DropdownMenuItem
|
||||
onClick={chatActions.newChat}
|
||||
className="flex cursor-pointer items-center gap-2 rounded-md px-1 py-2.5"
|
||||
>
|
||||
<i className="i-mgc-add-cute-re size-4" />
|
||||
<span className="text-sm">New Chat</span>
|
||||
</DropdownMenuItem>
|
||||
</>
|
||||
) : (
|
||||
<div className="flex flex-col items-center py-8 text-center">
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
import { useMemo } from "react"
|
||||
|
||||
import { useFeedEntrySearchService } from "~/modules/ai-chat/services/feedEntrySearchService"
|
||||
import { useFeedEntrySearchService } from "~/modules/ai-chat/hooks/useFeedEntrySearchService"
|
||||
|
||||
import type { MentionData, MentionType } from "../types"
|
||||
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
import { useMemo } from "react"
|
||||
|
||||
import type { SearchItem } from "./feedEntrySearchService"
|
||||
import { useFeedEntrySearchService } from "./feedEntrySearchService"
|
||||
|
||||
/**
|
||||
* Picker item interface for context bar compatibility
|
||||
*/
|
||||
export interface PickerItem {
|
||||
id: string
|
||||
title: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook that provides search functionality for context bar
|
||||
* Uses the shared feed/entry search service and converts results to PickerItem format
|
||||
*/
|
||||
export const useContextBarSearchService = () => {
|
||||
const { search, feedItems, entryItems } = useFeedEntrySearchService({
|
||||
maxRecentEntries: 50,
|
||||
fuseOptions: {
|
||||
keys: ["title", "id"],
|
||||
threshold: 0.3,
|
||||
},
|
||||
})
|
||||
|
||||
// Convert search items to picker items
|
||||
const convertToPickerItems = useMemo(() => {
|
||||
return (items: SearchItem[]): PickerItem[] => {
|
||||
return items.map((item) => ({
|
||||
id: item.id,
|
||||
title: item.title,
|
||||
}))
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Search function for feeds
|
||||
const searchFeeds = useMemo(() => {
|
||||
return (query: string): PickerItem[] => {
|
||||
const results = search(query, "feed", 20)
|
||||
return convertToPickerItems(results)
|
||||
}
|
||||
}, [search, convertToPickerItems])
|
||||
|
||||
// Search function for entries
|
||||
const searchEntries = useMemo(() => {
|
||||
return (query: string): PickerItem[] => {
|
||||
const results = search(query, "entry", 20)
|
||||
return convertToPickerItems(results)
|
||||
}
|
||||
}, [search, convertToPickerItems])
|
||||
|
||||
// Get all feeds as picker items
|
||||
const allFeeds = useMemo(() => {
|
||||
return convertToPickerItems(feedItems)
|
||||
}, [feedItems, convertToPickerItems])
|
||||
|
||||
// Get all recent entries as picker items
|
||||
const allRecentEntries = useMemo(() => {
|
||||
return convertToPickerItems(entryItems)
|
||||
}, [entryItems, convertToPickerItems])
|
||||
|
||||
return {
|
||||
searchFeeds,
|
||||
searchEntries,
|
||||
allFeeds,
|
||||
allRecentEntries,
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ export class BlockSliceAction {
|
||||
}
|
||||
}
|
||||
|
||||
clearBlocks() {
|
||||
clearBlocks({ keepSpecialTypes = false }: { keepSpecialTypes?: boolean } = {}) {
|
||||
this.set(
|
||||
produce((state: BlockSlice) => {
|
||||
// Clean up file attachments before clearing
|
||||
@@ -107,7 +107,11 @@ export class BlockSliceAction {
|
||||
cleanupFileAttachment(block.attachment)
|
||||
}
|
||||
})
|
||||
state.blocks = []
|
||||
state.blocks = keepSpecialTypes
|
||||
? state.blocks.filter((b) =>
|
||||
Object.values(BlockSliceAction.SPECIAL_TYPES).includes(b.type),
|
||||
)
|
||||
: []
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Spring } from "@follow/components/constants/spring.js"
|
||||
import { cn, computeAdjustedTopLeftPosition } from "@follow/utils"
|
||||
import { m } from "motion/react"
|
||||
import { AnimatePresence, m } from "motion/react"
|
||||
import type { ResizeDirection } from "re-resizable"
|
||||
import { Resizable } from "re-resizable"
|
||||
import type { FC } from "react"
|
||||
@@ -9,6 +9,7 @@ import { useEffect, useRef } from "react"
|
||||
import {
|
||||
getFloatingPanelState,
|
||||
setFloatingPanelState,
|
||||
useAIPanelVisibility,
|
||||
useFloatingPanelState,
|
||||
} from "~/atoms/settings/ai"
|
||||
import { Focusable } from "~/components/common/Focusable"
|
||||
@@ -19,7 +20,7 @@ import { ChatInterface } from "~/modules/ai-chat/components/layouts/ChatInterfac
|
||||
export interface AIChatFloatingPanelProps
|
||||
extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {}
|
||||
|
||||
export const AIChatFloatingPanel: FC<AIChatFloatingPanelProps> = ({ className, ...props }) => {
|
||||
const AIChatFloatingPanelInner: FC<AIChatFloatingPanelProps> = ({ className, ...props }) => {
|
||||
const floatingState = useFloatingPanelState()
|
||||
|
||||
// Preserve right/bottom margins to keep panel anchored to bottom-right on window resize
|
||||
@@ -59,25 +60,11 @@ export const AIChatFloatingPanel: FC<AIChatFloatingPanelProps> = ({ className, .
|
||||
return () => window.removeEventListener("resize", handleWindowResize)
|
||||
}, [floatingState.width, floatingState.height])
|
||||
|
||||
const chatContent = (
|
||||
<Focusable
|
||||
scope={HotkeyScope.AIChat}
|
||||
className={cn(
|
||||
"bg-background relative flex h-full flex-col overflow-hidden rounded-lg border shadow-xl",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChatHeader />
|
||||
<ChatInterface />
|
||||
</Focusable>
|
||||
)
|
||||
|
||||
return (
|
||||
<m.div
|
||||
initial={{ scale: 0.8, opacity: 0 }}
|
||||
animate={{ scale: 1, opacity: 1 }}
|
||||
exit={{ scale: 0.8, opacity: 0 }}
|
||||
initial={{ scale: 0.92, y: 100, opacity: 0 }}
|
||||
animate={{ scale: 1, y: 0, opacity: 1 }}
|
||||
exit={{ scale: 0.92, y: 100, opacity: 0 }}
|
||||
transition={Spring.presets.smooth}
|
||||
className="fixed z-50"
|
||||
style={{ left: floatingState.x, top: floatingState.y }}
|
||||
@@ -102,9 +89,28 @@ export const AIChatFloatingPanel: FC<AIChatFloatingPanelProps> = ({ className, .
|
||||
topLeft: true,
|
||||
}}
|
||||
>
|
||||
{chatContent}
|
||||
<Focusable
|
||||
scope={HotkeyScope.AIChat}
|
||||
className={cn(
|
||||
"bg-background relative flex h-full flex-col overflow-hidden rounded-lg border shadow-xl",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChatHeader />
|
||||
<ChatInterface />
|
||||
</Focusable>
|
||||
</Resizable>
|
||||
</div>
|
||||
</m.div>
|
||||
)
|
||||
}
|
||||
|
||||
export const AIChatFloatingPanel: FC<AIChatFloatingPanelProps> = (props) => {
|
||||
const visibility = useAIPanelVisibility()
|
||||
return (
|
||||
<AnimatePresence>
|
||||
{visibility && <AIChatFloatingPanelInner key="ai-chat-floating-panel" {...props} />}
|
||||
</AnimatePresence>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ declare module "@follow/utils/event-bus" {
|
||||
"global:toggle-corner-play": void
|
||||
"global:quick-add": void
|
||||
"global:toggle-ai-chat": void
|
||||
"global:toggle-ai-chat-pinned": void
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,18 +71,6 @@ export const useRegisterGlobalCommands = () => {
|
||||
icon: aiIcon,
|
||||
category,
|
||||
},
|
||||
{
|
||||
id: COMMAND_ID.global.toggleAIChatPinned,
|
||||
label: {
|
||||
title: t("command.global.toggle_ai_chat_pinned.title"),
|
||||
description: t("command.global.toggle_ai_chat_pinned.description"),
|
||||
},
|
||||
run: () => {
|
||||
EventBus.dispatch(COMMAND_ID.global.toggleAIChatPinned)
|
||||
},
|
||||
icon: aiIcon,
|
||||
category,
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
@@ -107,14 +94,8 @@ export type ToggleAIChatCommand = Command<{
|
||||
fn: (ctx?: { entryId?: string }) => void
|
||||
}>
|
||||
|
||||
export type ToggleAIChatPinnedCommand = Command<{
|
||||
id: typeof COMMAND_ID.global.toggleAIChatPinned
|
||||
fn: (ctx?: { entryId?: string }) => void
|
||||
}>
|
||||
|
||||
export type GlobalCommand =
|
||||
| ShowShortcutsCommand
|
||||
| ToggleCornerPlayCommand
|
||||
| QuickAddCommand
|
||||
| ToggleAIChatCommand
|
||||
| ToggleAIChatPinnedCommand
|
||||
|
||||
@@ -50,7 +50,6 @@ export const COMMAND_ID = {
|
||||
toggleCornerPlay: "global:toggle-corner-play",
|
||||
quickAdd: "global:quick-add",
|
||||
toggleAIChat: "global:toggle-ai-chat",
|
||||
toggleAIChatPinned: "global:toggle-ai-chat-pinned",
|
||||
},
|
||||
layout: {
|
||||
toggleSubscriptionColumn: "layout:toggle-subscription-column",
|
||||
|
||||
@@ -62,7 +62,7 @@ const warningActionButton: Partial<
|
||||
show: !IN_ELECTRON,
|
||||
info: "entry_actions.warn_info_for_desktop",
|
||||
},
|
||||
[COMMAND_ID.global.toggleAIChatPinned]: {
|
||||
[COMMAND_ID.global.toggleAIChat]: {
|
||||
show: true,
|
||||
info: "entry_actions.warn_info_ai_chat_pinned_tip",
|
||||
},
|
||||
|
||||
+2
-3
@@ -3,13 +3,12 @@ import { CircleProgress } from "@follow/components/icons/Progress.js"
|
||||
import { MotionButtonBase } from "@follow/components/ui/button/index.js"
|
||||
import { RootPortal } from "@follow/components/ui/portal/index.jsx"
|
||||
import { useScrollViewElement } from "@follow/components/ui/scroll-area/hooks.js"
|
||||
import { EventBus } from "@follow/utils/event-bus"
|
||||
import { springScrollTo } from "@follow/utils/scroller"
|
||||
import { cn } from "@follow/utils/utils"
|
||||
import { useStore } from "jotai"
|
||||
import { memo, useEffect, useMemo, useState } from "react"
|
||||
|
||||
import { AIChatPanelStyle, useAIChatPanelStyle } from "~/atoms/settings/ai"
|
||||
import { AIChatPanelStyle, setAIPanelVisibility, useAIChatPanelStyle } from "~/atoms/settings/ai"
|
||||
import type { TocRef } from "~/components/ui/markdown/components/Toc"
|
||||
import { Toc } from "~/components/ui/markdown/components/Toc"
|
||||
import { useFeature } from "~/hooks/biz/useFeature"
|
||||
@@ -69,7 +68,7 @@ const BackTopIndicator: Component = memo(({ className }) => {
|
||||
{aiEnabled && !isAiPanelOpen && (
|
||||
<MotionButtonBase
|
||||
onClick={() => {
|
||||
EventBus.dispatch("global:toggle-ai-chat-pinned")
|
||||
setAIPanelVisibility(true)
|
||||
}}
|
||||
className={cn(
|
||||
"mt-1 flex flex-nowrap items-center gap-2 text-sm opacity-50 transition-all duration-500 hover:opacity-100",
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
"command.global.show_shortcuts.title": "Show Shortcuts",
|
||||
"command.global.toggle_ai_chat.description": "Toggle the AI chat panel",
|
||||
"command.global.toggle_ai_chat.title": "AI Chat",
|
||||
"command.global.toggle_ai_chat_pinned.description": "Toggle the AI chat panel pinned",
|
||||
"command.global.toggle_ai_chat_pinned.title": "AI Chat(Pinned)",
|
||||
"command.global.toggle_corner_play.description": "Play/Pause playing status(If there is currently playing audio in the background)",
|
||||
"command.global.toggle_corner_play.title": "Toggle Corner Play",
|
||||
"command.layout.focus_to_entry_render.title": "Focus to Entry Render",
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
"command.global.show_shortcuts.title": "ショートカットを表示",
|
||||
"command.global.toggle_ai_chat.description": "AI チャットパネルを切り替えます",
|
||||
"command.global.toggle_ai_chat.title": "AI チャット",
|
||||
"command.global.toggle_ai_chat_pinned.description": "AI チャットパネルをピン留めします",
|
||||
"command.global.toggle_ai_chat_pinned.title": "AI チャット (ピン留め)",
|
||||
"command.global.toggle_corner_play.description": "再生中のオーディオがある場合、再生/一時停止の状態を切り替えます",
|
||||
"command.global.toggle_corner_play.title": "コーナー再生の切り替え",
|
||||
"command.layout.focus_to_entry_render.title": "エントリー レンダリングにフォーカス",
|
||||
|
||||
Reference in New Issue
Block a user