11"use client" ;
22
3- import { useState , useEffect } from "react" ;
3+ import { useState , useEffect , useRef } from "react" ;
44import { useTranslation } from "react-i18next" ;
55import { NavigationLayout } from "@/components/navigation/NavigationLayout" ;
66import { HomepageContent } from "@/components/homepage/HomepageContent" ;
@@ -18,6 +18,7 @@ import MemoryContent from "./memory/MemoryContent";
1818import ModelsContent from "./models/ModelsContent" ;
1919import AgentsContent from "./agents/AgentsContent" ;
2020import KnowledgesContent from "./knowledges/KnowledgesContent" ;
21+ import SaveConfirmModal from "./agents/components/SaveConfirmModal" ;
2122import { SpaceContent } from "./space/components/SpaceContent" ;
2223import { fetchAgentList } from "@/services/agentConfigService" ;
2324import { useAgentImport , ImportAgentData } from "@/hooks/useAgentImport" ;
@@ -103,6 +104,11 @@ export default function Home() {
103104 // Setup-specific states
104105 const [ currentSetupStep , setCurrentSetupStep ] = useState < SetupStep > ( "models" ) ;
105106 const [ isSaving , setIsSaving ] = useState ( false ) ;
107+
108+ // Agent save confirmation states
109+ const [ showAgentSaveConfirm , setShowAgentSaveConfirm ] = useState ( false ) ;
110+ const [ pendingCompleteAction , setPendingCompleteAction ] = useState < ( ( ) => void ) | null > ( null ) ;
111+ const agentConfigRef = useRef < any > ( null ) ;
106112
107113 // Handle operations that require login
108114 const handleAuthRequired = ( ) => {
@@ -284,6 +290,20 @@ export default function Home() {
284290 } ;
285291
286292 const handleSetupComplete = ( ) => {
293+ // Check if we're on the agents step and if there are unsaved changes
294+ if ( currentSetupStep === "agents" && isAdmin && agentConfigRef . current ) {
295+ if ( agentConfigRef . current . hasUnsavedChanges ?.( ) ) {
296+ // Show save confirmation modal
297+ setShowAgentSaveConfirm ( true ) ;
298+ setPendingCompleteAction ( ( ) => ( ) => {
299+ setCurrentView ( "chat" ) ;
300+ saveView ( "chat" ) ;
301+ } ) ;
302+ return ;
303+ }
304+ }
305+
306+ // No unsaved changes, proceed directly
287307 setCurrentView ( "chat" ) ;
288308 saveView ( "chat" ) ;
289309 } ;
@@ -518,6 +538,7 @@ export default function Home() {
518538
519539 { currentSetupStep === "agents" && isAdmin && (
520540 < AgentsContent
541+ ref = { agentConfigRef }
521542 isSaving = { isSaving }
522543 connectionStatus = { connectionStatus }
523544 isCheckingConnection = { isCheckingConnection }
@@ -621,6 +642,37 @@ export default function Home() {
621642 < RegisterModal />
622643 </ >
623644 ) }
645+
646+ { /* Agent save confirmation modal for setup completion */ }
647+ < SaveConfirmModal
648+ open = { showAgentSaveConfirm }
649+ onCancel = { async ( ) => {
650+ // Reload data from backend to discard changes
651+ await agentConfigRef . current ?. reloadCurrentAgentData ?.( ) ;
652+ setShowAgentSaveConfirm ( false ) ;
653+ const action = pendingCompleteAction ;
654+ setPendingCompleteAction ( null ) ;
655+ if ( action ) action ( ) ;
656+ } }
657+ onSave = { async ( ) => {
658+ try {
659+ setIsSaving ( true ) ;
660+ await agentConfigRef . current ?. saveAllChanges ?.( ) ;
661+ setShowAgentSaveConfirm ( false ) ;
662+ const action = pendingCompleteAction ;
663+ setPendingCompleteAction ( null ) ;
664+ if ( action ) action ( ) ;
665+ } catch ( e ) {
666+ // errors are surfaced by underlying save
667+ } finally {
668+ setIsSaving ( false ) ;
669+ }
670+ } }
671+ onClose = { ( ) => {
672+ setShowAgentSaveConfirm ( false ) ;
673+ setPendingCompleteAction ( null ) ;
674+ } }
675+ />
624676 </ NavigationLayout >
625677 ) ;
626678 }
0 commit comments