|
| 1 | +import React from 'react'; |
| 2 | +import { useRef } from 'react'; |
| 3 | +import type { |
| 4 | + WorkspacesStateData, |
| 5 | + WorkspacesStorage, |
| 6 | +} from './workspaces-storage'; |
| 7 | +import { AtlasUserData } from '@mongodb-js/compass-user-data'; |
| 8 | +import { WorkspacesStateSchema } from './workspaces-storage'; |
| 9 | +import { EJSON } from 'bson'; |
| 10 | +import { WorkspacesStorageServiceProvider } from '../provider'; |
| 11 | +import type { AtlasService } from '@mongodb-js/atlas-service/provider'; |
| 12 | +import { atlasServiceLocator } from '@mongodb-js/atlas-service/provider'; |
| 13 | + |
| 14 | +class WorkspacesStorageWeb implements WorkspacesStorage { |
| 15 | + private userData: AtlasUserData<typeof WorkspacesStateSchema>; |
| 16 | + |
| 17 | + constructor(orgId: string, projectId: string, atlasService: AtlasService) { |
| 18 | + this.userData = new AtlasUserData( |
| 19 | + WorkspacesStateSchema, |
| 20 | + 'WorkspacesState', |
| 21 | + { |
| 22 | + orgId, |
| 23 | + projectId, |
| 24 | + getResourceUrl: (path?: string) => { |
| 25 | + const url = atlasService.userDataEndpoint(`/${path || ''}`); |
| 26 | + return url; |
| 27 | + }, |
| 28 | + authenticatedFetch: atlasService.authenticatedFetch.bind(atlasService), |
| 29 | + serialize: (content) => EJSON.stringify(content), |
| 30 | + deserialize: (content: string) => EJSON.parse(content), |
| 31 | + } |
| 32 | + ); |
| 33 | + } |
| 34 | + save(state: WorkspacesStateData): Promise<boolean> { |
| 35 | + return this.userData.write('current-workspace', state); |
| 36 | + } |
| 37 | + load(): Promise<WorkspacesStateData | null> { |
| 38 | + return this.userData |
| 39 | + .readOne('current-workspace') |
| 40 | + .then((data) => data ?? null); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export const WorkspacesStorageServiceProviderWeb: React.FC<{ |
| 45 | + orgId: string; |
| 46 | + projectId: string; |
| 47 | +}> = ({ orgId, projectId, children }) => { |
| 48 | + const atlasService = atlasServiceLocator(); |
| 49 | + const storage = useRef( |
| 50 | + new WorkspacesStorageWeb(orgId, projectId, atlasService) |
| 51 | + ); |
| 52 | + return ( |
| 53 | + <WorkspacesStorageServiceProvider storage={storage}> |
| 54 | + {children} |
| 55 | + </WorkspacesStorageServiceProvider> |
| 56 | + ); |
| 57 | +}; |
0 commit comments