Skip to content

Commit

Permalink
feat: store multiple tokens
Browse files Browse the repository at this point in the history
Signed-off-by: Prakhar Agarwal <[email protected]>
  • Loading branch information
Prakhar-Agarwal-byte committed Jun 14, 2024
1 parent 3571b60 commit 7b28b9a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 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: "",
galoySavedAccounts: [],
},
updateState: () => {},
resetState: () => {},
Expand Down
8 changes: 7 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),
savedAccounts: persistentState.galoySavedAccounts,
}),
[persistentState.galoyAuthToken, persistentState.galoyInstance],
[
persistentState.galoyAuthToken,
persistentState.galoyInstance,
persistentState.galoySavedAccounts,
],
)

const setGaloyInstance = useCallback(
Expand All @@ -35,6 +40,7 @@ export const useAppConfig = () => {
return {
...state,
galoyAuthToken: token,
galoySavedAccounts: [...state.galoySavedAccounts, 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
galoySavedAccounts: string[]
}

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

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

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: "",
galoySavedAccounts: [],
}

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 7b28b9a

Please sign in to comment.