|
| 1 | +import type { NotesDashboardResponse } from '../dto/notes-dashboard' |
| 2 | +import Elysia from 'elysia' |
| 3 | +import { useNotesStorage, useStorage } from '../../storage' |
| 4 | +import { buildNotesGraph } from '../../storage/providers/markdown/notes/runtime/graph' |
| 5 | +import { notesDashboardDTO } from '../dto/notes-dashboard' |
| 6 | + |
| 7 | +const RECENT_NOTES_LIMIT = 6 |
| 8 | +const TOP_LINKED_NOTES_LIMIT = 6 |
| 9 | + |
| 10 | +function countWords(content: string): number { |
| 11 | + if (!content.trim()) { |
| 12 | + return 0 |
| 13 | + } |
| 14 | + |
| 15 | + return content.trim().split(/\s+/).filter(Boolean).length |
| 16 | +} |
| 17 | + |
| 18 | +function getDayKey(timestamp: number): string { |
| 19 | + const date = new Date(timestamp) |
| 20 | + |
| 21 | + return [ |
| 22 | + date.getFullYear(), |
| 23 | + String(date.getMonth() + 1).padStart(2, '0'), |
| 24 | + String(date.getDate()).padStart(2, '0'), |
| 25 | + ].join('-') |
| 26 | +} |
| 27 | + |
| 28 | +function buildActivity(updatedAtList: number[]) { |
| 29 | + const days: Record<string, number> = {} |
| 30 | + |
| 31 | + updatedAtList.forEach((updatedAt) => { |
| 32 | + const dayKey = getDayKey(updatedAt) |
| 33 | + days[dayKey] = (days[dayKey] ?? 0) + 1 |
| 34 | + }) |
| 35 | + |
| 36 | + const today = new Date() |
| 37 | + today.setHours(0, 0, 0, 0) |
| 38 | + const todayTime = today.getTime() |
| 39 | + const todayStart = todayTime |
| 40 | + const sevenDaysAgo = todayTime - 6 * 24 * 60 * 60 * 1000 |
| 41 | + |
| 42 | + return { |
| 43 | + days, |
| 44 | + notesUpdatedLast7Days: updatedAtList.filter( |
| 45 | + updatedAt => updatedAt >= sevenDaysAgo, |
| 46 | + ).length, |
| 47 | + notesUpdatedToday: updatedAtList.filter( |
| 48 | + updatedAt => updatedAt >= todayStart, |
| 49 | + ).length, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const app = new Elysia({ prefix: '/notes' }) |
| 54 | + |
| 55 | +app.use(notesDashboardDTO).get( |
| 56 | + '/dashboard', |
| 57 | + () => { |
| 58 | + const notesStorage = useNotesStorage() |
| 59 | + const storage = useStorage() |
| 60 | + const notes = notesStorage.notes.getNotes({ isDeleted: 0 }) |
| 61 | + const folders = notesStorage.folders.getFolders() |
| 62 | + const tags = notesStorage.tags.getTags() |
| 63 | + const snippets = storage.snippets.getSnippets({ isDeleted: 0 }) |
| 64 | + |
| 65 | + const graph = buildNotesGraph({ |
| 66 | + notes: notes.map(note => ({ |
| 67 | + content: note.content, |
| 68 | + createdAt: note.createdAt, |
| 69 | + description: note.description, |
| 70 | + filePath: '', |
| 71 | + folderId: note.folder?.id ?? null, |
| 72 | + id: note.id, |
| 73 | + isDeleted: note.isDeleted, |
| 74 | + isFavorites: note.isFavorites, |
| 75 | + name: note.name, |
| 76 | + tags: note.tags.map(tag => tag.id), |
| 77 | + updatedAt: note.updatedAt, |
| 78 | + })), |
| 79 | + snippets: snippets.map(snippet => ({ |
| 80 | + id: snippet.id, |
| 81 | + name: snippet.name, |
| 82 | + })), |
| 83 | + }) |
| 84 | + |
| 85 | + return { |
| 86 | + activity: buildActivity(notes.map(note => note.updatedAt)), |
| 87 | + graphPreview: { |
| 88 | + edges: graph.edges, |
| 89 | + nodes: graph.nodes.map(node => ({ |
| 90 | + id: node.id, |
| 91 | + name: node.name, |
| 92 | + folderId: node.folderId, |
| 93 | + incomingLinksCount: node.incomingLinksCount, |
| 94 | + })), |
| 95 | + }, |
| 96 | + recent: [...notes] |
| 97 | + .sort((left, right) => right.updatedAt - left.updatedAt) |
| 98 | + .slice(0, RECENT_NOTES_LIMIT) |
| 99 | + .map(note => ({ |
| 100 | + id: note.id, |
| 101 | + name: note.name, |
| 102 | + folder: note.folder, |
| 103 | + updatedAt: note.updatedAt, |
| 104 | + })), |
| 105 | + stats: { |
| 106 | + foldersCount: folders.length, |
| 107 | + notesCount: notes.length, |
| 108 | + tagsCount: tags.length, |
| 109 | + wordsCount: notes.reduce( |
| 110 | + (total, note) => total + countWords(note.content), |
| 111 | + 0, |
| 112 | + ), |
| 113 | + }, |
| 114 | + topLinked: [...graph.nodes] |
| 115 | + .filter(node => node.incomingLinksCount > 0) |
| 116 | + .sort( |
| 117 | + (left, right) => right.incomingLinksCount - left.incomingLinksCount, |
| 118 | + ) |
| 119 | + .slice(0, TOP_LINKED_NOTES_LIMIT) |
| 120 | + .map(node => ({ |
| 121 | + id: node.id, |
| 122 | + incomingLinksCount: node.incomingLinksCount, |
| 123 | + name: node.name, |
| 124 | + })), |
| 125 | + } as NotesDashboardResponse |
| 126 | + }, |
| 127 | + { |
| 128 | + response: 'notesDashboardResponse', |
| 129 | + detail: { |
| 130 | + tags: ['Notes Dashboard'], |
| 131 | + }, |
| 132 | + }, |
| 133 | +) |
| 134 | + |
| 135 | +export default app |
0 commit comments