Skip to content

Commit 7579232

Browse files
committed
Cahce markdown files in local storage
1 parent 5f47f9e commit 7579232

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/global-state.ts

+32-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { atom } from "jotai"
55
import { atomWithMachine } from "jotai-xstate"
66
import { atomWithStorage, selectAtom } from "jotai/utils"
77
import { assign, createMachine } from "xstate"
8+
import { z } from "zod"
89
import {
910
GitHubRepository,
1011
GitHubUser,
@@ -26,6 +27,7 @@ import { removeTemplateFrontmatter } from "./utils/remove-template-frontmatter"
2627
const ROOT_DIR = "/root"
2728
const DEFAULT_BRANCH = "main"
2829
const GITHUB_USER_KEY = "github_user"
30+
const MARKDOWN_FILES_KEY = "markdown_files"
2931

3032
// -----------------------------------------------------------------------------
3133
// State machine
@@ -118,7 +120,7 @@ function createGlobalStateMachine() {
118120
src: "resolveRepo",
119121
onDone: {
120122
target: "cloned",
121-
actions: ["setGitHubRepo", "setMarkdownFiles"],
123+
actions: ["setGitHubRepo", "setMarkdownFiles", "setMarkdownFilesLocalStorage"],
122124
},
123125
onError: {
124126
target: "empty",
@@ -137,7 +139,7 @@ function createGlobalStateMachine() {
137139
src: "cloneRepo",
138140
onDone: {
139141
target: "cloned",
140-
actions: "setMarkdownFiles",
142+
actions: ["setMarkdownFiles", "setMarkdownFilesLocalStorage"],
141143
},
142144
onError: {
143145
target: "empty",
@@ -158,7 +160,7 @@ function createGlobalStateMachine() {
158160
on: {
159161
WRITE_FILE: {
160162
target: "writingFile",
161-
actions: "setMarkdownFile",
163+
actions: ["setMarkdownFile", "setMarkdownFileLocalStorage"],
162164
},
163165
},
164166
},
@@ -174,7 +176,7 @@ function createGlobalStateMachine() {
174176
on: {
175177
WRITE_FILE: {
176178
target: "writingFile",
177-
actions: "setMarkdownFile",
179+
actions: ["setMarkdownFile", "setMarkdownFileLocalStorage"],
178180
},
179181
},
180182
},
@@ -193,7 +195,7 @@ function createGlobalStateMachine() {
193195
src: "sync",
194196
onDone: {
195197
target: "idle",
196-
actions: "setMarkdownFiles",
198+
actions: ["setMarkdownFiles", "setMarkdownFilesLocalStorage"],
197199
},
198200
onError: {
199201
target: "idle",
@@ -240,6 +242,8 @@ function createGlobalStateMachine() {
240242
return { githubUser: githubUserSchema.parse(githubUser) }
241243
},
242244
resolveRepo: async () => {
245+
console.time("resolveRepo()")
246+
243247
// Check git config for repo name
244248
const remoteOriginUrl = await git.getConfig({
245249
fs,
@@ -257,7 +261,10 @@ function createGlobalStateMachine() {
257261
}
258262

259263
const githubRepo = { owner, name }
260-
const markdownFiles = await getMarkdownFilesFromFs(ROOT_DIR)
264+
const markdownFiles =
265+
getMarkdownFilesFromLocalStorage() ?? (await getMarkdownFilesFromFs(ROOT_DIR))
266+
267+
console.timeEnd("resolveRepo()")
261268

262269
return { githubRepo, markdownFiles }
263270
},
@@ -407,6 +414,18 @@ function createGlobalStateMachine() {
407414
return { ...context.markdownFiles, [filepath]: content }
408415
},
409416
}),
417+
setMarkdownFilesLocalStorage: (_, event) => {
418+
// Cache markdown files in localStorage
419+
localStorage.setItem(MARKDOWN_FILES_KEY, JSON.stringify(event.data.markdownFiles))
420+
},
421+
setMarkdownFileLocalStorage: (context, event) => {
422+
// Update markdown file in localStorage
423+
const { filepath, content } = event
424+
localStorage.setItem(
425+
MARKDOWN_FILES_KEY,
426+
JSON.stringify({ ...context.markdownFiles, [filepath]: content }),
427+
)
428+
},
410429
setError: assign({
411430
// TODO: Remove `as Error`
412431
error: (_, event) => event.data as Error,
@@ -416,6 +435,13 @@ function createGlobalStateMachine() {
416435
)
417436
}
418437

438+
function getMarkdownFilesFromLocalStorage() {
439+
const markdownFiles = JSON.parse(localStorage.getItem(MARKDOWN_FILES_KEY) ?? "null")
440+
if (!markdownFiles) return null
441+
const parsedMarkdownFiles = z.record(z.string()).safeParse(markdownFiles)
442+
return parsedMarkdownFiles.success ? parsedMarkdownFiles.data : null
443+
}
444+
419445
/** Walk the file system and return the contents of all markdown files */
420446
async function getMarkdownFilesFromFs(dir: string) {
421447
console.time("getMarkdownFilesFromFs()")

src/global-state.typegen.ts

+2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
"setGitHubRepo": "SELECT_REPO" | "done.invoke.global.signedIn.resolvingRepo:invocation[0]";
3535
"setGitHubUser": "SIGN_IN" | "done.invoke.global.resolvingUser:invocation[0]";
3636
"setMarkdownFile": "WRITE_FILE";
37+
"setMarkdownFileLocalStorage": "WRITE_FILE";
3738
"setMarkdownFiles": "done.invoke.global.signedIn.cloned.sync.syncing:invocation[0]" | "done.invoke.global.signedIn.cloningRepo:invocation[0]" | "done.invoke.global.signedIn.resolvingRepo:invocation[0]";
39+
"setMarkdownFilesLocalStorage": "done.invoke.global.signedIn.cloned.sync.syncing:invocation[0]" | "done.invoke.global.signedIn.cloningRepo:invocation[0]" | "done.invoke.global.signedIn.resolvingRepo:invocation[0]";
3840
};
3941
eventsCausingDelays: {
4042

0 commit comments

Comments
 (0)