@sveda-ai/react 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/LICENSE +557 -0
  2. package/README.md +18 -0
  3. package/dist/appearance.d.ts +12 -0
  4. package/dist/appearance.d.ts.map +1 -0
  5. package/dist/components/SvedaChat.d.ts +14 -0
  6. package/dist/components/SvedaChat.d.ts.map +1 -0
  7. package/dist/components/shell/ChatHistoryList.d.ts +10 -0
  8. package/dist/components/shell/ChatHistoryList.d.ts.map +1 -0
  9. package/dist/components/shell/ChatMessageList.d.ts +7 -0
  10. package/dist/components/shell/ChatMessageList.d.ts.map +1 -0
  11. package/dist/components/shell/SvedaComposer.d.ts +11 -0
  12. package/dist/components/shell/SvedaComposer.d.ts.map +1 -0
  13. package/dist/components/shell/SvedaFrameTicks.d.ts +2 -0
  14. package/dist/components/shell/SvedaFrameTicks.d.ts.map +1 -0
  15. package/dist/components/shell/SvedaMinimizedTrigger.d.ts +9 -0
  16. package/dist/components/shell/SvedaMinimizedTrigger.d.ts.map +1 -0
  17. package/dist/components/shell/SvedaResizeHandles.d.ts +9 -0
  18. package/dist/components/shell/SvedaResizeHandles.d.ts.map +1 -0
  19. package/dist/components/shell/SvedaTabs.d.ts +13 -0
  20. package/dist/components/shell/SvedaTabs.d.ts.map +1 -0
  21. package/dist/components/shell/SvedaToolbar.d.ts +21 -0
  22. package/dist/components/shell/SvedaToolbar.d.ts.map +1 -0
  23. package/dist/hooks/useSvedaChat.d.ts +33 -0
  24. package/dist/hooks/useSvedaChat.d.ts.map +1 -0
  25. package/dist/hooks/useSvedaChatLayout.d.ts +25 -0
  26. package/dist/hooks/useSvedaChatLayout.d.ts.map +1 -0
  27. package/dist/hooks/useSvedaStreaming.d.ts +43 -0
  28. package/dist/hooks/useSvedaStreaming.d.ts.map +1 -0
  29. package/dist/i18n.d.ts +14 -0
  30. package/dist/i18n.d.ts.map +1 -0
  31. package/dist/index.d.ts +25 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +1238 -0
  34. package/dist/launcherIcons.d.ts +5 -0
  35. package/dist/launcherIcons.d.ts.map +1 -0
  36. package/dist/lib/utils.d.ts +3 -0
  37. package/dist/lib/utils.d.ts.map +1 -0
  38. package/dist/provider.d.ts +15 -0
  39. package/dist/provider.d.ts.map +1 -0
  40. package/dist/styles.css +3 -0
  41. package/dist/types.d.ts +58 -0
  42. package/dist/types.d.ts.map +1 -0
  43. package/package.json +74 -0
  44. package/src/appearance.ts +81 -0
  45. package/src/components/SvedaChat.tsx +428 -0
  46. package/src/components/shell/ChatHistoryList.tsx +151 -0
  47. package/src/components/shell/ChatMessageList.tsx +52 -0
  48. package/src/components/shell/SvedaComposer.tsx +100 -0
  49. package/src/components/shell/SvedaFrameTicks.tsx +22 -0
  50. package/src/components/shell/SvedaMinimizedTrigger.tsx +41 -0
  51. package/src/components/shell/SvedaResizeHandles.tsx +51 -0
  52. package/src/components/shell/SvedaTabs.tsx +106 -0
  53. package/src/components/shell/SvedaToolbar.tsx +132 -0
  54. package/src/hooks/useSvedaChat.ts +91 -0
  55. package/src/hooks/useSvedaChatLayout.ts +281 -0
  56. package/src/hooks/useSvedaStreaming.ts +139 -0
  57. package/src/i18n.ts +51 -0
  58. package/src/index.ts +116 -0
  59. package/src/launcherIcons.ts +43 -0
  60. package/src/lib/utils.ts +6 -0
  61. package/src/markdown.css +96 -0
  62. package/src/provider.tsx +179 -0
  63. package/src/styles.css +6 -0
  64. package/src/theme.css +137 -0
  65. package/src/types.ts +70 -0
