Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathid92838d9f24a
diff --git a/web/apps/photos/src/components/Sidebar/Preferences.tsx b/web/apps/photos/src/components/Sidebar/Preferences.tsx
index 9be4d0e2a0..a90747d0c8 100644
--- a/web/apps/photos/src/components/Sidebar/Preferences.tsx
+++ b/web/apps/photos/src/components/Sidebar/Preferences.tsx
@@ -114,8 +114,14 @@ const LanguageSelector = () => {
const updateCurrentLocale = (newLocale: SupportedLocale) => {
setLocaleInUse(newLocale);
+ // [Note: Changing locale causes a full reload]
+ //
// A full reload is needed because we use the global `t` instance
// instead of the useTranslation hook.
+ //
+ // We also rely on this behaviour by caching various formatters in
+ // module static variables that not get updated if the i18n.language
+ // changes unless there is a full reload.
window.location.reload();
};
diff --git a/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx b/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx
index be05fda6cf..a4468f3b2d 100644
--- a/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx
+++ b/web/apps/photos/src/components/pages/gallery/PreviewCard.tsx
@@ -1,4 +1,5 @@
import { Overlay } from "@/base/components/containers";
+import { formatDateRelative } from "@/base/i18n-date";
import log from "@/base/log";
import { downloadManager } from "@/gallery/services/download";
import { FileType } from "@/media/file-type";
@@ -17,7 +18,6 @@ import FavoriteRoundedIcon from "@mui/icons-material/FavoriteRounded";
import PlayCircleOutlineOutlinedIcon from "@mui/icons-material/PlayCircleOutlineOutlined";
import { styled } from "@mui/material";
import type { DisplayFile } from "components/PhotoFrame";
-import i18n from "i18next";
import { GalleryContext } from "pages/gallery";
import React, { useContext, useEffect, useRef, useState } from "react";
import { shouldShowAvatar } from "utils/file";
@@ -366,6 +366,7 @@ export default function PreviewCard(props: IProps) {
);
}
+/*
function formatDateRelative(date: number) {
const units = {
year: 24 * 60 * 60 * 1000 * 365,
@@ -389,3 +390,4 @@ function formatDateRelative(date: number) {
u as Intl.RelativeTimeFormatUnit,
);
}
+*/
diff --git a/web/packages/base/i18n-date.ts b/web/packages/base/i18n-date.ts
new file mode 100644
index 0000000000..de99b12829
--- /dev/null
+++ b/web/packages/base/i18n-date.ts
@@ -0,0 +1,37 @@
+/**
+ * @file Various date formatters.
+ *
+ * Note that we rely on the current behaviour of a full reload on changing the
+ * language. See: [Note: Changing locale causes a full reload].
+ */
+
+import i18n from "i18next";
+
+let _relativeDateFormat: Intl.RelativeTimeFormat | undefined;
+
+export const formatDateRelative = (epochMilliseconds: number) => {
+ const units = {
+ year: 24 * 60 * 60 * 1000 * 365,
+ month: (24 * 60 * 60 * 1000 * 365) / 12,
+ day: 24 * 60 * 60 * 1000,
+ hour: 60 * 60 * 1000,
+ minute: 60 * 1000,
+ second: 1000,
+ };
+ const relativeDateFormat = (_relativeDateFormat ??=
+ new Intl.RelativeTimeFormat(i18n.language, {
+ localeMatcher: "best fit",
+ numeric: "always",
+ style: "long",
+ }));
+
+ // "Math.abs" accounts for both past and future scenarios.
+ const elapsed = Math.abs(epochMilliseconds - Date.now());
+
+ for (const u in units)
+ if (elapsed > units[u] || u === "second")
+ return relativeDateFormat.format(
+ Math.round(elapsed / units[u]),
+ u as Intl.RelativeTimeFormatUnit,
+ );
+};