dify/web/app/components/workflow/skill/context.tsx
yyh fcd814a2c3
refactor(skill-editor): simplify state management and remove dead code
- 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
2026-01-15 14:02:15 +08:00

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>
)
}