mirror of
https://github.com/langgenius/dify.git
synced 2026-05-13 08:57:28 +08:00
- Replace useRef pattern with useMemo for store creation in context.tsx - Remove unused extension prop from EditorTabItem - Fix useMemo dependency warnings in editor-tabs.tsx and skill-doc-editor.tsx - Add proper OnMount type for Monaco editor instead of any - Delete unused file-item.tsx and fold-item.tsx components - Remove unused getExtension and fromOpensObject utilities from type.ts - Refactor auto-reveal effect in files.tsx for better readability
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
'use client'
|
|
|
|
import {
|
|
useEffect,
|
|
useMemo,
|
|
useRef,
|
|
} from 'react'
|
|
import { useStore as useAppStore } from '@/app/components/app/store'
|
|
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 = {
|
|
children: React.ReactNode
|
|
}
|
|
|
|
export const SkillEditorProvider = ({ children }: SkillEditorProviderProps) => {
|
|
// Create store once using useMemo (stable across re-renders)
|
|
const store = useMemo(() => createSkillEditorStore(), [])
|
|
|
|
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])
|
|
|
|
return (
|
|
<SkillEditorContext.Provider value={store}>
|
|
{children}
|
|
</SkillEditorContext.Provider>
|
|
)
|
|
}
|