mirror of
https://gitee.com/dify_ai/dify.git
synced 2024-12-02 19:27:48 +08:00
36 lines
841 B
TypeScript
36 lines
841 B
TypeScript
|
'use client'
|
||
|
|
||
|
import { createContext, useContext } from 'use-context-selector'
|
||
|
import useSWR from 'swr'
|
||
|
import { fetchWorkspaces } from '@/service/common'
|
||
|
import type { IWorkspace } from '@/models/common'
|
||
|
|
||
|
export type WorkspacesContextValue = {
|
||
|
workspaces: IWorkspace[]
|
||
|
}
|
||
|
|
||
|
const WorkspacesContext = createContext<WorkspacesContextValue>({
|
||
|
workspaces: []
|
||
|
})
|
||
|
|
||
|
interface IWorkspaceProviderProps {
|
||
|
children: React.ReactNode
|
||
|
}
|
||
|
export const WorkspaceProvider = ({
|
||
|
children
|
||
|
}: IWorkspaceProviderProps) => {
|
||
|
const { data } = useSWR({ url: '/workspaces' }, fetchWorkspaces)
|
||
|
|
||
|
return (
|
||
|
<WorkspacesContext.Provider value={{
|
||
|
workspaces: data?.workspaces || []
|
||
|
}}>
|
||
|
{children}
|
||
|
</WorkspacesContext.Provider>
|
||
|
)
|
||
|
}
|
||
|
|
||
|
export const useWorkspacesContext = () => useContext(WorkspacesContext)
|
||
|
|
||
|
export default WorkspacesContext
|