From 7b28b9aa0fed567a95f6735ada181fa7f7ee897a Mon Sep 17 00:00:00 2001 From: Prakhar Agarwal Date: Fri, 17 May 2024 20:01:20 +0530 Subject: [PATCH 1/3] feat: store multiple tokens Signed-off-by: Prakhar Agarwal --- .storybook/views/story-screen.tsx | 3 ++- app/hooks/use-app-config.ts | 8 +++++- .../persistent-state/state-migrations.ts | 26 ++++++++++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/.storybook/views/story-screen.tsx b/.storybook/views/story-screen.tsx index 631f86398c..bcb0dd85f3 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/app/hooks/use-app-config.ts b/app/hooks/use-app-config.ts index 88d4765a40..58b9d429f1 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), + savedAccounts: persistentState.galoySavedAccounts, }), - [persistentState.galoyAuthToken, persistentState.galoyInstance], + [ + persistentState.galoyAuthToken, + persistentState.galoyInstance, + persistentState.galoySavedAccounts, + ], ) const setGaloyInstance = useCallback( @@ -35,6 +40,7 @@ export const useAppConfig = () => { return { ...state, galoyAuthToken: token, + galoySavedAccounts: [...state.galoySavedAccounts, token], } return undefined }) diff --git a/app/store/persistent-state/state-migrations.ts b/app/store/persistent-state/state-migrations.ts index 64af8ecb4b..39be7209b7 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 + galoySavedAccounts: string[] +} + +const migrate7ToCurrent = (state: PersistentState_7): Promise => Promise.resolve(state) +const migrate6ToCurrent = (state: PersistentState_6): Promise => { + return migrate7ToCurrent({ + ...state, + schemaVersion: 7, + galoySavedAccounts: [], + }) +} + 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: "", + galoySavedAccounts: [], } 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) From 6f2d81b1b0383fb7aab8e28388c6214388969b27 Mon Sep 17 00:00:00 2001 From: Prakhar Agarwal Date: Sun, 19 May 2024 17:22:26 +0530 Subject: [PATCH 2/3] fix: update persistent state property names, update test Signed-off-by: Prakhar Agarwal --- .storybook/views/story-screen.tsx | 2 +- __tests__/persistent-storage.spec.ts | 15 ++++++++------- app/hooks/use-app-config.ts | 7 ++++--- app/store/persistent-state/state-migrations.ts | 6 +++--- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.storybook/views/story-screen.tsx b/.storybook/views/story-screen.tsx index bcb0dd85f3..8871a7798a 100644 --- a/.storybook/views/story-screen.tsx +++ b/.storybook/views/story-screen.tsx @@ -10,7 +10,7 @@ const PersistentStateWrapper: React.FC = ({ children }) id: "Main", }, galoyAuthToken: "", - galoySavedAccounts: [], + galoyAllAuthTokens: [], }, updateState: () => {}, resetState: () => {}, diff --git a/__tests__/persistent-storage.spec.ts b/__tests__/persistent-storage.spec.ts index 6f8888797b..c57daab7fe 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: [], } - 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 58b9d429f1..9653bc4e44 100644 --- a/app/hooks/use-app-config.ts +++ b/app/hooks/use-app-config.ts @@ -10,12 +10,12 @@ export const useAppConfig = () => { () => ({ token: persistentState.galoyAuthToken, galoyInstance: resolveGaloyInstanceOrDefault(persistentState.galoyInstance), - savedAccounts: persistentState.galoySavedAccounts, + savedAccounts: persistentState.galoyAllAuthTokens, }), [ persistentState.galoyAuthToken, persistentState.galoyInstance, - persistentState.galoySavedAccounts, + persistentState.galoyAllAuthTokens, ], ) @@ -40,7 +40,7 @@ export const useAppConfig = () => { return { ...state, galoyAuthToken: token, - galoySavedAccounts: [...state.galoySavedAccounts, token], + galoyAllAuthTokens: [...state.galoyAllAuthTokens, token], } return undefined }) @@ -56,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 39be7209b7..ffc16efff3 100644 --- a/app/store/persistent-state/state-migrations.ts +++ b/app/store/persistent-state/state-migrations.ts @@ -34,7 +34,7 @@ type PersistentState_7 = { schemaVersion: 7 galoyInstance: GaloyInstanceInput galoyAuthToken: string - galoySavedAccounts: string[] + galoyAllAuthTokens: string[] } const migrate7ToCurrent = (state: PersistentState_7): Promise => @@ -44,7 +44,7 @@ const migrate6ToCurrent = (state: PersistentState_6): Promise = return migrate7ToCurrent({ ...state, schemaVersion: 7, - galoySavedAccounts: [], + galoyAllAuthTokens: [], }) } @@ -130,7 +130,7 @@ export const defaultPersistentState: PersistentState = { schemaVersion: 7, galoyInstance: { id: "Main" }, galoyAuthToken: "", - galoySavedAccounts: [], + galoyAllAuthTokens: [], } export const migrateAndGetPersistentState = async ( From 2e7f7ea4ca4c83a8abb16d745f34fc49ae18865d Mon Sep 17 00:00:00 2001 From: Prakhar Agarwal Date: Sat, 15 Jun 2024 20:16:20 +0530 Subject: [PATCH 3/3] fix: current token should be present in all tokens when migrating from schema 6 to 7 Signed-off-by: Prakhar Agarwal --- __tests__/persistent-storage.spec.ts | 2 +- app/hooks/use-app-config.ts | 2 +- app/store/persistent-state/state-migrations.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/__tests__/persistent-storage.spec.ts b/__tests__/persistent-storage.spec.ts index c57daab7fe..d8f02e3732 100644 --- a/__tests__/persistent-storage.spec.ts +++ b/__tests__/persistent-storage.spec.ts @@ -36,7 +36,7 @@ it("migration from 6 to 7", async () => { schemaVersion: 7, galoyInstance: { id: "Main" }, galoyAuthToken: "myToken", - galoyAllAuthTokens: [], + galoyAllAuthTokens: ["myToken"], } const res = await migrateAndGetPersistentState(state6) diff --git a/app/hooks/use-app-config.ts b/app/hooks/use-app-config.ts index 9653bc4e44..ccc363f603 100644 --- a/app/hooks/use-app-config.ts +++ b/app/hooks/use-app-config.ts @@ -10,7 +10,7 @@ export const useAppConfig = () => { () => ({ token: persistentState.galoyAuthToken, galoyInstance: resolveGaloyInstanceOrDefault(persistentState.galoyInstance), - savedAccounts: persistentState.galoyAllAuthTokens, + allTokens: persistentState.galoyAllAuthTokens, }), [ persistentState.galoyAuthToken, diff --git a/app/store/persistent-state/state-migrations.ts b/app/store/persistent-state/state-migrations.ts index ffc16efff3..a40df47683 100644 --- a/app/store/persistent-state/state-migrations.ts +++ b/app/store/persistent-state/state-migrations.ts @@ -44,7 +44,7 @@ const migrate6ToCurrent = (state: PersistentState_6): Promise = return migrate7ToCurrent({ ...state, schemaVersion: 7, - galoyAllAuthTokens: [], + galoyAllAuthTokens: [state.galoyAuthToken], }) }