Skip to content

Commit

Permalink
Merge pull request #32 from ma-ray/handle-null-settings
Browse files Browse the repository at this point in the history
update functions to handle when settings is null
  • Loading branch information
ma-ray authored Jan 8, 2024
2 parents 5befe52 + 5d2944d commit 3eec577
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
40 changes: 26 additions & 14 deletions electron/editor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IpcMainEvent, IpcMainInvokeEvent, dialog, shell } from 'electron'
import { store } from './main'
import { Settings } from '../shared/settings'
import { getSettings } from './settings'
import store, { getSettings } from './settings'
import path from 'node:path'
import { access, mkdir, readFile, writeFile, readdir } from 'fs/promises'
import moment from 'moment'
Expand All @@ -14,6 +13,7 @@ export const writeDiary = (
data: string
) => {
const settings = getSettings()
if (!settings) return Promise.resolve()
const trueMonth = month + 1
const diaryDayPath = path.join(
settings.diaryLocation,
Expand All @@ -30,6 +30,7 @@ export const readDiary = async (
day: number
) => {
const settings = getSettings()
if (!settings) return ''
const trueMonth = month + 1
const diaryDayPath = path.join(
settings.diaryLocation,
Expand Down Expand Up @@ -60,6 +61,7 @@ export const doesDiaryDayExist = async (
day: number
) => {
const settings = getSettings()
if (!settings) return false
const trueMonth = month + 1
const diaryDayPath = path.join(
settings.diaryLocation,
Expand Down Expand Up @@ -95,8 +97,10 @@ const doesFileExist = async (path: string) => {
}

export const availableEntries = async (_: IpcMainInvokeEvent, year: number) => {
const diaryPath = getSettings().diaryLocation
const settings = getSettings()
const availableEntries = new Set<string>()
if (!settings) return availableEntries
const diaryPath = settings.diaryLocation

if (!(await doesFileExist(path.join(diaryPath, `${year}`)))) {
return availableEntries
Expand Down Expand Up @@ -137,8 +141,11 @@ export const availableEntries = async (_: IpcMainInvokeEvent, year: number) => {
}

export const showDiaryInExplorer = () => {
const diaryPath = getSettings().diaryLocation
shell.openPath(diaryPath)
const settings = getSettings()
if (settings) {
const diaryPath = settings.diaryLocation
shell.openPath(diaryPath)
}
}

export const showDiaryPageInExplorer = async (
Expand All @@ -147,18 +154,23 @@ export const showDiaryPageInExplorer = async (
month: number,
day: number
) => {
const diaryPath = getSettings().diaryLocation
const trueMonth = month + 1
const diaryDayPath = path.join(
diaryPath,
`${year}/${trueMonth}/${day}/${trueMonth}-${day}-${year}.md`
)
if (await doesFileExist(diaryDayPath)) {
shell.showItemInFolder(diaryDayPath)
const settings = getSettings()
if (settings) {
const diaryPath = settings.diaryLocation
const trueMonth = month + 1
const diaryDayPath = path.join(
diaryPath,
`${year}/${trueMonth}/${day}/${trueMonth}-${day}-${year}.md`
)
if (await doesFileExist(diaryDayPath)) {
shell.showItemInFolder(diaryDayPath)
}
}
}

export const doesDiaryPathExist = () => {
const diaryPath = getSettings().diaryLocation
const settings = getSettings()
if (!settings) return false
const diaryPath = settings.diaryLocation
return doesFileExist(diaryPath)
}
3 changes: 0 additions & 3 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
showDiaryPageInExplorer,
writeDiary,
} from './editor'
import Store from 'electron-store'
import { getSettings } from './settings'

// The built directory structure
Expand All @@ -31,8 +30,6 @@ let win: BrowserWindow | null
// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - Vite@2.x
const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']

export const store = new Store()

function createWindow() {
win = new BrowserWindow({
webPreferences: {
Expand Down
12 changes: 10 additions & 2 deletions electron/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { store } from './main'
import { Settings } from '../shared/settings'
import Store from 'electron-store'

export const getSettings = () => {
return store.get('settings') as Settings
return store.get('settings') as Settings | undefined
}

type SchemaType = {
settings: Settings
}

const store = new Store<SchemaType>()

export default store

0 comments on commit 3eec577

Please sign in to comment.