Skip to content

Commit

Permalink
feat: store multiple tokens (#3272)
Browse files Browse the repository at this point in the history
* feat: store multiple tokens

Signed-off-by: Prakhar Agarwal <[email protected]>

* fix: update persistent state property names, update test

Signed-off-by: Prakhar Agarwal <[email protected]>

* fix: current token should be present in all tokens when migrating from schema 6 to 7

Signed-off-by: Prakhar Agarwal <[email protected]>

---------

Signed-off-by: Prakhar Agarwal <[email protected]>
  • Loading branch information
Prakhar-Agarwal-byte authored and dolcalmi committed Nov 5, 2024
1 parent 9aa9fe8 commit ef6c823
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .storybook/views/story-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const PersistentStateWrapper: React.FC<React.PropsWithChildren> = ({ children })
<PersistentStateContext.Provider
value={{
persistentState: {
schemaVersion: 6,
schemaVersion: 7,
galoyInstance: {
id: "Main",
},
galoyAuthToken: "",
galoyAllAuthTokens: [],
},
updateState: () => {},
resetState: () => {},
Expand Down
15 changes: 8 additions & 7 deletions __tests__/persistent-storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
9 changes: 8 additions & 1 deletion app/hooks/use-app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -35,6 +40,7 @@ export const useAppConfig = () => {
return {
...state,
galoyAuthToken: token,
galoyAllAuthTokens: [...state.galoyAllAuthTokens, token],
}
return undefined
})
Expand All @@ -50,6 +56,7 @@ export const useAppConfig = () => {
...state,
galoyInstance: instance,
galoyAuthToken: token,
galoyAllAuthTokens: [...state.galoyAllAuthTokens, token],
}
return undefined
})
Expand Down
26 changes: 22 additions & 4 deletions app/store/persistent-state/state-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,24 @@ type PersistentState_6 = {
galoyAuthToken: string
}

const migrate6ToCurrent = (state: PersistentState_6): Promise<PersistentState> =>
type PersistentState_7 = {
schemaVersion: 7
galoyInstance: GaloyInstanceInput
galoyAuthToken: string
galoyAllAuthTokens: string[]
}

const migrate7ToCurrent = (state: PersistentState_7): Promise<PersistentState> =>
Promise.resolve(state)

const migrate6ToCurrent = (state: PersistentState_6): Promise<PersistentState> => {
return migrate7ToCurrent({
...state,
schemaVersion: 7,
galoyAllAuthTokens: [state.galoyAuthToken],
})
}

const migrate5ToCurrent = (state: PersistentState_5): Promise<PersistentState> => {
return migrate6ToCurrent({
...state,
Expand Down Expand Up @@ -98,21 +113,24 @@ type StateMigrations = {
4: (state: PersistentState_4) => Promise<PersistentState>
5: (state: PersistentState_5) => Promise<PersistentState>
6: (state: PersistentState_6) => Promise<PersistentState>
7: (state: PersistentState_7) => Promise<PersistentState>
}

const stateMigrations: StateMigrations = {
3: migrate3ToCurrent,
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 (
Expand All @@ -122,7 +140,7 @@ export const migrateAndGetPersistentState = async (
data: any,
): Promise<PersistentState> => {
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)
Expand Down

0 comments on commit ef6c823

Please sign in to comment.