1 import type { ReactNode } from 'react';
2 import { createContext, useContext } from 'react';
4 import { useDriveDocsFeatureFlag, useDriveDocsPublicSharingFF, useOpenDocument } from '../store/_documents';
5 import { type DocumentKeys } from './_documents';
6 import { usePublicNode } from './_nodes';
7 import type { DecryptedNode } from './_nodes/interface';
8 import { usePublicDocsToken } from './_shares';
9 import type { NodeMeta, PublicNodeMeta } from './interface';
11 export interface PublicDriveCompat {
13 * Whether or not Docs is enabled. Only uses feature flags, not context aware.
15 isDocsEnabled: boolean;
18 * Whether or not the public docs feature flag is enabled.
20 isPublicDocsEnabled: boolean;
23 * The custom password for the public link.
25 customPassword: string;
28 * The token for the public link.
33 * The url password for the public link.
38 * Whether or not a custom password is needed.
40 isPasswordNeeded: boolean;
43 * Submits the custom password.
45 submitPassword: (customPassword: string) => Promise<void>;
48 * Whether or not the interface is ready to receive calls.
53 * Whether or not there was an error loading the public link.
58 * The error that occurred while loading the public link.
63 * Gets a node, either from cache or fetched.
65 getNode: (meta: PublicNodeMeta, forceFetch?: boolean) => Promise<DecryptedNode>;
68 * Redirects to the authed document.
70 redirectToAuthedDocument: (meta: NodeMeta) => void;
73 * Gets the keys for a given document node.
75 getDocumentKeys: (meta: PublicNodeMeta) => Promise<Pick<DocumentKeys, 'documentContentKey'>>;
78 * Gets the authentication headers for `useApi()`
80 getPublicAuthHeaders: () => { [key: string]: string };
83 export const usePublicDriveCompatValue = (): PublicDriveCompat => {
84 const { isDocsEnabled } = useDriveDocsFeatureFlag();
85 const { isDocsPublicSharingEnabled } = useDriveDocsPublicSharingFF();
88 isReady: isDocsTokenReady,
98 } = usePublicDocsToken();
100 const { getNode, getNodeContentKey, didCompleteInitialSetup } = usePublicNode({ isDocsTokenReady, linkId });
101 const { openDocumentWindow } = useOpenDocument();
103 const redirectToAuthedDocument = (meta: NodeMeta) => openDocumentWindow({ ...meta, mode: 'open', window: window });
112 isReady: isDocsTokenReady && didCompleteInitialSetup,
115 redirectToAuthedDocument: redirectToAuthedDocument,
116 isPublicDocsEnabled: isDocsPublicSharingEnabled,
117 getDocumentKeys: async (nodeMeta) => ({
118 documentContentKey: await getNodeContentKey(nodeMeta),
121 getPublicAuthHeaders,
125 export const PublicCompatContext = createContext<PublicDriveCompat | null>(null);
127 export const usePublicDriveCompat = () => {
128 const context = useContext(PublicCompatContext);
130 throw new Error('No provider for PublicCompatContext');
135 export const PublicCompatProvider = ({ children }: { children: ReactNode }) => {
136 const value = usePublicDriveCompatValue();
138 return <PublicCompatContext.Provider value={value}>{children}</PublicCompatContext.Provider>;