mirror of
https://github.com/langgenius/dify.git
synced 2026-01-19 11:45:05 +08:00
refactor(skill-editor): simplify SkillEditorProvider
Remove verbose comments and appId reset logic since parent component remounts on appId change. Consolidate imports and use function declaration.
This commit is contained in:
@ -1,45 +1,21 @@
|
||||
'use client'
|
||||
|
||||
import {
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
} from 'react'
|
||||
import { useStore as useAppStore } from '@/app/components/app/store'
|
||||
import {
|
||||
createSkillEditorStore,
|
||||
SkillEditorContext,
|
||||
} from './store'
|
||||
import type { SkillEditorStore } from './store'
|
||||
import { useRef } from 'react'
|
||||
import { createSkillEditorStore, SkillEditorContext } from './store'
|
||||
|
||||
/**
|
||||
* SkillEditorProvider
|
||||
*
|
||||
* Provides the SkillEditor store to all child components.
|
||||
* The store is created once per mount and persists across view switches.
|
||||
* When appId changes, the store is reset.
|
||||
*/
|
||||
export type SkillEditorProviderProps = {
|
||||
type SkillEditorProviderProps = {
|
||||
children: React.ReactNode
|
||||
}
|
||||
|
||||
export const SkillEditorProvider = ({ children }: SkillEditorProviderProps) => {
|
||||
// Create store once using useMemo (stable across re-renders)
|
||||
const store = useMemo(() => createSkillEditorStore(), [])
|
||||
export function SkillEditorProvider({ children }: SkillEditorProviderProps): React.ReactElement {
|
||||
const storeRef = useRef<SkillEditorStore | undefined>(undefined)
|
||||
|
||||
const appDetail = useAppStore(s => s.appDetail)
|
||||
const appId = appDetail?.id
|
||||
const prevAppIdRef = useRef<string | undefined>(undefined)
|
||||
|
||||
// Reset store when appId changes
|
||||
useEffect(() => {
|
||||
if (prevAppIdRef.current !== undefined && prevAppIdRef.current !== appId)
|
||||
store.getState().reset()
|
||||
|
||||
prevAppIdRef.current = appId
|
||||
}, [appId, store])
|
||||
if (!storeRef.current)
|
||||
storeRef.current = createSkillEditorStore()
|
||||
|
||||
return (
|
||||
<SkillEditorContext.Provider value={store}>
|
||||
<SkillEditorContext.Provider value={storeRef.current}>
|
||||
{children}
|
||||
</SkillEditorContext.Provider>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user