Move electron API types to lower layer

This commit is contained in:
Manav Rathi 2024-04-08 16:42:54 +05:30
parent 48f741b792
commit f4f041552f
No known key found for this signature in database
34 changed files with 77 additions and 74 deletions

View file

@ -180,12 +180,12 @@ export const attachIPCHandlers = () => {
ipcMain.handle(
"saveStreamToDisk",
(_, path: string, fileStream: ReadableStream<any>) =>
(_, path: string, fileStream: ReadableStream) =>
saveStreamToDisk(path, fileStream),
);
ipcMain.handle("saveFileToDisk", (_, path: string, file: any) =>
saveFileToDisk(path, file),
ipcMain.handle("saveFileToDisk", (_, path: string, contents: string) =>
saveFileToDisk(path, contents),
);
ipcMain.handle("readTextFile", (_, path: string) => readTextFile(path));

View file

@ -32,9 +32,9 @@
* and when changing one of them, remember to see if the other two also need
* changing:
*
* - [renderer] web/packages/shared/electron/types.ts contains docs
* - [preload] desktop/src/preload.ts
* - [main] desktop/src/main/ipc.ts contains impl
* - [renderer] web/packages/next/types/electron.ts contains docs
* - [preload] desktop/src/preload.ts
* - [main] desktop/src/main/ipc.ts contains impl
*/
import { contextBridge, ipcRenderer } from "electron/renderer";
@ -227,11 +227,11 @@ const checkExistsAndCreateDir = (dirPath: string): Promise<void> =>
const saveStreamToDisk = (
path: string,
fileStream: ReadableStream<any>,
fileStream: ReadableStream,
): Promise<void> => ipcRenderer.invoke("saveStreamToDisk", path, fileStream);
const saveFileToDisk = (path: string, file: any): Promise<void> =>
ipcRenderer.invoke("saveFileToDisk", path, file);
const saveFileToDisk = (path: string, contents: string): Promise<void> =>
ipcRenderer.invoke("saveFileToDisk", path, contents);
const readTextFile = (path: string): Promise<string> =>
ipcRenderer.invoke("readTextFile", path);

View file

@ -1,5 +1,5 @@
import ElectronAPIs from "@/next/electron";
import LinkButton from "@ente/shared/components/LinkButton";
import ElectronAPIs from "@ente/shared/electron";
import { logError } from "@ente/shared/sentry";
import { Tooltip } from "@mui/material";
import { styled } from "@mui/material/styles";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import Notification from "components/Notification";
import { t } from "i18next";
import isElectron from "is-electron";

View file

@ -3,7 +3,7 @@ import { AppContext } from "pages/_app";
import { useContext, useEffect, useState } from "react";
import { Trans } from "react-i18next";
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { addLogLine } from "@ente/shared/logging";
import { getDebugLogs } from "@ente/shared/logging/web";
import { downloadAsFile } from "@ente/shared/utils";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { CustomError } from "@ente/shared/error";
import { addLogLine } from "@ente/shared/logging";
import { logError } from "@ente/shared/sentry";

View file

@ -6,8 +6,8 @@ import watchFolderService from "services/watchFolder/watchFolderService";
import { WatchMapping } from "types/watchFolder";
import { MappingList } from "./mappingList";
import ElectronAPIs from "@/next/electron";
import DialogTitleWithCloseButton from "@ente/shared/components/DialogBox/TitleWithCloseButton";
import ElectronAPIs from "@ente/shared/electron";
import UploadStrategyChoiceModal from "components/Upload/UploadStrategyChoiceModal";
import { PICKED_UPLOAD_TYPE, UPLOAD_STRATEGY } from "constants/upload";
import isElectron from "is-electron";

View file

@ -1,5 +1,7 @@
import { CustomHead } from "@/next/components/Head";
import ElectronAPIs from "@/next/electron";
import { setupI18n } from "@/next/i18n";
import { AppUpdateInfo } from "@/next/types/ipc";
import {
APPS,
APP_TITLES,
@ -20,8 +22,6 @@ import EnteSpinner from "@ente/shared/components/EnteSpinner";
import { MessageContainer } from "@ente/shared/components/MessageContainer";
import AppNavbar from "@ente/shared/components/Navbar/app";
import { PHOTOS_PAGES as PAGES } from "@ente/shared/constants/pages";
import ElectronAPIs from "@ente/shared/electron";
import { AppUpdateInfo } from "@ente/shared/electron/types";
import { CustomError } from "@ente/shared/error";
import { Events, eventBus } from "@ente/shared/events";
import { useLocalState } from "@ente/shared/hooks/useLocalState";

View file

@ -89,9 +89,9 @@ import {
splitNormalAndHiddenCollections,
} from "utils/collection";
import ElectronAPIs from "@/next/electron";
import { APPS } from "@ente/shared/apps/constants";
import { CenteredFlex } from "@ente/shared/components/Container";
import ElectronAPIs from "@ente/shared/electron";
import useFileInput from "@ente/shared/hooks/useFileInput";
import useMemoSingleThreaded from "@ente/shared/hooks/useMemoSingleThreaded";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";

View file

@ -1,3 +1,4 @@
import ElectronAPIs from "@/next/electron";
import Login from "@ente/accounts/components/Login";
import SignUp from "@ente/accounts/components/SignUp";
import { APPS } from "@ente/shared/apps/constants";
@ -5,7 +6,6 @@ import { EnteLogo } from "@ente/shared/components/EnteLogo";
import EnteSpinner from "@ente/shared/components/EnteSpinner";
import { PHOTOS_PAGES as PAGES } from "@ente/shared/constants/pages";
import { saveKeyInSessionStore } from "@ente/shared/crypto/helpers";
import ElectronAPIs from "@ente/shared/electron";
import { getAlbumsURL } from "@ente/shared/network/api";
import { logError } from "@ente/shared/sentry";
import localForage from "@ente/shared/storage/localForage";

View file

@ -1,5 +1,5 @@
import ElectronAPIs from "@/next/electron";
import ComlinkCryptoWorker from "@ente/shared/crypto";
import ElectronAPIs from "@ente/shared/electron";
import { CustomError } from "@ente/shared/error";
import { Events, eventBus } from "@ente/shared/events";
import { addLogLine } from "@ente/shared/logging";

View file

@ -37,7 +37,7 @@ import {
} from "utils/file";
import { decodeLivePhoto } from "../livePhotoService";
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { CustomError } from "@ente/shared/error";
import { Events, eventBus } from "@ente/shared/events";
import { addLogLine } from "@ente/shared/logging";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import isElectron from "is-electron";
import { ElectronFile } from "types/upload";
import ComlinkFFmpegWorker from "utils/comlink/ComlinkFFmpegWorker";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { logError } from "@ente/shared/sentry";
import { PICKED_UPLOAD_TYPE } from "constants/upload";
import { Collection } from "types/collection";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { CustomError } from "@ente/shared/error";
import { addLogLine } from "@ente/shared/logging";
import { getFileNameSize } from "@ente/shared/logging/web";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { addLocalLog, addLogLine } from "@ente/shared/logging";
import { logError } from "@ente/shared/sentry";
import { UPLOAD_RESULT, UPLOAD_STRATEGY } from "constants/upload";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { CustomError } from "@ente/shared/error";
import { addLogLine } from "@ente/shared/logging";
import { getAlbumsURL } from "@ente/shared/network/api";

View file

@ -52,7 +52,7 @@ import {
} from "services/fileService";
import { FileTypeInfo } from "types/upload";
import { default as ElectronAPIs } from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { workerBridge } from "@ente/shared/worker/worker-bridge";
import { t } from "i18next";
import { getFileExportPath, getUniqueFileExportName } from "utils/export";

View file

@ -1,7 +1,7 @@
import ElectronAPIs from "@/next/electron";
import { AppUpdateInfo } from "@/next/types/ipc";
import { logoutUser } from "@ente/accounts/services/user";
import { DialogBoxAttributes } from "@ente/shared/components/DialogBox/types";
import ElectronAPIs from "@ente/shared/electron";
import { AppUpdateInfo } from "@ente/shared/electron/types";
import AutoAwesomeOutlinedIcon from "@mui/icons-material/AutoAwesomeOutlined";
import InfoOutlined from "@mui/icons-material/InfoRounded";
import { Link } from "@mui/material";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { getFileNameSize } from "@ente/shared/logging/web";
import { FILE_READER_CHUNK_SIZE, PICKED_UPLOAD_TYPE } from "constants/upload";
import isElectron from "is-electron";

View file

@ -24,6 +24,7 @@ import { PAGES } from "../constants/pages";
import { generateSRPSetupAttributes } from "../services/srp";
import { logoutUser } from "../services/user";
import ElectronAPIs from "@/next/electron";
import { APP_HOMES } from "@ente/shared/apps/constants";
import { PageProps } from "@ente/shared/apps/types";
import { VerticallyCentered } from "@ente/shared/components/Container";
@ -37,7 +38,6 @@ import VerifyMasterPasswordForm, {
} from "@ente/shared/components/VerifyMasterPasswordForm";
import ComlinkCryptoWorker from "@ente/shared/crypto";
import { B64EncryptionResult } from "@ente/shared/crypto/types";
import ElectronAPIs from "@ente/shared/electron";
import { CustomError } from "@ente/shared/error";
import { addLocalLog } from "@ente/shared/logging";
import { getAccountsURL } from "@ente/shared/network/api";

View file

@ -1,4 +1,4 @@
import ElectronAPIs from "@ente/shared/electron";
import ElectronAPIs from "@/next/electron";
import { Events, eventBus } from "@ente/shared/events";
import { logError } from "@ente/shared/sentry";
import InMemoryStore from "@ente/shared/storage/InMemoryStore";

View file

@ -0,0 +1,10 @@
import type { ElectronAPIsType } from "./types/ipc";
// TODO (MR):
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ElectronAPIs = (globalThis as unknown as any)[
// eslint-disable-next-line @typescript-eslint/dot-notation, @typescript-eslint/no-unsafe-member-access
"ElectronAPIs"
] as ElectronAPIsType;
export default ElectronAPIs;

View file

@ -1,3 +1,8 @@
export enum UPLOAD_STRATEGY {
SINGLE_COLLECTION,
COLLECTION_PER_FOLDER,
}
/*
* ElectronFile is a custom interface that is used to represent
* any file on disk as a File-like object in the Electron desktop app.
@ -20,3 +25,25 @@ export interface DataStream {
stream: ReadableStream<Uint8Array>;
chunkCount: number;
}
export interface WatchMappingSyncedFile {
path: string;
uploadedFileID: number;
collectionID: number;
}
export interface WatchMapping {
rootFolderName: string;
folderPath: string;
uploadStrategy: UPLOAD_STRATEGY;
syncedFiles: WatchMappingSyncedFile[];
ignoredFiles: string[];
}
export interface EventQueueItem {
type: "upload" | "trash";
folderPath: string;
collectionName?: string;
paths?: string[];
files?: ElectronFile[];
}

View file

@ -3,8 +3,7 @@
//
// See [Note: types.ts <-> preload.ts <-> ipc.ts]
import type { ElectronFile } from "@ente/shared/upload/types";
import type { WatchMapping } from "@ente/shared/watchFolder/types";
import type { ElectronFile, WatchMapping } from "./file";
export interface AppUpdateInfo {
autoUpdatable: boolean;
@ -199,9 +198,9 @@ export interface ElectronAPIsType {
checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
saveStreamToDisk: (
path: string,
fileStream: ReadableStream<any>,
fileStream: ReadableStream,
) => Promise<void>;
saveFileToDisk: (path: string, file: any) => Promise<void>;
saveFileToDisk: (path: string, contents: string) => Promise<void>;
readTextFile: (path: string) => Promise<string>;
isFolder: (dirPath: string) => Promise<boolean>;
moveFile: (oldPath: string, newPath: string) => Promise<void>;

View file

@ -1,5 +1,5 @@
import ElectronAPIs from "@/next/electron";
import LinkButton from "@ente/shared/components/LinkButton";
import ElectronAPIs from "@ente/shared/electron";
import { logError } from "@ente/shared/sentry";
import { Tooltip } from "@mui/material";
import { styled } from "@mui/material/styles";

View file

@ -7,7 +7,7 @@ import { getActualKey } from "@ente/shared/user";
import { KeyAttributes } from "@ente/shared/user/types";
import isElectron from "is-electron";
import ComlinkCryptoWorker from ".";
import ElectronAPIs from "../electron";
import ElectronAPIs from "@/next/electron";
import { addLogLine } from "../logging";
const LOGIN_SUB_KEY_LENGTH = 32;

View file

@ -1,4 +1,4 @@
import { DataStream } from "@ente/shared/upload/types";
import { DataStream } from "@/next/types/file";
export interface LocalFileAttributes<
T extends string | Uint8Array | DataStream,

View file

@ -1,5 +0,0 @@
import { ElectronAPIsType } from "./types";
const ElectronAPIs: ElectronAPIsType = globalThis["ElectronAPIs"];
export default ElectronAPIs;

View file

@ -1,7 +1,7 @@
import { inWorker, isDevBuild } from "@/next/env";
import { logError } from "@ente/shared/sentry";
import isElectron from "is-electron";
import ElectronAPIs from "../electron";
import ElectronAPIs from "@/next/electron";
import { workerBridge } from "../worker/worker-bridge";
import { formatLog, logWeb } from "./web";

View file

@ -1,4 +1,5 @@
import { isDevBuild } from "@/next/env";
import { ElectronFile } from "@/next/types/file";
import { logError } from "@ente/shared/sentry";
import {
LS_KEYS,
@ -8,7 +9,6 @@ import {
} from "@ente/shared/storage/localStorage";
import { addLogLine } from ".";
import { formatDateTimeShort } from "../time/format";
import { ElectronFile } from "../upload/types";
import type { User } from "../user/types";
import { convertBytesToHumanReadable } from "../utils/size";

View file

@ -1,4 +0,0 @@
export enum UPLOAD_STRATEGY {
SINGLE_COLLECTION,
COLLECTION_PER_FOLDER,
}

View file

@ -1,24 +0,0 @@
import { UPLOAD_STRATEGY } from "@ente/shared/upload/constants";
import { ElectronFile } from "@ente/shared/upload/types";
export interface WatchMappingSyncedFile {
path: string;
uploadedFileID: number;
collectionID: number;
}
export interface WatchMapping {
rootFolderName: string;
folderPath: string;
uploadStrategy: UPLOAD_STRATEGY;
syncedFiles: WatchMappingSyncedFile[];
ignoredFiles: string[];
}
export interface EventQueueItem {
type: "upload" | "trash";
folderPath: string;
collectionName?: string;
paths?: string[];
files?: ElectronFile[];
}

View file

@ -1,6 +1,6 @@
import { addLocalLog, logToDisk } from "@ente/shared/logging";
import { Remote, expose, wrap } from "comlink";
import ElectronAPIs from "../electron";
import ElectronAPIs from "@/next/electron";
import { logError } from "../sentry";
export class ComlinkWorker<T extends new () => InstanceType<T>> {