|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, {useState, useEffect, useRef} from "react"; |
| 4 | +import {motion} from "framer-motion"; |
| 5 | + |
| 6 | +import {useSetupFlow} from "@/hooks/useSetupFlow"; |
| 7 | +import { |
| 8 | + ConnectionStatus, |
| 9 | +} from "@/const/modelConfig"; |
| 10 | + |
| 11 | +import AgentConfig, {AgentConfigHandle} from "./AgentConfiguration"; |
| 12 | +import SaveConfirmModal from "./components/SaveConfirmModal"; |
| 13 | + |
| 14 | +interface AgentsContentProps { |
| 15 | + /** Whether currently saving */ |
| 16 | + isSaving?: boolean; |
| 17 | + /** Connection status */ |
| 18 | + connectionStatus?: ConnectionStatus; |
| 19 | + /** Is checking connection */ |
| 20 | + isCheckingConnection?: boolean; |
| 21 | + /** Check connection callback */ |
| 22 | + onCheckConnection?: () => void; |
| 23 | + /** Callback to expose connection status */ |
| 24 | + onConnectionStatusChange?: (status: ConnectionStatus) => void; |
| 25 | + /** Callback to expose saving state */ |
| 26 | + onSavingStateChange?: (isSaving: boolean) => void; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * AgentsContent - Main component for agent configuration |
| 31 | + * Can be used in setup flow or as standalone page |
| 32 | + */ |
| 33 | +export default function AgentsContent({ |
| 34 | + isSaving: externalIsSaving, |
| 35 | + connectionStatus: externalConnectionStatus, |
| 36 | + isCheckingConnection: externalIsCheckingConnection, |
| 37 | + onCheckConnection: externalOnCheckConnection, |
| 38 | + onConnectionStatusChange, |
| 39 | + onSavingStateChange, |
| 40 | +}: AgentsContentProps) { |
| 41 | + const agentConfigRef = useRef<AgentConfigHandle | null>(null); |
| 42 | + const [showSaveConfirm, setShowSaveConfirm] = useState(false); |
| 43 | + const pendingNavRef = useRef<null | (() => void)>(null); |
| 44 | + |
| 45 | + // Use custom hook for common setup flow logic |
| 46 | + const { |
| 47 | + canAccessProtectedData, |
| 48 | + pageVariants, |
| 49 | + pageTransition, |
| 50 | + } = useSetupFlow({ |
| 51 | + requireAdmin: true, |
| 52 | + externalConnectionStatus, |
| 53 | + externalIsCheckingConnection, |
| 54 | + onCheckConnection: externalOnCheckConnection, |
| 55 | + onConnectionStatusChange, |
| 56 | + nonAdminRedirect: "/setup/knowledges", |
| 57 | + }); |
| 58 | + |
| 59 | + const [internalIsSaving, setInternalIsSaving] = useState(false); |
| 60 | + const isSaving = externalIsSaving ?? internalIsSaving; |
| 61 | + |
| 62 | + // Update external saving state |
| 63 | + useEffect(() => { |
| 64 | + onSavingStateChange?.(isSaving); |
| 65 | + }, [isSaving, onSavingStateChange]); |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <motion.div |
| 70 | + initial="initial" |
| 71 | + animate="in" |
| 72 | + exit="out" |
| 73 | + variants={pageVariants} |
| 74 | + transition={pageTransition} |
| 75 | + style={{width: "100%", height: "100%"}} |
| 76 | + > |
| 77 | + {canAccessProtectedData ? ( |
| 78 | + <AgentConfig ref={agentConfigRef} canAccessProtectedData={canAccessProtectedData} /> |
| 79 | + ) : null} |
| 80 | + </motion.div> |
| 81 | + |
| 82 | + <SaveConfirmModal |
| 83 | + open={showSaveConfirm} |
| 84 | + onCancel={async () => { |
| 85 | + // Reload data from backend to discard changes |
| 86 | + await agentConfigRef.current?.reloadCurrentAgentData?.(); |
| 87 | + setShowSaveConfirm(false); |
| 88 | + const go = pendingNavRef.current; |
| 89 | + pendingNavRef.current = null; |
| 90 | + if (go) go(); |
| 91 | + }} |
| 92 | + onSave={async () => { |
| 93 | + try { |
| 94 | + setInternalIsSaving(true); |
| 95 | + await agentConfigRef.current?.saveAllChanges?.(); |
| 96 | + setShowSaveConfirm(false); |
| 97 | + const go = pendingNavRef.current; |
| 98 | + pendingNavRef.current = null; |
| 99 | + if (go) go(); |
| 100 | + } catch (e) { |
| 101 | + // errors are surfaced by underlying save |
| 102 | + } finally { |
| 103 | + setInternalIsSaving(false); |
| 104 | + } |
| 105 | + }} |
| 106 | + /> |
| 107 | + </> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
0 commit comments