diff --git a/src/context/read-mode.tsx b/src/context/read-mode.tsx index 534e3498..c36bfbff 100644 --- a/src/context/read-mode.tsx +++ b/src/context/read-mode.tsx @@ -1,8 +1,9 @@ import { + IDocumentDriveServer, InferDocumentLocalState, InferDocumentOperation, InferDocumentState, - IReadMoveDriveServer, + IReadModeDriveServer, ReadDocumentNotFoundError, ReadDrive, ReadDriveContext, @@ -25,17 +26,18 @@ import { useState, } from 'react'; import { drivesToHash } from 'src/hooks/useDocumentDrives'; +import { useUserPermissions } from 'src/hooks/useUserPermissions'; import { logger } from 'src/services/logger'; import { DefaultDocumentDriveServer } from 'src/utils/document-drive-server'; -export interface IReadModeContext extends IReadMoveDriveServer { +export interface IReadModeContext extends IReadModeDriveServer { readDrives: ReadDrive[]; - setDocumentDrive(documentDrive: IReadMoveDriveServer): void; + setDocumentDrive(documentDrive: IReadModeDriveServer): void; } // decorator method to ensure server is defined before calling it function checkServer< - T extends IReadModeContext & { server: IReadMoveDriveServer | undefined }, + T extends IReadModeContext & { server: IReadModeDriveServer | undefined }, U extends any[], R, >( @@ -72,14 +74,18 @@ function bindClassMethods(instance: any) { } class ReadModeContextImpl implements Omit { - private server?: IReadMoveDriveServer; + private server?: IReadModeDriveServer; - constructor(documentDrive?: IReadMoveDriveServer) { + constructor(documentDrive?: IReadModeDriveServer) { bindClassMethods(this); this.server = documentDrive; } - setDocumentDrive(documentDrive: IReadMoveDriveServer) { + getServer(): IReadModeDriveServer | undefined { + return this.server; + } + + setDocumentDrive(documentDrive: IReadModeDriveServer) { this.server = documentDrive; } @@ -156,6 +162,7 @@ class ReadModeContextImpl implements Omit { ): Promise { return this.server!.onReadDrivesUpdate(listener); } + /* eslint-enable @typescript-eslint/no-non-null-assertion */ } @@ -175,6 +182,25 @@ export const ReadModeContextProvider: FC< > = props => { const [readDrives, setReadDrives] = useState([]); + const { isAllowedToCreateDocuments, isAllowedToEditDocuments } = + useUserPermissions(); + + // updates drive access level when user permissions change + const readMode = !(isAllowedToCreateDocuments || isAllowedToEditDocuments); + useMemo(() => { + const accessLevel = readMode ? 'READ' : 'WRITE'; + const server = ReadModeInstance.getServer(); + if ( + server && + typeof (server as IDocumentDriveServer) + .setAllDefaultDrivesAccessLevel === 'function' + ) { + (server as IDocumentDriveServer) + .setAllDefaultDrivesAccessLevel(accessLevel) + .catch(logger.error); + } + }, [readMode]); + useEffect(() => { const unsubscribe = ReadModeInstance.onReadDrivesUpdate(newDrives => { setReadDrives(readDrives => diff --git a/src/hooks/useDocumentDriveServer.ts b/src/hooks/useDocumentDriveServer.ts index c443db8f..2918f40b 100644 --- a/src/hooks/useDocumentDriveServer.ts +++ b/src/hooks/useDocumentDriveServer.ts @@ -38,8 +38,9 @@ import { useConnectCrypto, useConnectDid } from './useConnectCrypto'; import { useDocumentDrives } from './useDocumentDrives'; import { useUserPermissions } from './useUserPermissions'; -export const FILE_UPLOAD_OPERATIONS_CHUNK_SIZE = - parseInt(import.meta.env.FILE_UPLOAD_OPERATIONS_CHUNK_SIZE as string) || 50; +export const FILE_UPLOAD_OPERATIONS_CHUNK_SIZE = parseInt( + (import.meta.env.FILE_UPLOAD_OPERATIONS_CHUNK_SIZE as string) || '50', +); // TODO this should be added to the document model export interface SortOptions { diff --git a/src/hooks/useLoadInitialData.tsx b/src/hooks/useLoadInitialData.tsx index fe0f708b..d45d6c6f 100644 --- a/src/hooks/useLoadInitialData.tsx +++ b/src/hooks/useLoadInitialData.tsx @@ -125,7 +125,8 @@ export const useLoadInitialData = () => { ); useEffect(() => { - updateUiDriveNodes(readDrives.concat(documentDrives)).catch( + const drives: DocumentDriveDocument[] = [...readDrives, ...documentDrives]; + updateUiDriveNodes(drives).catch( console.error, ); }, [documentDrives, readDrives, updateUiDriveNodes]); diff --git a/src/utils/reactor.ts b/src/utils/reactor.ts index 5661bd89..84bc6f3f 100644 --- a/src/utils/reactor.ts +++ b/src/utils/reactor.ts @@ -1,4 +1,7 @@ -import { DocumentDriveServerOptions } from 'document-drive/server'; +import { + DefaultRemoteDriveInput, + DocumentDriveServerOptions, +} from 'document-drive/server'; const DEFAULT_DRIVES_URL = import.meta.env.PH_CONNECT_DEFAULT_DRIVES_URL || undefined; @@ -8,12 +11,13 @@ const defaultDrivesUrl = DEFAULT_DRIVES_URL export const getReactorDefaultDrivesConfig = (): Pick< DocumentDriveServerOptions, - 'defaultRemoteDrives' | 'removeOldRemoteDrives' + 'defaultDrives' > => { - const defaultDrives: DocumentDriveServerOptions['defaultRemoteDrives'] = - defaultDrivesUrl.map(driveUrl => ({ + const remoteDrives: DefaultRemoteDriveInput[] = defaultDrivesUrl.map( + driveUrl => ({ url: driveUrl, options: { + accessLevel: 'READ', sharingType: 'PUBLIC', availableOffline: true, listeners: [ @@ -38,16 +42,19 @@ export const getReactorDefaultDrivesConfig = (): Pick< triggers: [], pullInterval: 3000, }, - })); + }), + ); return { - defaultRemoteDrives: defaultDrives, - removeOldRemoteDrives: - defaultDrivesUrl.length > 0 - ? { - strategy: 'preserve-by-url', - urls: defaultDrivesUrl, - } - : { strategy: 'preserve-all' }, + defaultDrives: { + remoteDrives, + removeOldRemoteDrives: + defaultDrivesUrl.length > 0 + ? { + strategy: 'preserve-by-url', + urls: defaultDrivesUrl, + } + : { strategy: 'preserve-all' }, + }, }; };