From d65d1307e2469853f600950494ef9772b3496cb8 Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Sun, 7 Sep 2025 00:02:24 -0700 Subject: [PATCH] Extract some common Yjs code into Vault class --- src/components/App.tsx | 5 +---- src/components/Chooser.tsx | 3 +-- src/sync.ts | 5 +++-- src/vault.ts | 31 +++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/vault.ts diff --git a/src/components/App.tsx b/src/components/App.tsx index cda16a9..9f8acc4 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -38,10 +38,7 @@ function Layout(props: any) { if (id === null) { return null; } else { - return (defaultVault.doc.getMap() as Y.Map) - .get('docs') - .get(id) - .get('title') as string; + return defaultVault.getDocInfo(id)?.title ?? ''; } }; diff --git a/src/components/Chooser.tsx b/src/components/Chooser.tsx index 31865d9..f680db1 100644 --- a/src/components/Chooser.tsx +++ b/src/components/Chooser.tsx @@ -3,14 +3,13 @@ import './Chooser.css'; import { useNavbar } from './App'; import { useNavigate } from '@solidjs/router'; import { defaultVault } from '../sync'; -import * as Y from 'yjs'; function Chooser() { const navigate = useNavigate(); const { setFileId } = useNavbar(); onMount(() => setFileId(null)); - const docsMap = defaultVault.doc.getMap().get('docs') as Y.Map; + const docsMap = defaultVault.getDocsMap(); const titles: Record = {}; for (const [id, docInfo] of docsMap.entries()) { titles[id] = docInfo.get('title'); diff --git a/src/sync.ts b/src/sync.ts index 426e71e..ed7dcbc 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -2,6 +2,7 @@ import _ from 'lodash'; import * as Y from 'yjs'; import { openDB } from 'idb'; import { generateId } from './util'; +import { Vault } from './vault'; const DB_NAME = 'synced-docs'; const LOCAL_UPDATE_STORE = 'local-updates'; @@ -95,12 +96,12 @@ async function getOrCreateVaultDoc() { if (!vaultMap.has('docs')) { vaultMap.set('docs', new Y.Map()); } - return vault; + return new Vault(vault); } export const defaultVault = await getOrCreateVaultDoc(); -const docsMap = defaultVault.doc.getMap().get('docs') as Y.Map; +const docsMap = defaultVault.getDocsMap(); if (docsMap.size === 0) { console.log('Running vault migration...'); for (const oldKey of ['foo', 'bar', 'baz']) { diff --git a/src/vault.ts b/src/vault.ts new file mode 100644 index 0000000..47d0e74 --- /dev/null +++ b/src/vault.ts @@ -0,0 +1,31 @@ +import type { LocalDocument } from './sync'; +import * as Y from 'yjs'; + +export class Vault { + doc: LocalDocument; + + constructor(doc: LocalDocument) { + this.doc = doc; + } + + getDocInfo(id: string): DocInfo | null { + const docsEntry = this.getDocsMap().get(id); + if (!docsEntry) { + return null; + } + return { + title: docsEntry.get('title'), + tags: new Set(), + }; + } + + getDocsMap(): Y.Map> { + const vaultMap = this.doc.doc.getMap(); + return vaultMap.get('docs') as Y.Map; + } +} + +export interface DocInfo { + title: string; + tags: Set; +}