From cf3728eee80e1ce757e85e318299eeda9b6bce6d Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 1 Jul 2024 13:46:48 +0530 Subject: [PATCH] Migrate --- web/apps/accounts/src/pages/_app.tsx | 2 +- web/apps/auth/src/pages/_app.tsx | 11 ++++++++--- web/apps/photos/src/pages/_app.tsx | 11 ++++++++--- .../shared/storage/localStorage/index.ts | 18 ++++++++++++++++-- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/web/apps/accounts/src/pages/_app.tsx b/web/apps/accounts/src/pages/_app.tsx index 845ac235f4..7029cacd86 100644 --- a/web/apps/accounts/src/pages/_app.tsx +++ b/web/apps/accounts/src/pages/_app.tsx @@ -33,7 +33,7 @@ const App: React.FC = ({ Component, pageProps }) => { useEffect(() => { disableDiskLogs(); // The accounts app has no local state, but some older builds might've - // leftover some scraps. Clear it out. Thi code added 1 July 2024, can + // leftover some scraps. Clear it out. This code added 1 July 2024, can // be removed after a while (tag: Migration). clearData(); void setupI18n().finally(() => setIsI18nReady(true)); diff --git a/web/apps/auth/src/pages/_app.tsx b/web/apps/auth/src/pages/_app.tsx index 113d851dc2..1f1ba88863 100644 --- a/web/apps/auth/src/pages/_app.tsx +++ b/web/apps/auth/src/pages/_app.tsx @@ -17,7 +17,11 @@ import { AppNavbar } from "@ente/shared/components/Navbar/app"; import { PHOTOS_PAGES as PAGES } from "@ente/shared/constants/pages"; import { useLocalState } from "@ente/shared/hooks/useLocalState"; import HTTPService from "@ente/shared/network/HTTPService"; -import { LS_KEYS, getData } from "@ente/shared/storage/localStorage"; +import { + LS_KEYS, + getData, + setKVToken, +} from "@ente/shared/storage/localStorage"; import { getTheme } from "@ente/shared/themes"; import { THEME_COLOR } from "@ente/shared/themes/constants"; import type { User } from "@ente/shared/user/types"; @@ -75,8 +79,9 @@ const App: React.FC = ({ Component, pageProps }) => { useEffect(() => { void setupI18n().finally(() => setIsI18nReady(true)); - const userID = (getData(LS_KEYS.USER) as User)?.id; - logStartupBanner(userID); + const user = getData(LS_KEYS.USER) as User | undefined | null; + setKVToken(user); + logStartupBanner(user?.id); HTTPService.setHeaders({ "X-Client-Package": clientPackageName }); logUnhandledErrorsAndRejections(true); return () => logUnhandledErrorsAndRejections(false); diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx index d000858522..4c90736132 100644 --- a/web/apps/photos/src/pages/_app.tsx +++ b/web/apps/photos/src/pages/_app.tsx @@ -23,7 +23,11 @@ import { AppNavbar } from "@ente/shared/components/Navbar/app"; import { PHOTOS_PAGES as PAGES } from "@ente/shared/constants/pages"; import { useLocalState } from "@ente/shared/hooks/useLocalState"; import HTTPService from "@ente/shared/network/HTTPService"; -import { LS_KEYS, getData } from "@ente/shared/storage/localStorage"; +import { + LS_KEYS, + getData, + setKVToken, +} from "@ente/shared/storage/localStorage"; import { getLocalMapEnabled, getToken, @@ -140,8 +144,9 @@ export default function App({ Component, pageProps }: AppProps) { useEffect(() => { void setupI18n().finally(() => setIsI18nReady(true)); - const userID = (getData(LS_KEYS.USER) as User)?.id; - logStartupBanner(userID); + const user = getData(LS_KEYS.USER) as User | undefined | null; + setKVToken(user); + logStartupBanner(user?.id); HTTPService.setHeaders({ "X-Client-Package": clientPackageName }); logUnhandledErrorsAndRejections(true); return () => logUnhandledErrorsAndRejections(false); diff --git a/web/packages/shared/storage/localStorage/index.ts b/web/packages/shared/storage/localStorage/index.ts index 64f0423064..f2c33a3f2c 100644 --- a/web/packages/shared/storage/localStorage/index.ts +++ b/web/packages/shared/storage/localStorage/index.ts @@ -54,9 +54,23 @@ export const clearData = () => localStorage.clear(); // // Creating a new function here to act as a funnel point. export const setLSUser = async (user: object) => { - const token = user["token"]; + await setKVToken(user); + setData(LS_KEYS.USER, user); +}; + +/** + * Update the "token" KV with the token (if any) for the given {@link user}. + * + * This is an internal implementation details of {@link setLSUser} and doesn't + * need to exposed conceptually. For now though, we need to call this externally + * at an early point in the app startup to also copy over the token into KV DB + * for existing users. + * + * This was added 1 July 2024, can be removed after a while (tag: Migration). + */ +export const setKVToken = async (user: unknown) => { + const token = user ? user["token"] : undefined; token && typeof token == "string" ? await setKV("token", token) : await removeKV("token"); - setData(LS_KEYS.USER, user); };