Skip to content

Commit

Permalink
feat: updated default drives handling
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Sep 11, 2024
1 parent 4311b4c commit 9e44023
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
40 changes: 33 additions & 7 deletions src/context/read-mode.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
IDocumentDriveServer,
InferDocumentLocalState,
InferDocumentOperation,
InferDocumentState,
IReadMoveDriveServer,
IReadModeDriveServer,
ReadDocumentNotFoundError,
ReadDrive,
ReadDriveContext,
Expand All @@ -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,
>(
Expand Down Expand Up @@ -72,14 +74,18 @@ function bindClassMethods(instance: any) {
}

class ReadModeContextImpl implements Omit<IReadModeContext, 'readDrives'> {
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;
}

Expand Down Expand Up @@ -156,6 +162,7 @@ class ReadModeContextImpl implements Omit<IReadModeContext, 'readDrives'> {
): Promise<ReadDrivesListenerUnsubscribe> {
return this.server!.onReadDrivesUpdate(listener);
}

/* eslint-enable @typescript-eslint/no-non-null-assertion */
}

Expand All @@ -175,6 +182,25 @@ export const ReadModeContextProvider: FC<
> = props => {
const [readDrives, setReadDrives] = useState<ReadDrive[]>([]);

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 =>
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useDocumentDriveServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useLoadInitialData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
33 changes: 20 additions & 13 deletions src/utils/reactor.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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: [
Expand All @@ -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' },
},
};
};

0 comments on commit 9e44023

Please sign in to comment.