mirror of
https://github.com/langgenius/dify.git
synced 2026-08-03 11:46:37 +08:00
Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
'use client'
|
|
|
|
import type { AgentSoulConfig } from '@dify/contracts/api/console/agent/types.gen'
|
|
import type { ReactNode } from 'react'
|
|
import type { AgentSoulConfigFormState } from './form-state'
|
|
import { createStore, Provider as JotaiProvider } from 'jotai'
|
|
import { useRef } from 'react'
|
|
import {
|
|
agentComposerDraftAtom,
|
|
agentComposerOriginalConfigAtom,
|
|
agentComposerOriginalDraftAtom,
|
|
agentComposerPublishedDraftAtom,
|
|
} from './store'
|
|
|
|
function createAgentComposerStore({
|
|
initialDraft,
|
|
initialOriginalConfig,
|
|
}: {
|
|
initialDraft?: AgentSoulConfigFormState
|
|
initialOriginalConfig?: AgentSoulConfig
|
|
}) {
|
|
const store = createStore()
|
|
|
|
if (initialOriginalConfig)
|
|
store.set(agentComposerOriginalConfigAtom, initialOriginalConfig)
|
|
if (initialDraft)
|
|
store.set(agentComposerDraftAtom, initialDraft)
|
|
if (initialDraft)
|
|
store.set(agentComposerOriginalDraftAtom, initialDraft)
|
|
if (initialDraft)
|
|
store.set(agentComposerPublishedDraftAtom, initialDraft)
|
|
|
|
return store
|
|
}
|
|
|
|
export function AgentComposerProvider({
|
|
children,
|
|
initialDraft,
|
|
initialOriginalConfig,
|
|
}: {
|
|
children: ReactNode
|
|
initialDraft?: AgentSoulConfigFormState
|
|
initialOriginalConfig?: AgentSoulConfig
|
|
}) {
|
|
const storeRef = useRef<ReturnType<typeof createAgentComposerStore> | null>(null)
|
|
if (!storeRef.current) {
|
|
storeRef.current = createAgentComposerStore({
|
|
initialDraft,
|
|
initialOriginalConfig,
|
|
})
|
|
}
|
|
const store = storeRef.current
|
|
|
|
return (
|
|
<JotaiProvider store={store}>
|
|
{children}
|
|
</JotaiProvider>
|
|
)
|
|
}
|