Skip to content

Commit

Permalink
Persist color scheme prefence in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
jlfwong committed Nov 12, 2020
1 parent c46af71 commit b7af76b
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {actions} from './actions'
*/

import * as redux from 'redux'
import {setter, Reducer} from '../lib/typed-redux'
import {setter, Reducer, Action} from '../lib/typed-redux'
import {HashParams, getHashParams} from '../lib/hash-params'
import {ProfileGroupState, profileGroup} from './profiles-state'
import {SortMethod, SortField, SortDirection} from '../views/profile-table-view'
Expand Down Expand Up @@ -83,6 +83,47 @@ const protocol = window.location.protocol
// however, XHR will be unavailable to fetching files in adjacent directories.
export const canUseXHR = protocol === 'http:' || protocol === 'https:'

function colorScheme(state: ColorScheme | undefined, action: Action<any>): ColorScheme {
const localStorageKey = 'speedscope-color-scheme'

if (state === undefined) {
const storedPreference = window.localStorage && window.localStorage[localStorageKey]
if (storedPreference === 'DARK') {
return ColorScheme.DARK
} else if (storedPreference === 'LIGHT') {
return ColorScheme.LIGHT
} else {
return ColorScheme.SYSTEM
}
}

if (actions.setColorScheme.matches(action)) {
const value = action.payload

switch (value) {
case ColorScheme.DARK: {
window.localStorage[localStorageKey] = 'DARK'
break
}
case ColorScheme.LIGHT: {
window.localStorage[localStorageKey] = 'LIGHT'
break
}
case ColorScheme.SYSTEM: {
delete window.localStorage[localStorageKey]
break
}
default: {
const _exhaustiveCheck: never = value
return _exhaustiveCheck
}
}
return value
}

return state
}

export function createAppStore(initialState?: ApplicationState): redux.Store<ApplicationState> {
const hashParams = getHashParams()

Expand Down Expand Up @@ -111,7 +152,7 @@ export function createAppStore(initialState?: ApplicationState): redux.Store<App
direction: SortDirection.DESCENDING,
}),

colorScheme: setter<ColorScheme>(actions.setColorScheme, ColorScheme.SYSTEM),
colorScheme,
})

return redux.createStore(reducer, initialState)
Expand Down

0 comments on commit b7af76b

Please sign in to comment.