From a3e77276df1d16d01e9d3600b7e55c7cd653d48f Mon Sep 17 00:00:00 2001 From: ~latter-bolden Date: Thu, 5 Dec 2024 18:51:29 -0600 Subject: [PATCH] key value: add createStorageItem config for persist or not after logout --- packages/app/hooks/useHandleLogout.native.ts | 4 +-- packages/app/hooks/useHandleLogout.ts | 3 +- packages/shared/src/db/keyValue.ts | 31 ++++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/packages/app/hooks/useHandleLogout.native.ts b/packages/app/hooks/useHandleLogout.native.ts index 3ff963c1f9..22ace8d1f4 100644 --- a/packages/app/hooks/useHandleLogout.native.ts +++ b/packages/app/hooks/useHandleLogout.native.ts @@ -1,6 +1,6 @@ import { createDevLogger } from '@tloncorp/shared'; import * as api from '@tloncorp/shared/api'; -import { finishingSelfHostedLogin } from '@tloncorp/shared/db'; +import { clearNonPersistentStorageItems } from '@tloncorp/shared/db'; import * as store from '@tloncorp/shared/store'; import { useCallback } from 'react'; @@ -28,7 +28,7 @@ export function useHandleLogout({ resetDb }: { resetDb: () => void }) { clearDeepLink(); clearSplashDismissed(); clearTelemetry(); - finishingSelfHostedLogin.resetValue(); + clearNonPersistentStorageItems(); if (!resetDb) { logger.trackError('could not reset db on logout'); return; diff --git a/packages/app/hooks/useHandleLogout.ts b/packages/app/hooks/useHandleLogout.ts index 54a52b374d..8cb2827909 100644 --- a/packages/app/hooks/useHandleLogout.ts +++ b/packages/app/hooks/useHandleLogout.ts @@ -4,6 +4,7 @@ // which isn't made for web. import { createDevLogger } from '@tloncorp/shared'; import * as api from '@tloncorp/shared/api'; +import { clearNonPersistentStorageItems } from '@tloncorp/shared/db'; import * as store from '@tloncorp/shared/store'; import { useCallback } from 'react'; @@ -33,7 +34,7 @@ export function useHandleLogout({ resetDb }: { resetDb?: () => void }) { removeHostingUserId(); removeHostingAuthTracking(); clearSplashDismissed(); - // signupContext.clear(); + clearNonPersistentStorageItems(); if (!resetDb) { logger.trackError('could not reset db on logout'); return; diff --git a/packages/shared/src/db/keyValue.ts b/packages/shared/src/db/keyValue.ts index 6e613f1d0e..be89ee3778 100644 --- a/packages/shared/src/db/keyValue.ts +++ b/packages/shared/src/db/keyValue.ts @@ -188,14 +188,16 @@ type StorageItem = { key: string; defaultValue: T; isSecure?: boolean; + persistAfterLogout?: boolean; serialize?: (value: T) => string; deserialize?: (value: string) => T; }; -const createStorageItem = (config: StorageItem) => { +const createStorageItemImpl = (config: StorageItem) => { const { key, defaultValue, + persistAfterLogout = false, serialize = JSON.stringify, deserialize = JSON.parse, } = config; @@ -248,7 +250,30 @@ const createStorageItem = (config: StorageItem) => { }; } - return { getValue, setValue, resetValue, useValue, useStorageItem }; + return { + getValue, + setValue, + resetValue, + useValue, + useStorageItem, + __persistAfterLogout: persistAfterLogout, + }; +}; + +const storageItems: Array>> = []; +export const clearNonPersistentStorageItems = async (): Promise => { + const clearPromises = storageItems + .filter((item) => !item.__persistAfterLogout) + .map((item) => item.resetValue()); + + await Promise.all(clearPromises); + logger.log('Cleared all non-persistent storage items'); +}; + +const createStorageItem = (config: StorageItem) => { + const storageItem = createStorageItemImpl(config); + storageItems.push(storageItem); + return storageItem; }; export const signupData = createStorageItem({ @@ -263,11 +288,13 @@ export const signupData = createStorageItem({ export const lastAppVersion = createStorageItem({ key: 'lastAppVersion', defaultValue: null, + persistAfterLogout: true, }); export const didSignUp = createStorageItem({ key: 'didSignUp', defaultValue: false, + persistAfterLogout: true, }); export const didInitializeTelemetry = createStorageItem({