Skip to content

Commit

Permalink
remove null state
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Jul 5, 2024
1 parent 8e3d0f1 commit 579b061
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
17 changes: 8 additions & 9 deletions src/background/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const defaultStorage = {
* existing installations.
*/
version: 3,
state: null,
state: {},
connected: false,
enabled: true,
exceptionList: {},
Expand Down Expand Up @@ -127,10 +127,10 @@ export class StorageService {
return false
}

async setState(state: NonNullable<Storage['state']>): Promise<boolean> {
async setState(state: Storage['state']): Promise<boolean> {
const { state: prevState } = await this.get(['state'])

const newState: NonNullable<Storage['state']> = { ...prevState }
const newState: Storage['state'] = { ...prevState }
for (const key of Object.keys(state) as ExtensionState[]) {
newState[key] = state[key]
}
Expand Down Expand Up @@ -251,12 +251,11 @@ const MIGRATIONS: Record<Storage['version'], Migration> = {
return [data, deleteKeys]
},
3: (data) => {
const state = data.state
if (!state) {
data.state = null
} else if (typeof state === 'string') {
data.state = { [state]: true } satisfies Storage['state']
}
const newState =
data.state && typeof data.state === 'string'
? { [data.state as ExtensionState]: true }
: {}
data.state = newState satisfies Storage['state']
return [data]
}
}
2 changes: 1 addition & 1 deletion src/popup/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ROUTES_PATH } from '../Popup'
export const ProtectedRoute = () => {
const { state } = React.useContext(PopupStateContext)

if (state.state?.missing_host_permissions) {
if (state.state.missing_host_permissions) {
return <Navigate to={ROUTES_PATH.MISSING_HOST_PERMISSION} />
}
if (state.connected === false) {
Expand Down
7 changes: 2 additions & 5 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ export interface Storage {
enabled: boolean
/** If a wallet is connected or not */
connected: boolean
/**
* Extension state. null is an optimization (not guaranteed due to race
* conditions) implying empty object or all keys set to false.
*/
state: null | Partial<Record<ExtensionState, boolean>>
/** Extension state */
state: Partial<Record<ExtensionState, boolean>>

rateOfPay?: string | undefined | null
minRateOfPay?: string | undefined | null
Expand Down

0 comments on commit 579b061

Please sign in to comment.