@@ -0,0 +1,100 @@
1
+ import { Loader2, Send, Square } from 'lucide-react';
2
+ import { useEffect, useRef, type KeyboardEvent } from 'react';
3
+ import { useSvedaT } from '../../provider';
4
+ import { cn } from '../../lib/utils';
5
+
6
+ export interface SvedaComposerProps {
7
+ value: string;
8
+ onChange: (value: string) => void;
9
+ onSend: () => void;
10
+ onStop?: () => void;
11
+ isLoading?: boolean;
12
+ isStreaming?: boolean;
13
+ disabled?: boolean;
14
+ }
15
+
16
+ export function SvedaComposer({
17
+ value,
18
+ onChange,
19
+ onSend,
20
+ onStop,
21
+ isLoading = false,
22
+ isStreaming = false,
23
+ disabled = false,
24
+ }: SvedaComposerProps) {
25
+ const t = useSvedaT();
26
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
27
+ const composerDisabled = disabled || isLoading;
28
+ const canSend = value.trim().length > 0;
29
+
30
+ useEffect(() => {
31
+ const el = textareaRef.current;
32
+ if (!el) {
33
+ return;
34
+ }
35
+ el.style.height = '60px';
36
+ const next = Math.min(Math.max(el.scrollHeight, 60), 250);
37
+ el.style.height = `${next}px`;
38
+ }, [value]);
39
+
40
+ const handleKeyPress = (event: KeyboardEvent<HTMLTextAreaElement>) => {
41
+ if (event.key === 'Enter' && !event.shiftKey) {
42
+ event.preventDefault();
43
+ if (!composerDisabled && canSend && !isStreaming) {
44
+ onSend();
45
+ }
46
+ }
47
+ };
48
+
49
+ return (
50
+ <div className="flex flex-shrink-0 flex-col bg-background p-3">
51
+ <div className="sveda-chat-frame relative flex flex-col bg-background transition-colors">
52
+ <textarea
53
+ ref={textareaRef}
54
+ value={value}
55
+ onChange={(event) => onChange(event.target.value)}
56
+ onKeyPress={handleKeyPress}
57
+ placeholder={t('typeMessage')}
58
+ disabled={composerDisabled}
59
+ className="sveda-chat-input-textarea max-h-[250px] min-h-[60px] w-full resize-none border-0 bg-transparent p-3 font-sans text-sm text-foreground shadow-none !outline-none !ring-0 placeholder:font-mono placeholder:text-[11px] placeholder:tracking-[0.08em] placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-offset-0"
60
+ style={{ height: 60, outline: 'none', boxShadow: 'none' }}
61
+ data-testid="sveda-composer"
62
+ />
63
+
64
+ <div className="flex items-center justify-between gap-2 border-t border-border/40 px-2 py-1.5">
65
+ <div className="min-w-0 flex-1" />
66
+ <div className="flex items-center gap-1">
67
+ {isStreaming ? (
68
+ <button
69
+ type="button"
70
+ className="inline-flex h-9 w-9 items-center justify-center text-muted-foreground hover:text-foreground"
71
+ onClick={onStop}
72
+ aria-label="Stop"
73
+ >
74
+ <Square className="h-4 w-4" />
75
+ </button>
76
+ ) : (
77
+ <button
78
+ type="button"
79
+ className={cn(
80
+ 'inline-flex h-9 w-9 items-center justify-center text-muted-foreground hover:text-foreground',
81
+ composerDisabled || !canSend ? 'opacity-40' : ''
82
+ )}
83
+ disabled={composerDisabled || !canSend}
84
+ onClick={onSend}
85
+ aria-label="Send"
86
+ data-testid="sveda-send"
87
+ >
88
+ {isLoading ? (
89
+ <Loader2 className="h-4 w-4 animate-spin" />
90
+ ) : (
91
+ <Send className="h-4 w-4" />
92
+ )}
93
+ </button>
94
+ )}
95
+ </div>
96
+ </div>
97
+ </div>
98
+ </div>
99
+ );
100
+ }
@@ -0,0 +1,22 @@
1
+ export function SvedaFrameTicks() {
2
+ return (
3
+ <>
4
+ <span
5
+ className="pointer-events-none absolute -left-px -top-px z-10 h-2.5 w-2.5 border-l border-t border-foreground"
6
+ aria-hidden="true"
7
+ />
8
+ <span
9
+ className="pointer-events-none absolute -right-px -top-px z-10 h-2.5 w-2.5 border-r border-t border-foreground"
10
+ aria-hidden="true"
11
+ />
12
+ <span
13
+ className="pointer-events-none absolute -bottom-px -left-px z-10 h-2.5 w-2.5 border-b border-l border-foreground"
14
+ aria-hidden="true"
15
+ />
16
+ <span
17
+ className="pointer-events-none absolute -bottom-px -right-px z-10 h-2.5 w-2.5 border-b border-r border-foreground"
18
+ aria-hidden="true"
19
+ />
20
+ </>
21
+ );
22
+ }
@@ -0,0 +1,41 @@
1
+ import type { LucideIcon } from 'lucide-react';
2
+ import { svedaLauncherIconComponent } from '../../launcherIcons';
3
+ import { cn } from '../../lib/utils';
4
+
5
+ const triggerClass =
6
+ 'inline-flex h-auto min-w-0 items-center justify-center gap-2 rounded-none border border-primary bg-primary px-3.5 py-2 font-mono text-[11px] tracking-[0.14em] text-primary-foreground shadow-none transition-opacity hover:opacity-90 [&_svg]:!h-3.5 [&_svg]:!w-3.5';
7
+
8
+ export interface SvedaMinimizedTriggerProps {
9
+ label: string;
10
+ icon?: string;
11
+ logoSrc?: string;
12
+ logoAlt?: string;
13
+ onOpen: () => void;
14
+ }
15
+
16
+ export function SvedaMinimizedTrigger({
17
+ label,
18
+ icon = 'sparkles',
19
+ logoSrc = '',
20
+ logoAlt = '',
21
+ onOpen,
22
+ }: SvedaMinimizedTriggerProps) {
23
+ const Icon: LucideIcon = svedaLauncherIconComponent(icon);
24
+
25
+ return (
26
+ <button type="button" className={cn(triggerClass)} onClick={onOpen} data-testid="sveda-launcher">
27
+ {logoSrc ? (
28
+ <img
29
+ src={logoSrc}
30
+ alt={logoAlt}
31
+ className="h-6 w-6 flex-shrink-0 object-cover min-[1872px]:h-10 min-[1872px]:w-10"
32
+ />
33
+ ) : (
34
+ <Icon className="h-6 w-6 flex-shrink-0" />
35
+ )}
36
+ {label ? (
37
+ <span className="font-mono text-[11px] font-normal tracking-[0.14em]">{label}</span>
38
+ ) : null}
39
+ </button>
40
+ );
41
+ }
@@ -0,0 +1,51 @@
1
+ import type { PointerEvent } from 'react';
2
+ import type { SvedaChatViewMode } from '../../hooks/useSvedaChatLayout';
3
+
4
+ export interface SvedaResizeHandlesProps {
5
+ isMobile: boolean;
6
+ viewMode: SvedaChatViewMode;
7
+ onStartResize: (direction: string, event: PointerEvent<HTMLElement>) => void;
8
+ }
9
+
10
+ export function SvedaResizeHandles({ isMobile, viewMode, onStartResize }: SvedaResizeHandlesProps) {
11
+ if (isMobile || (viewMode !== 'fixed' && viewMode !== 'floating')) {
12
+ return null;
13
+ }
14
+
15
+ if (viewMode === 'fixed') {
16
+ return (
17
+ <div
18
+ className="absolute bottom-3 left-0 top-3 z-10 w-1 touch-none cursor-ew-resize hover:bg-primary/30"
19
+ role="separator"
20
+ aria-orientation="vertical"
21
+ aria-label="Resize chat"
22
+ onPointerDown={(event) => onStartResize('fixed-left', event)}
23
+ />
24
+ );
25
+ }
26
+
27
+ return (
28
+ <>
29
+ <div
30
+ className="absolute left-0 top-0 z-10 h-3 w-3 touch-none cursor-nw-resize rounded-br hover:bg-primary/20"
31
+ role="separator"
32
+ aria-label="Resize chat"
33
+ onPointerDown={(event) => onStartResize('top-left', event)}
34
+ />
35
+ <div
36
+ className="absolute left-3 right-3 top-0 z-10 h-1 touch-none cursor-n-resize hover:bg-primary/30"
37
+ role="separator"
38
+ aria-orientation="horizontal"
39
+ aria-label="Resize chat height"
40
+ onPointerDown={(event) => onStartResize('top', event)}
41
+ />
42
+ <div
43
+ className="absolute bottom-3 left-0 top-3 z-10 w-1 touch-none cursor-w-resize hover:bg-primary/30"
44
+ role="separator"
45
+ aria-orientation="vertical"
46
+ aria-label="Resize chat width"
47
+ onPointerDown={(event) => onStartResize('left', event)}
48
+ />
49
+ </>
50
+ );
51
+ }
@@ -0,0 +1,106 @@
1
+ import { buildChatTabTitleMap, type SvedaTabChat } from '@sveda-ai/chat';
2
+ import { Loader2, Plus, X } from 'lucide-react';
3
+ import { useMemo } from 'react';
4
+ import { useSvedaT } from '../../provider';
5
+ import { cn } from '../../lib/utils';
6
+
7
+ export interface SvedaTabsProps {
8
+ tabs: SvedaTabChat[];
9
+ currentChatId?: string | null;
10
+ streamingChatIds?: Set<string>;
11
+ unreadChatIds?: Set<string>;
12
+ compact?: boolean;
13
+ onSelectChat: (chatId: string) => void;
14
+ onCloseTab: (chatId: string) => void;
15
+ onNewChat: () => void;
16
+ }
17
+
18
+ export function SvedaTabs({
19
+ tabs,
20
+ currentChatId = null,
21
+ streamingChatIds = new Set(),
22
+ unreadChatIds = new Set(),
23
+ compact = false,
24
+ onSelectChat,
25
+ onCloseTab,
26
+ onNewChat,
27
+ }: SvedaTabsProps) {
28
+ const t = useSvedaT();
29
+ const visibleTabs = useMemo(() => tabs.filter((tab) => tab?.id), [tabs]);
30
+ const tabTitles = useMemo(() => buildChatTabTitleMap(visibleTabs, t), [visibleTabs, t]);
31
+
32
+ if (visibleTabs.length === 0) {
33
+ return null;
34
+ }
35
+
36
+ const isStreaming = (chatId: string) => streamingChatIds.has(chatId);
37
+ const hasUnread = (chatId: string) => !isStreaming(chatId) && unreadChatIds.has(chatId);
38
+
39
+ return (
40
+ <div
41
+ className={cn(
42
+ 'flex shrink-0 items-stretch border-b border-border bg-card',
43
+ compact ? 'px-3' : 'px-4 md:px-6'
44
+ )}
45
+ >
46
+ <div className="min-w-0 flex-1 overflow-x-auto">
47
+ <div className="flex h-12 min-w-max items-end gap-px px-1">
48
+ {visibleTabs.map((tab) => (
49
+ <div
50
+ key={tab.id}
51
+ role="button"
52
+ tabIndex={0}
53
+ className={cn(
54
+ 'inline-flex h-10 max-w-56 shrink-0 items-center gap-1.5 whitespace-nowrap border-b border-transparent px-3 py-2 font-mono text-[11px] tracking-[0.12em] text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none',
55
+ currentChatId === tab.id ? 'border-foreground bg-transparent text-foreground' : ''
56
+ )}
57
+ onClick={() => onSelectChat(tab.id)}
58
+ onKeyDown={(event) => {
59
+ if (event.key === 'Enter' || event.key === ' ') {
60
+ event.preventDefault();
61
+ onSelectChat(tab.id);
62
+ }
63
+ }}
64
+ >
65
+ {isStreaming(tab.id) ? (
66
+ <Loader2
67
+ className="h-3.5 w-3.5 shrink-0 animate-spin text-primary"
68
+ aria-label={t('chatTabRunning')}
69
+ />
70
+ ) : hasUnread(tab.id) ? (
71
+ <span
72
+ className="h-2 w-2 shrink-0 rounded-full bg-primary"
73
+ aria-label={t('chatTabUnread')}
74
+ />
75
+ ) : null}
76
+ <span className="min-w-0 truncate">{tabTitles.get(tab.id)}</span>
77
+ <button
78
+ type="button"
79
+ className="ml-0.5 rounded-sm p-0.5 text-muted-foreground opacity-70 hover:bg-background/80 hover:text-foreground hover:opacity-100"
80
+ aria-label={t('closeChatTab')}
81
+ onMouseDown={(event) => event.stopPropagation()}
82
+ onClick={(event) => {
83
+ event.stopPropagation();
84
+ onCloseTab(tab.id);
85
+ }}
86
+ >
87
+ <X className="h-3 w-3" />
88
+ </button>
89
+ </div>
90
+ ))}
91
+ </div>
92
+ </div>
93
+
94
+ <div className="flex shrink-0 items-center border-l border-border/40 pl-1">
95
+ <button
96
+ type="button"
97
+ className="inline-flex h-9 w-9 shrink-0 items-center justify-center text-muted-foreground hover:text-foreground"
98
+ aria-label={t('newChat')}
99
+ onClick={onNewChat}
100
+ >
101
+ <Plus className="h-4 w-4" />
102
+ </button>
103
+ </div>
104
+ </div>
105
+ );
106
+ }
@@ -0,0 +1,132 @@
1
+ import { History, Maximize2, MessageSquare, Minimize2, PanelRight, Sparkles, X } from 'lucide-react';
2
+ import { useSvedaT } from '../../provider';
3
+ import { cn } from '../../lib/utils';
4
+ import type { SvedaChatViewMode } from '../../hooks/useSvedaChatLayout';
5
+
6
+ const iconButtonClass =
7
+ 'inline-flex h-9 w-9 items-center justify-center bg-transparent text-muted-foreground hover:text-foreground';
8
+
9
+ export interface SvedaToolbarProps {
10
+ title: string;
11
+ isMobile: boolean;
12
+ viewMode: SvedaChatViewMode;
13
+ isImmersiveDesktop: boolean;
14
+ showHistorySidebar: boolean;
15
+ viewModeFloatingLabel: string;
16
+ viewModeFixedLabel: string;
17
+ immersiveEnterLabel: string;
18
+ immersiveExitLabel: string;
19
+ immersiveBrandName?: string;
20
+ immersiveBrandLogo?: string;
21
+ onToggleHistory: () => void;
22
+ onEnterImmersive: () => void;
23
+ onExitImmersive: () => void;
24
+ onToggleViewMode: () => void;
25
+ onMinimize: () => void;
26
+ }
27
+
28
+ export function SvedaToolbar({
29
+ title,
30
+ isMobile,
31
+ viewMode,
32
+ isImmersiveDesktop,
33
+ showHistorySidebar,
34
+ viewModeFloatingLabel,
35
+ viewModeFixedLabel,
36
+ immersiveEnterLabel,
37
+ immersiveExitLabel,
38
+ immersiveBrandName = '',
39
+ immersiveBrandLogo = '',
40
+ onToggleHistory,
41
+ onEnterImmersive,
42
+ onExitImmersive,
43
+ onToggleViewMode,
44
+ onMinimize,
45
+ }: SvedaToolbarProps) {
46
+ const t = useSvedaT();
47
+ const headerClass = cn(
48
+ 'flex h-12 shrink-0 flex-row items-center justify-between border-b border-border bg-card px-3',
49
+ viewMode === 'floating' && !isMobile ? 'rounded-t-[var(--sveda-radius)]' : 'rounded-none'
50
+ );
51
+
52
+ return (
53
+ <header className={headerClass}>
54
+ {!isImmersiveDesktop ? (
55
+ <div className="flex min-w-0 flex-1 items-center gap-2">
56
+ <button
57
+ type="button"
58
+ className={cn(iconButtonClass, showHistorySidebar ? 'bg-muted/60 text-foreground' : '')}
59
+ aria-label={t('chatHistory')}
60
+ title={t('chatHistory')}
61
+ onClick={onToggleHistory}
62
+ >
63
+ <History className="h-4 w-4" />
64
+ </button>
65
+ <h2 className="min-w-0 flex-1 truncate text-left font-mono text-[11px] font-normal uppercase tracking-[0.14em] text-foreground">
66
+ {title}
67
+ </h2>
68
+ </div>
69
+ ) : (
70
+ <div className="flex min-w-0 flex-1 items-center gap-4">
71
+ <div className="w-[min(20rem,33vw)] min-w-0">
72
+ <div className="flex items-center gap-2">
73
+ {immersiveBrandLogo ? (
74
+ <img src={immersiveBrandLogo} alt={immersiveBrandName} className="size-7 shrink-0 rounded" />
75
+ ) : (
76
+ <Sparkles className="size-5 shrink-0 text-primary" />
77
+ )}
78
+ <span className="truncate text-lg font-bold leading-none text-foreground">{immersiveBrandName}</span>
79
+ </div>
80
+ </div>
81
+ <h2 className="min-w-0 flex-1 truncate text-left font-mono text-[11px] font-normal uppercase tracking-[0.14em] text-foreground">
82
+ {title}
83
+ </h2>
84
+ </div>
85
+ )}
86
+ <div className="flex shrink-0 items-center gap-0.5">
87
+ {!isMobile && viewMode === 'immersive' ? (
88
+ <button
89
+ type="button"
90
+ className={iconButtonClass}
91
+ aria-label={immersiveExitLabel}
92
+ title={immersiveExitLabel}
93
+ onClick={onExitImmersive}
94
+ >
95
+ <Minimize2 className="h-4 w-4" />
96
+ </button>
97
+ ) : null}
98
+ {!isMobile && viewMode !== 'immersive' ? (
99
+ <button
100
+ type="button"
101
+ className={iconButtonClass}
102
+ aria-label={immersiveEnterLabel}
103
+ title={immersiveEnterLabel}
104
+ onClick={onEnterImmersive}
105
+ >
106
+ <Maximize2 className="h-4 w-4" />
107
+ </button>
108
+ ) : null}
109
+ {!isMobile && viewMode !== 'immersive' ? (
110
+ <button
111
+ type="button"
112
+ className={iconButtonClass}
113
+ aria-label={viewMode === 'floating' ? viewModeFixedLabel : viewModeFloatingLabel}
114
+ title={viewMode === 'floating' ? viewModeFixedLabel : viewModeFloatingLabel}
115
+ onClick={onToggleViewMode}
116
+ >
117
+ {viewMode === 'floating' ? <PanelRight className="h-4 w-4" /> : <MessageSquare className="h-4 w-4" />}
118
+ </button>
119
+ ) : null}
120
+ <button
121
+ type="button"
122
+ className={iconButtonClass}
123
+ aria-label={t('toolbarMinimize')}
124
+ title={t('toolbarMinimize')}
125
+ onClick={onMinimize}
126
+ >
127
+ <X className="h-4 w-4" />
128
+ </button>
129
+ </div>
130
+ </header>
131
+ );
132
+ }
@@ -0,0 +1,91 @@
1
+ import {
2
+ getSvedaChatStore,
3
+ type SvedaChatHistory,
4
+ type SvedaChatState,
5
+ type SvedaChatStoreSnapshot,
6
+ } from '@sveda-ai/chat';
7
+ import { useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';
8
+ import { useSvedaContext, useSvedaT } from '../provider';
9
+ import { SVEDA_EMBED_AUTH_EVENT } from '../types';
10
+
11
+ export type { SvedaChatHistory, SvedaChatState };
12
+
13
+ const store = getSvedaChatStore({ embedAuthEvent: SVEDA_EMBED_AUTH_EVENT });
14
+
15
+ let initialized = false;
16
+ let cachedSnapshot: SvedaChatStoreSnapshot = store.getState();
17
+
18
+ const subscribe = (onStoreChange: () => void) =>
19
+ store.subscribe(() => {
20
+ cachedSnapshot = store.getState();
21
+ onStoreChange();
22
+ });
23
+
24
+ const getSnapshot = () => cachedSnapshot;
25
+
26
+ export function useSvedaChat() {
27
+ const { client } = useSvedaContext();
28
+ const t = useSvedaT();
29
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
30
+ const translateRef = useRef(t);
31
+ translateRef.current = t;
32
+
33
+ const translate = useCallback((key: string) => translateRef.current(key), []);
34
+
35
+ useEffect(() => {
36
+ store.setClient(client);
37
+ store.setTranslate(translate);
38
+
39
+ if (!initialized) {
40
+ initialized = true;
41
+ store.initAuthListener();
42
+ void store.loadHistories();
43
+ }
44
+ }, [client, translate]);
45
+
46
+ const sortedHistories = useMemo(() => store.sortedHistories(), [snapshot]);
47
+ const openChatTabs = useMemo(() => store.openChatTabs(), [snapshot]);
48
+
49
+ const createNewChat = useCallback(() => store.createNewChat(translate), [translate]);
50
+ const setCurrentChat = useCallback(
51
+ (chatId: string) => store.setCurrentChat(chatId, translate),
52
+ [translate]
53
+ );
54
+ const deleteChat = useCallback((chatId: string) => store.deleteChat(chatId, translate), [translate]);
55
+ const closeChatTab = useCallback(
56
+ (chatId: string) => store.closeChatTab(chatId, translate),
57
+ [translate]
58
+ );
59
+ const openChat = useCallback(() => store.openChat(translate), [translate]);
60
+
61
+ return {
62
+ chatState: snapshot.chatState,
63
+ currentChat: snapshot.currentChat,
64
+ chatHistories: snapshot.chatHistories,
65
+ sortedHistories,
66
+ openChatTabs,
67
+ isOpen: snapshot.chatState.isOpen,
68
+ isMinimized: snapshot.chatState.isMinimized,
69
+ isLoading: snapshot.chatState.isLoading,
70
+ isLoadingChatHistory: snapshot.chatState.isLoadingChatHistory,
71
+ hasCurrentChat: Boolean(snapshot.currentChat),
72
+ loadHistories: store.loadHistories.bind(store),
73
+ createNewChat,
74
+ setCurrentChat,
75
+ addMessage: store.addMessage.bind(store),
76
+ setChatTitle: store.setChatTitle.bind(store),
77
+ refreshChatTitleFromServer: store.refreshChatTitleFromServer.bind(store),
78
+ updateMessage: store.updateMessage.bind(store),
79
+ setChatMessages: store.setChatMessages.bind(store),
80
+ incrementChatTokens: store.incrementChatTokens.bind(store),
81
+ setChatContextWindowTokens: store.setChatContextWindowTokens.bind(store),
82
+ deleteChat,
83
+ renameChat: store.renameChat.bind(store),
84
+ closeChatTab,
85
+ openChat,
86
+ closeChat: store.closeChat.bind(store),
87
+ minimizeChat: store.minimizeChat.bind(store),
88
+ maximizeChat: store.maximizeChat.bind(store),
89
+ setLoading: store.setLoading.bind(store),
90
+ };
91
+ }