From 9ccb59b50b901f7f6416b9d7a67c04747531a9c0 Mon Sep 17 00:00:00 2001 From: Prakhar Agarwal Date: Mon, 24 Jun 2024 11:57:25 +0530 Subject: [PATCH] feat: store multiple tokens (#3272) * feat: store multiple tokens Signed-off-by: Prakhar Agarwal * fix: update persistent state property names, update test Signed-off-by: Prakhar Agarwal * fix: current token should be present in all tokens when migrating from schema 6 to 7 Signed-off-by: Prakhar Agarwal --------- Signed-off-by: Prakhar Agarwal --- .storybook/views/story-screen.tsx | 3 ++- __tests__/persistent-storage.spec.ts | 15 ++++++----- app/hooks/use-app-config.ts | 9 ++++++- .../persistent-state/state-migrations.ts | 26 ++++++++++++++++--- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.storybook/views/story-screen.tsx b/.storybook/views/story-screen.tsx index 631f86398c..8871a7798a 100644 --- a/.storybook/views/story-screen.tsx +++ b/.storybook/views/story-screen.tsx @@ -5,11 +5,12 @@ const PersistentStateWrapper: React.FC = ({ children }) {}, resetState: () => {}, diff --git a/__tests__/persistent-storage.spec.ts b/__tests__/persistent-storage.spec.ts index 6f8888797b..d8f02e3732 100644 --- a/__tests__/persistent-storage.spec.ts +++ b/__tests__/persistent-storage.spec.ts @@ -25,20 +25,21 @@ it("returns default when schema is not present", async () => { expect(state).toEqual(defaultPersistentState) }) -it("migration from 5 to 6", async () => { - const state5 = { - schemaVersion: 5, +it("migration from 6 to 7", async () => { + const state6 = { + schemaVersion: 6, galoyInstance: { id: "Main" }, galoyAuthToken: "myToken", } - const state6 = { - schemaVersion: 6, + const state7 = { + schemaVersion: 7, galoyInstance: { id: "Main" }, galoyAuthToken: "myToken", + galoyAllAuthTokens: ["myToken"], } - const res = await migrateAndGetPersistentState(state5) + const res = await migrateAndGetPersistentState(state6) - expect(res).toStrictEqual(state6) + expect(res).toStrictEqual(state7) }) diff --git a/app/hooks/use-app-config.ts b/app/hooks/use-app-config.ts index 88d4765a40..ccc363f603 100644 --- a/app/hooks/use-app-config.ts +++ b/app/hooks/use-app-config.ts @@ -10,8 +10,13 @@ export const useAppConfig = () => { () => ({ token: persistentState.galoyAuthToken, galoyInstance: resolveGaloyInstanceOrDefault(persistentState.galoyInstance), + allTokens: persistentState.galoyAllAuthTokens, }), - [persistentState.galoyAuthToken, persistentState.galoyInstance], + [ + persistentState.galoyAuthToken, + persistentState.galoyInstance, + persistentState.galoyAllAuthTokens, + ], ) const setGaloyInstance = useCallback( @@ -35,6 +40,7 @@ export const useAppConfig = () => { return { ...state, galoyAuthToken: token, + galoyAllAuthTokens: [...state.galoyAllAuthTokens, token], } return undefined }) @@ -50,6 +56,7 @@ export const useAppConfig = () => { ...state, galoyInstance: instance, galoyAuthToken: token, + galoyAllAuthTokens: [...state.galoyAllAuthTokens, token], } return undefined }) diff --git a/app/store/persistent-state/state-migrations.ts b/app/store/persistent-state/state-migrations.ts index 64af8ecb4b..a40df47683 100644 --- a/app/store/persistent-state/state-migrations.ts +++ b/app/store/persistent-state/state-migrations.ts @@ -30,9 +30,24 @@ type PersistentState_6 = { galoyAuthToken: string } -const migrate6ToCurrent = (state: PersistentState_6): Promise => +type PersistentState_7 = { + schemaVersion: 7 + galoyInstance: GaloyInstanceInput + galoyAuthToken: string + galoyAllAuthTokens: string[] +} + +const migrate7ToCurrent = (state: PersistentState_7): Promise => Promise.resolve(state) +const migrate6ToCurrent = (state: PersistentState_6): Promise => { + return migrate7ToCurrent({ + ...state, + schemaVersion: 7, + galoyAllAuthTokens: [state.galoyAuthToken], + }) +} + const migrate5ToCurrent = (state: PersistentState_5): Promise => { return migrate6ToCurrent({ ...state, @@ -98,6 +113,7 @@ type StateMigrations = { 4: (state: PersistentState_4) => Promise 5: (state: PersistentState_5) => Promise 6: (state: PersistentState_6) => Promise + 7: (state: PersistentState_7) => Promise } const stateMigrations: StateMigrations = { @@ -105,14 +121,16 @@ const stateMigrations: StateMigrations = { 4: migrate4ToCurrent, 5: migrate5ToCurrent, 6: migrate6ToCurrent, + 7: migrate7ToCurrent, } -export type PersistentState = PersistentState_6 +export type PersistentState = PersistentState_7 export const defaultPersistentState: PersistentState = { - schemaVersion: 6, + schemaVersion: 7, galoyInstance: { id: "Main" }, galoyAuthToken: "", + galoyAllAuthTokens: [], } export const migrateAndGetPersistentState = async ( @@ -122,7 +140,7 @@ export const migrateAndGetPersistentState = async ( data: any, ): Promise => { if (Boolean(data) && data.schemaVersion in stateMigrations) { - const schemaVersion: 3 | 4 | 5 | 6 = data.schemaVersion + const schemaVersion: 3 | 4 | 5 | 6 | 7 = data.schemaVersion try { const migration = stateMigrations[schemaVersion] const persistentState = await migration(data)