From 9a275d070f671886c2f6ad076f2adbf0faa8406a Mon Sep 17 00:00:00 2001 From: Raymond Ma <60418739+ma-ray@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:44:26 -0500 Subject: [PATCH 1/3] add availableEntries editor function --- electron/editor.ts | 61 ++++++++++++++++++++++++++++++++++++++++------ electron/main.ts | 2 ++ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/electron/editor.ts b/electron/editor.ts index 6b6fdbd..811c520 100644 --- a/electron/editor.ts +++ b/electron/editor.ts @@ -3,7 +3,8 @@ import { store } from './main' import { Settings } from '../shared/settings' import { getSettings } from './settings' import path from 'node:path' -import { access, mkdir, readFile, writeFile } from 'fs/promises' +import { access, mkdir, readFile, writeFile, readdir } from 'fs/promises' +import moment from 'moment' export const writeDiary = ( _: IpcMainEvent, @@ -64,12 +65,7 @@ export const doesDiaryDayExist = async ( settings.diaryLocation, `${year}/${trueMonth}/${day}/${trueMonth}-${day}-${year}.md` ) - try { - await access(diaryDayPath) - return true - } catch (error) { - return false - } + return await doesFileExist(diaryDayPath) } export const openDirectory = async () => { @@ -88,3 +84,54 @@ export const openDirectory = async () => { } return false } + +const doesFileExist = async (path: string) => { + try { + await access(path) + return true + } catch (error) { + return false + } +} + +export const availableEntries = async (_: IpcMainInvokeEvent, year: number) => { + const diaryPath = getSettings().diaryLocation + const availableEntries = new Set() + + if (!(await doesFileExist(path.join(diaryPath, `${year}`)))) { + return availableEntries + } + + const months = (await readdir(path.join(diaryPath, `${year}`))).filter( + (month) => { + return parseInt(month) < 13 && parseInt(month) > 0 + } + ) + + await Promise.all( + months.map(async (month) => { + const days = ( + await readdir(path.join(diaryPath, `${year}/${month}`)) + ).filter((d) => { + const daysInMonth = moment([year, parseInt(month) - 1]).daysInMonth() + const day = parseInt(d) + return day > 0 && day <= daysInMonth + }) + + await Promise.all( + days.map(async (day) => { + const diaryDayPath = path.join( + diaryPath, + `${year}/${month}/${day}/${month}-${day}-${year}.md` + ) + + if (await doesFileExist(diaryDayPath)) { + availableEntries.add(`${month}-${day}-${year}`) + } + }) + ) + }) + ) + + return availableEntries +} diff --git a/electron/main.ts b/electron/main.ts index 4113b7f..39af3cf 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -1,6 +1,7 @@ import { app, BrowserWindow, ipcMain, shell } from 'electron' import path from 'node:path' import { + availableEntries, doesDiaryDayExist, openDirectory, readDiary, @@ -79,6 +80,7 @@ app.whenReady().then(() => { ipcMain.handle('open-directory', openDirectory) ipcMain.handle('get-settings', getSettings) ipcMain.handle('does-diary-exist', doesDiaryDayExist) + ipcMain.handle('available-entries', availableEntries) createWindow() }) From 98baa644490441174448a8685dda5b2cfa7a9c69 Mon Sep 17 00:00:00 2001 From: Raymond Ma <60418739+ma-ray@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:47:03 -0500 Subject: [PATCH 2/3] show created entries in the calendar --- src/components/Calendar.tsx | 42 +++++++++++++++++++++++++++---------- src/pages/HomePage.tsx | 15 ++++++++++++- tailwind.config.js | 1 + 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/components/Calendar.tsx b/src/components/Calendar.tsx index c379002..2bb9cd2 100644 --- a/src/components/Calendar.tsx +++ b/src/components/Calendar.tsx @@ -5,9 +5,10 @@ import { months } from '../util/date' type DayProps = { date: CalendarDate + entryAvailable?: boolean } -const Day: React.FC = ({ date }) => { +const Day: React.FC = ({ date, entryAvailable }) => { const dayPassed = moment([date.year, date.month, date.day]).isBefore( moment().startOf('day') ) @@ -16,14 +17,15 @@ const Day: React.FC = ({ date }) => { moment().startOf('day') ) - const bg = - date.siblingMonth && dayPassed - ? 'bg-daypassed' - : dayPassed - ? 'bg-daypassed' - : isToday - ? 'bg-today' - : '' + const bg = entryAvailable + ? 'bg-available' + : date.siblingMonth && dayPassed + ? 'bg-daypassed' + : dayPassed + ? 'bg-daypassed' + : isToday + ? 'bg-today' + : '' return (
= ({ date }) => { type CalendarProps = { month: number year: number + availableEntries: Set } -const Calendar: React.FC = ({ month, year }) => { +const Calendar: React.FC = ({ + month, + year, + availableEntries, +}) => { const calendar = new CalendarBase({ siblingMonths: true, }) @@ -68,7 +75,20 @@ const Calendar: React.FC = ({ month, year }) => {
{days.map( (date) => - date && + date && ( + + ) )}
diff --git a/src/pages/HomePage.tsx b/src/pages/HomePage.tsx index 0f00e3d..90235e3 100644 --- a/src/pages/HomePage.tsx +++ b/src/pages/HomePage.tsx @@ -13,6 +13,9 @@ const HomePage = () => { : moment().year().toString() const [year, setYear] = useState(parseInt(savedYear)) + const [availableEntries, setAvailableEntries] = useState>( + new Set() + ) const { loadSettings } = useContext(SettingsContext) const calendarListRef = useRef(null) const { scrollYProgress } = useScroll({ @@ -41,6 +44,12 @@ const HomePage = () => { } }, []) + useEffect(() => { + window.ipcRenderer.invoke('available-entries', year).then((res) => { + setAvailableEntries(res) + }) + }, [year]) + return (
@@ -101,7 +110,11 @@ const HomePage = () => { key={i} className="snap-center relative flex justify-center items-center h-full" > - +
))}
diff --git a/tailwind.config.js b/tailwind.config.js index 8dc0ef0..f5070fe 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -52,6 +52,7 @@ module.exports = { }, daypassed: '#D8D1D1', today: '#A9E0F1', + available: '#8AF4BB', }, borderRadius: { lg: 'var(--radius)', From 7d13f74488dbfc7188b87bcdf29160c9398cb207 Mon Sep 17 00:00:00 2001 From: Raymond Ma <60418739+ma-ray@users.noreply.github.com> Date: Fri, 5 Jan 2024 22:20:25 -0500 Subject: [PATCH 3/3] get available entries when diary is changed --- src/pages/HomePage.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pages/HomePage.tsx b/src/pages/HomePage.tsx index 90235e3..d94211f 100644 --- a/src/pages/HomePage.tsx +++ b/src/pages/HomePage.tsx @@ -89,7 +89,13 @@ const HomePage = () => { e.preventDefault() window.ipcRenderer.invoke('open-directory').then((res) => { if (res) { - loadSettings() + loadSettings().then(() => + window.ipcRenderer + .invoke('available-entries', year) + .then((res) => { + setAvailableEntries(res) + }) + ) } }) }}