1 import type { ReactNode } from 'react';
3 import type { PublicKeyReference } from '@proton/crypto/lib';
4 import type { SHARE_MEMBER_PERMISSIONS } from '@proton/shared/lib/drive/permissions';
5 import { getNewWindow } from '@proton/shared/lib/helpers/window';
7 import { useLinkSharingModal } from '../components/modals/ShareLinkModal/ShareLinkModal';
8 import { useDriveCrypto } from '../store/_crypto';
9 import type { DocumentAction } from '../store/_documents';
10 import { useDriveDocsFeatureFlag, useOpenDocument } from '../store/_documents';
11 import type { PathItem } from '../store/_views/useLinkPath';
12 import type { DocumentKeys, DocumentNodeMeta } from './_documents';
13 import { useDocuments } from './_documents';
14 import type { DecryptedNode } from './_nodes/interface';
15 import useNode from './_nodes/useNode';
16 import useNodes from './_nodes/useNodes';
17 import { useMyFiles, useResolveShareId } from './_shares';
18 import type { NodeMeta } from './interface';
20 export interface DriveCompat {
22 * Whether or not Docs is enabled. Only uses feature flags, not context aware.
24 isDocsEnabled: boolean;
27 * Gets whether or not Docs can be used, with awareness of the context.
29 canUseDocs: (meta: NodeMeta) => Promise<boolean>;
32 * Gets a node, either from cache or fetched.
34 getNode: (meta: NodeMeta) => Promise<DecryptedNode>;
36 getNodes: (ids: { linkId: string; shareId: string }[]) => Promise<DecryptedNode[]>;
38 getShareId: (meta: NodeMeta) => Promise<string>;
41 * Gets the contents of a node.
43 getNodeContents: (meta: NodeMeta) => Promise<{ contents: Uint8Array; node: DecryptedNode }>;
46 * Gets permissions associated to a specific node.
48 getNodePermissions: (meta: NodeMeta) => Promise<SHARE_MEMBER_PERMISSIONS>;
51 * Finds an available name for a new node.
53 * @param parentMeta The parent node where the new node will be located.
55 findAvailableNodeName: (parentMeta: NodeMeta, desiredName: string) => Promise<string>;
58 * Creates an empty document node (document shell).
60 * @param parentMeta The parent node where the new node will be located.
62 createDocumentNode: (parentMeta: NodeMeta, name: string) => Promise<DocumentNodeMeta>;
65 * Gets the keys for a given document node.
67 getDocumentKeys: (meta: NodeMeta) => Promise<DocumentKeys>;
70 * Renames a document node.
72 renameDocument: (meta: NodeMeta, newName: string) => Promise<void>;
74 trashDocument: (meta: NodeMeta, parentLinkId: string) => Promise<void>;
75 restoreDocument: (meta: NodeMeta, parentLinkId: string) => Promise<void>;
77 * Gets the URL for a given document.
79 getDocumentUrl: (meta: NodeMeta) => URL;
81 getNodePaths: (ids: { linkId: string; shareId: string }[]) => Promise<PathItem[][]>;
82 getNodesAreShared: (ids: { linkId: string; shareId: string }[]) => Promise<boolean[]>;
84 * Opens a document's sharing modal.
86 openDocumentSharingModal: (meta: NodeMeta) => void;
89 * Opens a document in a new window.
91 openDocument: (meta: NodeMeta) => void;
93 action: DocumentAction & {
99 * Gets the key used to verify signatures.
101 getVerificationKey: (email: string) => Promise<PublicKeyReference[]>;
104 * Gets the node identifier for My Files.
106 * Temporary utility function, subject to change :)
108 getMyFilesNodeMeta: () => Promise<NodeMeta>;
111 * Modals that should be included in the DOM tree.
116 export const useDriveCompat = (): DriveCompat => {
117 const { withResolve } = useResolveShareId();
119 const { createDocumentNode, getDocumentKeys, renameDocument, getDocumentUrl, trashDocument, restoreDocument } =
121 const { getNode, getNodeContents, getNodePermissions, findAvailableNodeName } = useNode();
122 const { getNodes, getNodePaths, getNodesAreShared } = useNodes();
123 const { getMyFilesNodeMeta } = useMyFiles();
124 const { openDocumentWindow } = useOpenDocument();
125 const { getVerificationKey } = useDriveCrypto();
126 const { isDocsEnabled, canUseDocs } = useDriveDocsFeatureFlag();
128 const [linkSharingModal, showLinkSharingModal] = useLinkSharingModal();
130 const openDocument = (meta: NodeMeta) =>
131 openDocumentWindow({ ...meta, mode: 'open', window: getNewWindow().handle });
135 canUseDocs: withResolve(({ shareId }) => canUseDocs(shareId)),
136 createDocumentNode: withResolve(createDocumentNode),
137 getDocumentKeys: withResolve(getDocumentKeys),
140 getNode: withResolve(getNode),
141 getNodeContents: withResolve(getNodeContents),
142 getNodePermissions: withResolve(getNodePermissions),
144 getShareId: withResolve(({ shareId }) => shareId),
145 findAvailableNodeName: withResolve(findAvailableNodeName),
146 renameDocument: withResolve(renameDocument),
147 trashDocument: withResolve(trashDocument),
148 restoreDocument: withResolve(restoreDocument),
152 openDocumentSharingModal: withResolve(showLinkSharingModal),
155 // This should be changed to a fragment if more modals are added
156 modals: linkSharingModal,