Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store multiple tokens #3272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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