Skip to content

Commit

Permalink
fix(Desktop): Make first launch experience work across platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch committed Jun 10, 2021
1 parent 1504b37 commit af5cca8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
10 changes: 6 additions & 4 deletions desktop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { requestHandler, scheme } from './main/app-protocol'
import { openLauncherWindow } from './main/launcher/window'
import { openOnboardingWindow } from './main/onboarding/window'
import { initAppConfigStore } from './main/store/bootstrap'
import { isFirstLaunch, setFirstLaunchState } from './main/utils/firstLaunch'

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
Expand All @@ -25,8 +26,9 @@ const createMainWindow = (): void => {
initAppConfigStore()

// If app is launched for the first time, show onboarding flow
if (process.argv[1] === '--squirrel-firstrun') {
if (isFirstLaunch()) {
openOnboardingWindow()
setFirstLaunchState(false)
} else {
openLauncherWindow()
}
Expand All @@ -37,9 +39,9 @@ protocol.registerSchemesAsPrivileged([
scheme: scheme,
privileges: {
standard: true,
secure: true,
},
},
secure: true
}
}
])

if (process.env.NODE_ENV === 'development') {
Expand Down
8 changes: 6 additions & 2 deletions desktop/src/main/store/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface AppConfigStore {
[key: string]: JSONValue
}

const storeName = 'unprotected.json'
const storeName = 'storeUnprotected.json'
const userDataPath = app.getPath('userData')
export const unprotectedStorePath = path.join(userDataPath, storeName)

Expand All @@ -30,7 +30,10 @@ export const readUnprotectedStore = (): AppConfigStore => {
return {}
}

const defaultConfigStore = {}
const defaultConfigStore: AppConfigStore = {
REPORT_ERRORS: false
}

export let unprotectedStore: ObservableMap<AppConfigStore>

export const writeUnprotectedStore = (store: AppConfigStore): void => {
Expand All @@ -40,6 +43,7 @@ export const writeUnprotectedStore = (store: AppConfigStore): void => {
export const resetUnprotectedStore = (): void => {
fs.writeFileSync(unprotectedStorePath, JSON.stringify(defaultConfigStore))
}

export const initAppConfigStore = () => {
let config = defaultConfigStore
if (fs.existsSync(unprotectedStorePath)) {
Expand Down
10 changes: 6 additions & 4 deletions desktop/src/main/store/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { unprotectedStore, AppConfigStore, JSONValue } from './bootstrap'

export enum UnprotectedStoreKeys {
REPORT_ERRORS = 'REPORT_ERRORS',
FIRST_LAUNCH = 'FIRST_LAUNCH'
}

export const readAppConfig = () => {
Expand All @@ -12,10 +13,11 @@ export const getAppConfig = (key: UnprotectedStoreKeys) => {
return unprotectedStore.get(key)
}

export const setAppConfig =
(key: UnprotectedStoreKeys) => (value: JSONValue) => {
unprotectedStore.set(key, value)
}
export const setAppConfig = (key: UnprotectedStoreKeys) => (
value: JSONValue
) => {
unprotectedStore.set(key, value)
}

export const updateAppConfig = (newStore: AppConfigStore) => {
unprotectedStore.state = newStore
Expand Down
23 changes: 23 additions & 0 deletions desktop/src/main/utils/firstLaunch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { app } from 'electron'
import path from 'path'
import {
getAppConfig,
setAppConfig,
UnprotectedStoreKeys
} from '../store/handlers'

const storeName = 'unprotected.json'
const userDataPath = app.getPath('userData')
export const unprotectedStorePath = path.join(userDataPath, storeName)

/**
* Checks whether the app is being launched for the first time
*/
export const isFirstLaunch = (): boolean => {
const config = getAppConfig(UnprotectedStoreKeys.FIRST_LAUNCH)
return typeof config === 'boolean' ? config : true
}

export const setFirstLaunchState = (value: boolean) => {
setAppConfig(UnprotectedStoreKeys.FIRST_LAUNCH)(value)
}

0 comments on commit af5cca8

Please sign in to comment.