mirror of
https://github.com/langgenius/dify.git
synced 2026-04-19 05:23:29 +08:00
Migrate 172 files from es-toolkit/compat to native es-toolkit. Migrated: - noop → es-toolkit/function - cloneDeep, omit, clone, pick → es-toolkit/object - uniq, intersection → es-toolkit/array - capitalize, camelCase, kebabCase, escape → es-toolkit/string - isEqual → es-toolkit/predicate Kept in compat (API differences): - uniqueId, isEmpty - get, merge, flow, curry, debounce - different APIs - uniqBy, groupBy, intersectionBy - property string support - flatten - undefined handling Benefits: 2-3x faster, smaller bundle Part of #30243 Fixes #30244
80 lines
1.7 KiB
TypeScript
80 lines
1.7 KiB
TypeScript
import type {
|
|
FileEntity,
|
|
} from './types'
|
|
import { isEqual } from 'es-toolkit/predicate'
|
|
import {
|
|
createContext,
|
|
useContext,
|
|
useEffect,
|
|
useRef,
|
|
} from 'react'
|
|
import {
|
|
create,
|
|
useStore as useZustandStore,
|
|
} from 'zustand'
|
|
|
|
type Shape = {
|
|
files: FileEntity[]
|
|
setFiles: (files: FileEntity[]) => void
|
|
}
|
|
|
|
export const createFileStore = (
|
|
value: FileEntity[] = [],
|
|
onChange?: (files: FileEntity[]) => void,
|
|
) => {
|
|
return create<Shape>(set => ({
|
|
files: value ? [...value] : [],
|
|
setFiles: (files) => {
|
|
set({ files })
|
|
onChange?.(files)
|
|
},
|
|
}))
|
|
}
|
|
|
|
type FileStore = ReturnType<typeof createFileStore>
|
|
export const FileContext = createContext<FileStore | null>(null)
|
|
|
|
export function useStore<T>(selector: (state: Shape) => T): T {
|
|
const store = useContext(FileContext)
|
|
if (!store)
|
|
throw new Error('Missing FileContext.Provider in the tree')
|
|
|
|
return useZustandStore(store, selector)
|
|
}
|
|
|
|
export const useFileStore = () => {
|
|
return useContext(FileContext)!
|
|
}
|
|
|
|
type FileProviderProps = {
|
|
children: React.ReactNode
|
|
value?: FileEntity[]
|
|
onChange?: (files: FileEntity[]) => void
|
|
}
|
|
export const FileContextProvider = ({
|
|
children,
|
|
value,
|
|
onChange,
|
|
}: FileProviderProps) => {
|
|
const storeRef = useRef<FileStore | undefined>(undefined)
|
|
if (!storeRef.current)
|
|
storeRef.current = createFileStore(value, onChange)
|
|
|
|
useEffect(() => {
|
|
if (!storeRef.current)
|
|
return
|
|
if (isEqual(value, storeRef.current.getState().files))
|
|
return
|
|
|
|
storeRef.current.setState({
|
|
files: value ? [...value] : [],
|
|
})
|
|
}, [value])
|
|
|
|
return (
|
|
<FileContext.Provider value={storeRef.current}>
|
|
{children}
|
|
</FileContext.Provider>
|
|
)
|
|
}
|