dify/web/context/workspace-context.tsx

37 lines
842 B
TypeScript
Raw Normal View History

2023-05-15 08:51:32 +08:00
'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: [],
2023-05-15 08:51:32 +08:00
})
type IWorkspaceProviderProps = {
2023-05-15 08:51:32 +08:00
children: React.ReactNode
}
2023-05-15 08:51:32 +08:00
export const WorkspaceProvider = ({
children,
2023-05-15 08:51:32 +08:00
}: IWorkspaceProviderProps) => {
const { data } = useSWR({ url: '/workspaces' }, fetchWorkspaces)
return (
<WorkspacesContext.Provider value={{
workspaces: data?.workspaces || [],
2023-05-15 08:51:32 +08:00
}}>
{children}
</WorkspacesContext.Provider>
)
}
export const useWorkspacesContext = () => useContext(WorkspacesContext)
export default WorkspacesContext