2022-09-08 22:29:47 +00:00
|
|
|
import { PropsWithChildren, createContext, useContext } from 'react';
|
|
|
|
|
|
|
|
export type OperatingSystem = 'browser' | 'linux' | 'macOS' | 'windows' | 'unknown';
|
|
|
|
|
|
|
|
// Platform represents the underlying native layer the app is running on.
|
|
|
|
// This could be Tauri or web.
|
|
|
|
export type Platform = {
|
|
|
|
platform: 'web' | 'tauri'; // This represents the specific platform implementation
|
|
|
|
getThumbnailUrlById: (casId: string) => string;
|
2023-02-14 05:27:11 +00:00
|
|
|
getFileUrl: (libraryId: string, locationLocalId: number, filePathId: number) => string;
|
2022-09-12 16:14:47 +00:00
|
|
|
openLink: (url: string) => void;
|
2022-09-08 22:29:47 +00:00
|
|
|
demoMode?: boolean; // TODO: Remove this in favour of demo mode being handled at the React Query level
|
|
|
|
getOs?(): Promise<OperatingSystem>;
|
2022-12-13 01:48:14 +00:00
|
|
|
openDirectoryPickerDialog?(): Promise<null | string | string[]>;
|
2022-09-08 22:29:47 +00:00
|
|
|
openFilePickerDialog?(): Promise<null | string | string[]>;
|
2022-12-13 01:48:14 +00:00
|
|
|
saveFilePickerDialog?(): Promise<string | null>;
|
2022-10-24 07:48:52 +00:00
|
|
|
showDevtools?(): void;
|
|
|
|
openPath?(path: string): void;
|
2022-09-08 22:29:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Keep this private and use through helpers below
|
|
|
|
const context = createContext<Platform>(undefined!);
|
|
|
|
|
|
|
|
// is a hook which allows you to fetch information about the current platform from the React context.
|
|
|
|
export function usePlatform(): Platform {
|
|
|
|
const ctx = useContext(context);
|
|
|
|
if (!ctx)
|
|
|
|
throw new Error(
|
|
|
|
"The 'PlatformProvider' has not been mounted above the current 'usePlatform' call."
|
|
|
|
);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// provides the platform context to the rest of the app through React context.
|
|
|
|
// Mount it near the top of your component tree.
|
|
|
|
export function PlatformProvider({
|
|
|
|
platform,
|
|
|
|
children
|
|
|
|
}: PropsWithChildren<{ platform: Platform }>) {
|
|
|
|
return <context.Provider value={platform}>{children}</context.Provider>;
|
|
|
|
}
|