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