@@ -5,7 +5,14 @@ import { toast } from 'sonner';
55import { View , ViewLayout } from '@/application/types' ;
66import { ReactComponent as UploadIcon } from '@/assets/icons/upload.svg' ;
77import { ViewIcon } from '@/components/_shared/view-icon' ;
8- import { useAIEnabled , useAppOperations , useOpenPageModal , useToView } from '@/components/app/app.hooks' ;
8+ import { buildInitialAIChatSettings } from '@/components/ai-chat/chat-settings' ;
9+ import {
10+ useAIEnabled ,
11+ useAppOperations ,
12+ useCurrentWorkspaceId ,
13+ useOpenPageModal ,
14+ useToView ,
15+ } from '@/components/app/app.hooks' ;
916import { DropdownMenuGroup , DropdownMenuItem } from '@/components/ui/dropdown-menu' ;
1017import { Tooltip , TooltipContent , TooltipTrigger } from '@/components/ui/tooltip' ;
1118
@@ -15,31 +22,63 @@ function AddPageActions({ view, onImportClick }: { view: View; onImportClick?: (
1522 const openPageModal = useOpenPageModal ( ) ;
1623 const toView = useToView ( ) ;
1724 const aiEnabled = useAIEnabled ( ) ;
25+ const currentWorkspaceId = useCurrentWorkspaceId ( ) ;
1826 const lastChildViewId = view . children ?. [ view . children . length - 1 ] ?. view_id ;
1927
2028 const handleAddPage = useCallback (
2129 async ( layout : ViewLayout , name ?: string ) => {
2230 if ( ! addPage ) return ;
2331 if ( layout === ViewLayout . AIChat && ! aiEnabled ) return ;
24- toast . loading ( t ( 'document.creating' ) ) ;
32+
33+ const loadingToastId = toast . loading ( t ( 'document.creating' ) ) ;
34+
2535 try {
2636 // Append after the last child so the new page appears at the bottom.
2737 // When prev_view_id is omitted the backend prepends (inserts at index 0).
2838 const response = await addPage ( view . view_id , { layout, name, prev_view_id : lastChildViewId } ) ;
2939
40+ if ( layout === ViewLayout . AIChat && currentWorkspaceId ) {
41+ const initialSettings = buildInitialAIChatSettings ( { parent : view } ) ;
42+
43+ if ( Object . keys ( initialSettings ) . length > 0 ) {
44+ try {
45+ const [ { ChatRequest } , { getAxiosInstance } ] = await Promise . all ( [
46+ import ( '@/components/chat/request' ) ,
47+ import ( '@/application/services/js-services/http' ) ,
48+ ] ) ;
49+ const axiosInstance = getAxiosInstance ( ) ;
50+
51+ if ( ! axiosInstance ) {
52+ throw new Error ( 'Missing axios instance' ) ;
53+ }
54+
55+ const request = new ChatRequest ( currentWorkspaceId , response . view_id , axiosInstance ) ;
56+
57+ await request . updateChatSettings ( initialSettings ) ;
58+ } catch {
59+ toast . error (
60+ t ( 'search.updateAIChatSettingsFailed' , {
61+ defaultValue : 'AI chat was created, but the context could not be attached' ,
62+ } )
63+ ) ;
64+ }
65+ }
66+ }
67+
3068 if ( layout === ViewLayout . Document ) {
3169 void openPageModal ?.( response . view_id ) ;
3270 } else {
3371 void toView ( response . view_id ) ;
3472 }
3573
36- toast . dismiss ( ) ;
74+ toast . dismiss ( loadingToastId ) ;
3775 // eslint-disable-next-line
3876 } catch ( e : any ) {
77+ toast . dismiss ( loadingToastId ) ;
3978 toast . error ( e . message ) ;
4079 }
4180 } ,
42- [ addPage , aiEnabled , openPageModal , t , toView , view . view_id , lastChildViewId ]
81+ [ addPage , aiEnabled , currentWorkspaceId , openPageModal , t , toView , view , lastChildViewId ]
4382 ) ;
4483
4584 const actions : {
@@ -80,16 +119,18 @@ function AddPageActions({ view, onImportClick }: { view: View; onImportClick?: (
80119 void handleAddPage ( ViewLayout . Calendar , t ( 'document.plugins.database.newDatabase' ) ) ;
81120 } ,
82121 } ,
83- ...( aiEnabled ? [
84- {
85- label : t ( 'chat.newChat' ) ,
86- icon : < ViewIcon layout = { ViewLayout . AIChat } size = { 'small' } /> ,
87- testId : 'add-ai-chat-button' ,
88- onSelect : ( ) => {
89- void handleAddPage ( ViewLayout . AIChat , t ( 'menuAppHeader.defaultNewPageName' ) ) ;
90- } ,
91- } ,
92- ] : [ ] ) ,
122+ ...( aiEnabled
123+ ? [
124+ {
125+ label : t ( 'chat.newChat' ) ,
126+ icon : < ViewIcon layout = { ViewLayout . AIChat } size = { 'small' } /> ,
127+ testId : 'add-ai-chat-button' ,
128+ onSelect : ( ) => {
129+ void handleAddPage ( ViewLayout . AIChat , t ( 'menuAppHeader.defaultNewPageName' ) ) ;
130+ } ,
131+ } ,
132+ ]
133+ : [ ] ) ,
93134 {
94135 label : t ( 'chart.menuName' ) ,
95136 icon : < ViewIcon layout = { ViewLayout . Chart } size = { 'small' } /> ,
0 commit comments