Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Consolidate Web compatibility declarations and loaders

Manav Rathibf142077395e
diff --git a/web/apps/albums/src/public-album/components/TripLayout/map-helpers.ts b/web/apps/albums/src/public-album/components/TripLayout/map-helpers.ts
index 099873887d..698a09e0dd 100644
--- a/web/apps/albums/src/public-album/components/TripLayout/map-helpers.ts
+++ b/web/apps/albums/src/public-album/components/TripLayout/map-helpers.ts
@@ -1,4 +1,4 @@
-import { haveWindow } from "ente-base/env";
+import { getLeaflet } from "ente-gallery/utils/leaflet";
import type { JourneyPoint } from "./types";
import { iconCache } from "./utils/geocoding";
@@ -8,15 +8,6 @@ const isMobileDevice = () => {
return window.innerWidth < 960;
};
-// Leaflet reads window during import.
-const getLeaflet = () => {
- if (haveWindow()) {
- // eslint-disable-next-line @typescript-eslint/no-require-imports
- return require("leaflet") as typeof import("leaflet");
- }
- return null;
-};
-
// Haversine distance in kilometers.
export const calculateDistance = (
lat1: number,
diff --git a/web/apps/photos/src/components/Collections/CollectionMapDialog.tsx b/web/apps/photos/src/components/Collections/CollectionMapDialog.tsx
index 1fb1c1fe3c..9b76cb62ae 100644
--- a/web/apps/photos/src/components/Collections/CollectionMapDialog.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionMapDialog.tsx
@@ -23,6 +23,7 @@ import { isSxArray } from "ente-base/components/utils/sx";
import { useBaseContext } from "ente-base/context";
import { downloadManager } from "ente-gallery/services/download";
import { uniqueFilesByID } from "ente-gallery/utils/file";
+import { getLeaflet } from "ente-gallery/utils/leaflet";
import type { EnteFile } from "ente-media/file";
import {
fileCreationPhotoSortTime,
@@ -902,10 +903,8 @@ function createMarkerIcon(
clusterCount?: number,
interactive = true,
): import("leaflet").DivIcon | null {
- if (typeof window === "undefined") return null;
-
- // eslint-disable-next-line @typescript-eslint/no-require-imports
- const leaflet = require("leaflet") as typeof import("leaflet");
+ const leaflet = getLeaflet();
+ if (!leaflet) return null;
const pinSize = 84;
const triangleHeight = 10;
diff --git a/web/apps/photos/src/types/chromecast-sender.d.ts b/web/apps/photos/src/types/chromecast-sender.d.ts
new file mode 100644
index 0000000000..3c626719a3
--- /dev/null
+++ b/web/apps/photos/src/types/chromecast-sender.d.ts
@@ -0,0 +1,37 @@
+// Sender and receiver @types conflict, so declare only the sender APIs used here.
+
+declare global {
+ interface Window {
+ cast: typeof cast;
+ chrome?: unknown;
+ __onGCastApiAvailable(available: boolean, reason?: string): void;
+ }
+}
+
+export declare namespace chrome.cast {
+ export enum AutoJoinPolicy {
+ ORIGIN_SCOPED = "origin_scoped",
+ }
+}
+
+export declare namespace cast.framework {
+ interface CastOptions {
+ autoJoinPolicy: chrome.cast.AutoJoinPolicy;
+ receiverApplicationId?: string | undefined;
+ }
+
+ class CastContext {
+ static getInstance(): CastContext;
+ setOptions(options: CastOptions): void;
+ requestSession(): Promise<unknown>;
+ getCurrentSession(): CastSession | null;
+ }
+
+ class CastSession {
+ sendMessage(namespace: string, data: unknown): Promise<unknown>;
+ addMessageListener(
+ namespace: string,
+ listener: (namespace: string, message: string) => void,
+ ): void;
+ }
+}
diff --git a/web/apps/photos/src/utils/chromecast-sender.ts b/web/apps/photos/src/utils/chromecast-sender.ts
index f200f233ed..50808819fd 100644
--- a/web/apps/photos/src/utils/chromecast-sender.ts
+++ b/web/apps/photos/src/utils/chromecast-sender.ts
@@ -1,41 +1,5 @@
-/* eslint-disable @typescript-eslint/no-namespace */
-// Sender and receiver @types conflict, so declare only the sender APIs used here.
-
-declare global {
- interface Window {
- cast: typeof cast;
- chrome?: unknown;
- __onGCastApiAvailable(available: boolean, reason?: string): void;
- }
-}
-
-declare namespace chrome.cast {
- export enum AutoJoinPolicy {
- ORIGIN_SCOPED = "origin_scoped",
- }
-}
-
-declare namespace cast.framework {
- interface CastOptions {
- autoJoinPolicy: chrome.cast.AutoJoinPolicy;
- receiverApplicationId?: string | undefined;
- }
-
- class CastContext {
- static getInstance(): CastContext;
- setOptions(options: CastOptions): void;
- requestSession(): Promise<unknown>;
- getCurrentSession(): CastSession | null;
- }
-
- class CastSession {
- sendMessage(namespace: string, data: unknown): Promise<unknown>;
- addMessageListener(
- namespace: string,
- listener: (namespace: string, message: string) => void,
- ): void;
- }
-}
+declare const cast: typeof import("../types/chromecast-sender").cast;
+declare const chrome: typeof import("../types/chromecast-sender").chrome;
export const loadCast = (() => {
let promise: Promise<typeof cast> | undefined;
diff --git a/web/packages/gallery/components/FileInfo.tsx b/web/packages/gallery/components/FileInfo.tsx
index 2e5996f8d4..96b535dda5 100644
--- a/web/packages/gallery/components/FileInfo.tsx
+++ b/web/packages/gallery/components/FileInfo.tsx
@@ -45,13 +45,13 @@ import {
type ModalVisibilityProps,
} from "ente-base/components/utils/modal";
import { useBaseContext } from "ente-base/context";
-import { haveWindow } from "ente-base/env";
import { nameAndExtension } from "ente-base/file-name";
import { formattedDate, formattedTime } from "ente-base/i18n-date";
import log from "ente-base/log";
import type { Location } from "ente-base/types";
import { CopyButton } from "ente-gallery/components/FileInfoComponents";
import { tagNumericValue, type RawExifTags } from "ente-gallery/services/exif";
+import { getLeafletWithDefaultIcons } from "ente-gallery/utils/leaflet";
import { formattedByteSize } from "ente-gallery/utils/units";
import type { EnteFile } from "ente-media/file";
import {
@@ -90,13 +90,7 @@ import { Trans } from "react-i18next";
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.webpack.css";
import "leaflet/dist/leaflet.css";
-// Reuse Leaflet's bundled marker images.
-// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-expressions
-haveWindow() && require("leaflet-defaulticon-compatibility");
-const leaflet = haveWindow()
- ? // eslint-disable-next-line @typescript-eslint/no-require-imports
- (require("leaflet") as typeof import("leaflet"))
- : null;
+const leaflet = getLeafletWithDefaultIcons();
// TODO: Indicate missing exif (e.g. videos) better, both in the data type, and
// in the UI (e.g. by omitting the entire row).
diff --git a/web/packages/gallery/services/save-core.ts b/web/packages/gallery/services/save-core.ts
index 6eb9178332..a07328e88a 100644
--- a/web/packages/gallery/services/save-core.ts
+++ b/web/packages/gallery/services/save-core.ts
@@ -421,13 +421,6 @@ const saveAsZip = async (
const file = files[currentIndex]!;
try {
- // Event handlers can change this between awaits.
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
- if (networkState.isOffline) {
- onError(file, new Error("Network offline"));
- return false;
- }
-
const downloadedData = await downloadFileForZip(downloader, file);
await withZipLock(async () => {
diff --git a/web/packages/gallery/utils/leaflet.ts b/web/packages/gallery/utils/leaflet.ts
new file mode 100644
index 0000000000..bfa17e8e2f
--- /dev/null
+++ b/web/packages/gallery/utils/leaflet.ts
@@ -0,0 +1,15 @@
+import { haveWindow } from "ente-base/env";
+
+// Leaflet reads window during import.
+export const getLeaflet = () => {
+ if (!haveWindow()) return null;
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ return require("leaflet") as typeof import("leaflet");
+};
+
+export const getLeafletWithDefaultIcons = () => {
+ if (!haveWindow()) return null;
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ require("leaflet-defaulticon-compatibility");
+ return getLeaflet();
+};
diff --git a/web/packages/gallery/utils/native-stream.ts b/web/packages/gallery/utils/native-stream.ts
index 7a24944241..7a8e7a6661 100644
--- a/web/packages/gallery/utils/native-stream.ts
+++ b/web/packages/gallery/utils/native-stream.ts
@@ -2,6 +2,13 @@ import type { Electron, ElectronMLWorker, ZipItem } from "ente-base/types/ipc";
import { z } from "zod";
import type { FileSystemUploadItem } from "../services/upload";
+declare global {
+ interface RequestInit {
+ // Chromium requires this for streamed request bodies.
+ duplex?: "half";
+ }
+}
+
// Electron arguments prove these are desktop calls.
// The native protocol handlers do not otherwise use them.
export const readStream = async (
@@ -52,8 +59,6 @@ export const writeStream = async (
const req = new Request(url, {
method: "POST",
- // Chromium requires duplex for streamed request bodies.
- // @ts-expect-error duplex is missing from lib.dom.d.ts.
duplex: "half",
body: stream,
});
@@ -118,8 +123,6 @@ export const initiateGenerateHLS = async (
const res = await fetch(url, {
method: "POST",
headers: { "X-Auth-Token": authToken },
- // Chromium requires duplex for streamed request bodies.
- // @ts-expect-error duplex is missing from lib.dom.d.ts.
duplex: "half",
body,
});
diff --git a/web/packages/new/photos/components/EditLocationDialog.tsx b/web/packages/new/photos/components/EditLocationDialog.tsx
index ef945f2a4b..a7a8b8bdb6 100644
--- a/web/packages/new/photos/components/EditLocationDialog.tsx
+++ b/web/packages/new/photos/components/EditLocationDialog.tsx
@@ -18,8 +18,8 @@ import {
} from "@mui/material";
import { useIsSmallWidth } from "ente-base/components/utils/hooks";
import type { ModalVisibilityProps } from "ente-base/components/utils/modal";
-import { haveWindow } from "ente-base/env";
import type { Location } from "ente-base/types";
+import { getLeafletWithDefaultIcons } from "ente-gallery/utils/leaflet";
import type { EnteFile } from "ente-media/file";
import { fileLocation } from "ente-media/file-metadata";
import { t } from "i18next";
@@ -27,12 +27,7 @@ import React, { useEffect, useMemo, useRef, useState } from "react";
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.webpack.css";
import "leaflet/dist/leaflet.css";
-// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-expressions
-haveWindow() && require("leaflet-defaulticon-compatibility");
-const leaflet = haveWindow()
- ? // eslint-disable-next-line @typescript-eslint/no-require-imports
- (require("leaflet") as typeof import("leaflet"))
- : null;
+const leaflet = getLeafletWithDefaultIcons();
export interface EditLocationDialogProps extends ModalVisibilityProps {
files: EnteFile[];