Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update functions to handle when settings is null #32

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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