-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.svelte.ts
99 lines (85 loc) · 2.66 KB
/
db.svelte.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { Folder, Bookmark } from './types'
import { EventManager } from '$lib/utils/EventManager.svelte'
import { RuneQuery } from './RuneQuery.svelte'
import dexie from '$lib/data/dexie.svelte'
import { liveQuery } from 'dexie'
class BookmarksDB {
/**
* A tag by which to filter the active folder's bookmarks, or `null` to show all bookmarks.
* @state
*/
public tagFilter = $state<string>('')
#activeFolderId = new RuneQuery(
liveQuery(async () => (await dexie.settings.get('activeFolderId'))?.value as string),
)
/**
* The active folder id.
* @derived
*/
public activeFolderId = $derived(this.#activeFolderId.current)
#folders = new RuneQuery(
liveQuery(async () => (await dexie.table('folders').toArray()) as Folder[]),
)
/**
* All folders in the database.
* @derived
*/
public folders = $derived(this.#folders.current)
/**
* The active folder.
* @derived
*/
public activeFolder = $derived.by(() => {
this.folders
return this.folders?.find((f) => f.folder_id === this.activeFolderId) ?? null
})
#bookmarks = new RuneQuery(
liveQuery(async () => (await dexie.bookmarks.toArray()) as Bookmark[]),
)
public bookmarks = $derived(this.#bookmarks.current)
public activeBookmarks = $derived.by(() => {
const ids = this.activeFolder?.bookmark_ids ?? []
const active = this.bookmarks?.filter((b) => ids.includes(b.bookmark_id))
const filtered = this.tagFilter ? this.#filterByTag(active ?? []) : active
return filtered?.sort((a, b) => a.position - b.position) ?? []
})
#uniqueTags = new RuneQuery(
liveQuery(async () => (await dexie.bookmarks.orderBy('tags').uniqueKeys()) as string[]),
)
/**
* All unique tags in the database.
* @derived
*/
public uniqueTags = $derived(this.#uniqueTags.current)
#evm = new EventManager()
constructor() {
this.#evm.add(
$effect.root(() => {
$inspect('BookmarksDB', $state.snapshot(this))
$inspect('🔴 BookmarksDB.activeFolderId', this.activeFolderId)
$inspect('🟠 BookmarksDB.folders', this.folders)
$inspect('🟡 BookmarksDB.activeFolder', this.activeFolder)
$inspect('🟢 BookmarksDB.activeBookmarks', this.activeBookmarks)
}),
)
}
/**
* Some folders have similar bookmarks with unique id's, so we filter out the duplicates here.
*/
#filterByTag(bookmarks: Bookmark[]) {
const uniqueTitles = new Set()
return bookmarks.reduce((acc, curr) => {
if (!curr.tags.some((t) => t === this.tagFilter)) return acc
if (!uniqueTitles.has(curr.title)) {
uniqueTitles.add(curr.title)
return [...acc, curr]
}
return acc
}, [] as Bookmark[])
}
/**
* Cleanup function.
*/
dispose = () => this.#evm.dispose()
}
export const db = new BookmarksDB()