Skip to content

Commit

Permalink
fix DATABASE_URL, default journal, navigate; track saves
Browse files Browse the repository at this point in the history
- remove legacy CACHE_DIR key; update DATABSE_URL reference
- fix issue with default journal setting on import
- fix navigate back from loading screen
- track and log documents saves

Future change should be smarter about saving documents, reducing fs writes and alllowing user to leave note view while save runs in background
  • Loading branch information
cloverich committed Dec 22, 2024
1 parent 0668c97 commit e5d1f35
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/electron/userFilesInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const { ensureDir } = require("./ensureDir");
*/
exports.initUserFilesDir = (userDataDir) => {
initDir("NOTES_DIR", path.join(userDataDir, "/notes"));
initDir("CACHE_DIR", userDataDir);
initDir("SETTINGS_DIR", userDataDir);
};

Expand Down
32 changes: 18 additions & 14 deletions src/markdown/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,35 +621,39 @@ describe("Whacky shit", function () {
describe("front matter parsing", function () {
const content = `---
title: 2024-09-29
tags: weekly-persona
tags: weekly-todo
createdAt: 2024-09-30T17:50:22.000Z
updatedAt: 2024-11-04T16:24:11.000Z
---
\#weekly-persona
#weekly-todo
Last week: [2024-09-22](../persona/0193acd4fa3574698c36c4514b907c70.md)
Last week: [2024-09-22](../work/0193acd4fa3574698c36c4514b907c70.md)
**I am on call this week** [On call week of 2024-09-30](../persona/0193acd4fa45731f81350d4443c1ed16.md)
## Monday
`;

// it.skip("should parse front matter", function () {
// const parsed = parseMarkdown(content);
// console.log(yaml.parse(parsed.children[0].value as string));
// });

it("test how to splice it back in", function () {
// A very basic "it works" test
// todo: End to end test with a real document, asserting against the database values
it("parses front matter as an mdast node, and can be parsed with yaml.parse", function () {
const parsed = parseMarkdown(content);
expect(parsed.children[0].type).to.equal("yaml");
expect(parsed.children[0].value).to.equal(
"title: 2024-09-29\n" +
"tags: weekly-todo\n" +
"createdAt: 2024-09-30T17:50:22.000Z\n" +
"updatedAt: 2024-11-04T16:24:11.000Z",
);

const frontMatter = yaml.parse(parsed.children[0].value as string);
const newFrontMatter = yaml.stringify({
...frontMatter,
expect(frontMatter).to.deep.equal({
title: "2024-09-29",
tags: "weekly-todo",
createdAt: "2024-09-30T17:50:22.000Z",
updatedAt: "2024-11-04T16:24:11.000Z",
});

// ok, it needs --- added
console.log(newFrontMatter);
});
});
10 changes: 7 additions & 3 deletions src/preload/client/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,16 @@ export class DocumentsClient {
if (!args.id) throw new Error("id required to update document");

args.frontMatter.tags = Array.from(new Set(args.frontMatter.tags));
// todo: I think we accept this from the client now and just expect
// callers to update updatedAt, to support importers and sync manually configuring
// this...
args.frontMatter.updatedAt =
args.frontMatter.updatedAt || new Date().toISOString();

const content = this.prependFrontMatter(args.content, args.frontMatter);

const origDoc = await this.findById({ id: args.id! });
await this.files.uploadDocument({ id: args.id!, content }, args.journal);
const origDoc = await this.findById({ id: args.id });
await this.files.uploadDocument({ id: args.id, content }, args.journal);

// sigh; this is a bit of a mess.
if (origDoc.journal !== args.journal) {
Expand All @@ -233,7 +236,7 @@ export class DocumentsClient {
}

await this.updateIndex({
id: args.id!,
id: args.id,
content,
journal: args.journal,
frontMatter: args.frontMatter,
Expand Down Expand Up @@ -338,6 +341,7 @@ export class DocumentsClient {
);
}

await trx("document_links").where({ documentId: id }).del();
await this.addNoteLinks(trx, id!, content);
});
};
Expand Down
4 changes: 4 additions & 0 deletions src/preload/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const knex = Knex({
connection: {
filename: settings.get("DATABASE_URL") as string,
},
// https://knexjs.org/guide/query-builder.html#insert
// don't replace undefined with "DEFAULT" in insert statements; replace
// it with NULL instead (SQLite raises otherwise)
useNullAsDefault: true,
});

export { GetDocumentResponse } from "./types";
Expand Down
4 changes: 2 additions & 2 deletions src/preload/client/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { ipcRenderer } from "electron";
import Store from "electron-store";

export interface Preferences {
CACHE_DIR: string;
DATABASE_URL: string;
DEFAULT_JOURNAL: string | null;
ARCHIVED_JOURNALS: Record<string, boolean>;
NOTES_DIR: string;
SETTINGS_DIR: string;
}

const defaults = (): Preferences => ({
CACHE_DIR: "",
DATABASE_URL: "",
DEFAULT_JOURNAL: null,
ARCHIVED_JOURNALS: {},
NOTES_DIR: "",
Expand Down
11 changes: 8 additions & 3 deletions src/preload/client/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,19 @@ export class SyncClient {
}
}

// Ensure default journal exists; attempt to declare one otherwise
// Ensure default journal exists; attempt to declare one. Otherwise,
// new documents will default to a journal that does not exist, and fail
// to create.
const defaultJournal = await this.preferences.get("DEFAULT_JOURNAL");

if (!defaultJournal || !(defaultJournal in journals)) {
console.log("updating default journal", defaultJournal, journals);

if (journals.length) {
await this.preferences.set("DEFAULT_JOURNAL", journals[0]);
if (Object.keys(journals).length) {
await this.preferences.set("DEFAULT_JOURNAL", Object.keys(journals)[0]);
} else {
await this.journals.create({ name: "default_journal" });
await this.preferences.set("DEFAULT_JOURNAL", "default_journal");
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/views/edit/EditableDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export class EditableDocument {
slateContent: SlateCustom.SlateNode[];
@observable private changeCount = 0;

// todo: save queue. I'm saving too often, but need to do this until I allow exiting note
// while save is in progress; track and report saveCount to discover if this is a major issue
// or not.
saveCount = 0;

// reaction clean-up when component unmounts; see constructor
teardown?: IReactionDisposer;

Expand Down Expand Up @@ -135,13 +140,15 @@ export class EditableDocument {
journal: this.journal,
content: this.content,
id: this.id,
frontMatter: this.frontMatter,
frontMatter: toJS(this.frontMatter),
}),
);
this.saveCount++;
} catch (err) {
this.saving = false;
this.dirty = true;
wasError = true;
console.error("Error saving document", err);
toaster.danger(JSON.stringify(err));
} finally {
this.saving = false;
Expand All @@ -151,8 +158,8 @@ export class EditableDocument {
if (this.dirty && !wasError) this.save();
}
},
5000,
{ trailing: true },
3000,
{ leading: true },
);

del = async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/views/edit/loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const EditLoadingComponent = observer((props: LoadingComponentProps) => {
border="none"
icon={ChevronLeftIcon}
className="drag-none"
onClick={() => {}}
onClick={() => navigate(-1)}
marginRight={8}
>
Back to documents
Expand All @@ -32,7 +32,9 @@ export const EditLoadingComponent = observer((props: LoadingComponentProps) => {
</Titlebar>
<Pane padding={50} paddingTop={98} flexGrow={1} display="flex">
<Pane flexGrow={1} display="flex" flexDirection="column" width="100%">
<Pane flexGrow={1} paddingTop={24}></Pane>
<Pane flexGrow={1} paddingTop={24}>
{props.error && props.error?.message}
</Pane>
</Pane>
</Pane>
</>
Expand Down
9 changes: 8 additions & 1 deletion src/views/edit/useEditableDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ export function useEditableDocument(documentId: string) {

load();
return () => {
if (state.document?.teardown) state.document.teardown();
if (state.document?.teardown) {
console.log(
`save count for ${state.document.id}: ${state.document.saveCount}`,
);
state.document.teardown();
}
if (state.document?.saveCount)
console.log("saved", state.document.saveCount, "times");
};
}, [documentId]);

Expand Down
2 changes: 1 addition & 1 deletion src/views/preferences/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const Preferences = observer(() => {
</p>
<p>
The current Chronicles cache is located at{" "}
{store.preferences.CACHE_DIR}
{store.preferences.DATABASE_URL}
</p>
<Button
isLoading={store.loading}
Expand Down

0 comments on commit e5d1f35

Please sign in to comment.