-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotesPanel.tsx
More file actions
296 lines (281 loc) · 11.3 KB
/
Copy pathNotesPanel.tsx
File metadata and controls
296 lines (281 loc) · 11.3 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { useQuery, invalidateQuery } from "@blitzjs/rpc"
import { useMutation } from "@blitzjs/rpc"
import { useState, useMemo } from "react"
import listNotes from "../queries/listNotes"
import deleteNote from "../mutations/deleteNote"
import updateNote from "src/notes/mutations/updateNote"
import NoteEditor from "./NotesEditor"
import SearchButton from "src/core/components/SearchButton"
type SortOption = "updatedAt" | "createdAt" | "title"
function downloadMarkdown(filename: string, content: string) {
const blob = new Blob([content], { type: "text/markdown" })
const url = URL.createObjectURL(blob)
const a = document.createElement("a")
a.href = url
a.download = filename
a.click()
URL.revokeObjectURL(url)
}
export const NotesPanel = ({ projectId }: { projectId: number }) => {
const [includeArchived, setIncludeArchived] = useState(false)
const [sortBy, setSortBy] = useState<SortOption>("updatedAt")
const [searchQuery, setSearchQuery] = useState("")
const [notes, { refetch, setQueryData }] = useQuery(
listNotes,
{ projectId, includeArchived },
{ suspense: false }
)
const [deleteNoteMutation] = useMutation(deleteNote)
const [updateNoteMutation] = useMutation(updateNote)
const [creating, setCreating] = useState(false)
const [editingId, setEditingId] = useState<number | null>(null)
const filteredAndSortedNotes = useMemo(() => {
if (!notes) return []
let result = [...notes]
if (searchQuery.trim()) {
const q = searchQuery.toLowerCase()
result = result.filter((n) => (n.title ?? "Untitled").toLowerCase().includes(q))
}
result.sort((a, b) => {
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1
if (sortBy === "title") {
if (!a.title && b.title) return 1
if (a.title && !b.title) return -1
return (a.title ?? "").toLowerCase().localeCompare((b.title ?? "").toLowerCase())
}
if (sortBy === "createdAt") {
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
}
return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
})
return result
}, [notes, searchQuery, sortBy])
const canEditRow = (n: any) => {
if (!n) return false
return !!n.editable || (!!n.canSetContributors && n.visibility === "CONTRIBUTORS")
}
const formatNoteMd = (n: any) => {
const title = n.title || "Untitled"
const created = new Date(n.createdAt).toLocaleString()
const edited = new Date(n.updatedAt).toLocaleString()
const meta = `_Created: ${created} · Last edited: ${edited}_`
const body = n.contentMarkdown ?? "(No content)"
return `# ${title}\n\n${meta}\n\n${body}`
}
const handleDownloadNote = (n: any) => {
const title = n.title || "Untitled"
const safeTitle = title.replace(/[^a-zA-Z0-9_\- ]/g, "").trim() || "note"
downloadMarkdown(`${safeTitle}.md`, formatNoteMd(n))
}
const handleDownloadAll = () => {
if (!filteredAndSortedNotes.length) return
const combined = filteredAndSortedNotes.map(formatNoteMd).join("\n\n---\n\n")
downloadMarkdown("notes.md", combined)
}
return (
<div className="space-y-4">
{/* Index toolbar — hidden while editing/creating */}
{!(creating || editingId !== null) && (
<div className="flex items-center gap-2">
<div className="flex-1">
<SearchButton onChange={(val) => setSearchQuery(String(val))} className="max-w-none" />
</div>
<select
className="select rounded-full border-2 border-primary bg-base-300"
value={sortBy}
onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
setSortBy(e.target.value as SortOption)
}
>
<option value="updatedAt">Last edited</option>
<option value="createdAt">Date created</option>
<option value="title">Title A–Z</option>
</select>
<label className="label cursor-pointer gap-2">
<span className="text-sm">Show archived</span>
<input
type="checkbox"
className="toggle toggle-sm"
checked={includeArchived}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setIncludeArchived(e.target.checked)
}
/>
</label>
{filteredAndSortedNotes.length > 0 && (
<button className="btn btn-accent" onClick={handleDownloadAll}>
Download all
</button>
)}
<button className="btn btn-primary" onClick={() => setCreating(true)}>
New note
</button>
</div>
)}
{/* Create mode: show editor with no noteId */}
{creating && (
<NoteEditor
projectId={projectId}
className="shadow"
onCreated={async (id) => {
setCreating(false)
setEditingId(id)
await refetch()
}}
onSaved={async () => {
setCreating(false)
await refetch()
}}
/>
)}
{/* Edit mode: show editor for a single selected note */}
{editingId !== null &&
(() => {
const n = notes?.find((x) => x.id === editingId)
if (!n) return null
return (
<NoteEditor
projectId={projectId}
noteId={n.id}
initialTitle={n.title ?? ""}
initialMarkdown={n.contentMarkdown ?? ""}
initialJSON={n.contentJSON}
initialVisibility={n.visibility}
className="shadow"
readOnly={!canEditRow(n)}
canSetContributors={!!n.canSetContributors}
onClose={() => setEditingId(null)}
onSaved={async () => {
setEditingId(null)
await refetch()
}}
/>
)
})()}
{/* Index list — only when not editing/creating */}
{!(creating || editingId !== null) && (
<ul className="space-y-3">
{filteredAndSortedNotes.map((n) => (
<li key={n.id} className="card bg-base-300 shadow border border-base-300">
<div className="card-body p-4">
<div className="flex items-center justify-between gap-2">
<div className="flex flex-col items-start">
<button
className="font-medium text-lg truncate text-left hover:underline"
onClick={() => setEditingId(n.id)}
title={n.title ?? "Untitled"}
>
{n.title ?? "Untitled"}
</button>
<div className="mt-1">
<span
className={
"badge " +
(n.visibility === "PRIVATE"
? "badge-neutral"
: n.visibility === "PM_ONLY"
? "badge-warning"
: "badge-info")
}
>
{n.visibility === "PRIVATE"
? "Private"
: n.visibility === "PM_ONLY"
? "PM"
: "Contributors"}
</span>
</div>
<span className="opacity-70">
Last edited {new Date(n.updatedAt).toLocaleDateString()}{" "}
{new Date(n.updatedAt).toLocaleTimeString()}
</span>
</div>
<div className="flex items-center gap-2">
<button
className={`btn ${n.pinned ? "btn-warning" : "btn-secondary"}`}
disabled={!canEditRow(n)}
onClick={async () => {
await updateNoteMutation({ id: n.id, pinned: !n.pinned })
await refetch()
}}
>
{n.pinned ? "Unpin" : "Pin"}
</button>
{!n.archived && (
<button
className="btn btn-warning"
disabled={!canEditRow(n)}
onClick={async () => {
await updateNoteMutation({ id: n.id, archived: true })
await refetch()
}}
>
Archive
</button>
)}
{n.archived && (
<button
className="btn btn-warning"
disabled={!canEditRow(n)}
onClick={async () => {
await updateNoteMutation({ id: n.id, archived: false })
await setQueryData((prev) =>
(prev ?? []).map((x) => (x.id === n.id ? { ...x, archived: false } : x))
)
await invalidateQuery(listNotes, { projectId, includeArchived })
await refetch()
}}
>
Unarchive
</button>
)}
<button className="btn btn-accent" onClick={() => handleDownloadNote(n)}>
Download
</button>
<button className="btn btn-primary" onClick={() => setEditingId(n.id)}>
{canEditRow(n) ? "Edit" : "View"}
</button>
<button
className="btn btn-error"
disabled={!canEditRow(n)}
onClick={async () => {
if (window.confirm("This note will be permanently deleted. Continue?")) {
await deleteNoteMutation({ id: n.id })
setEditingId(null)
setCreating(false)
await setQueryData((prev) =>
Array.isArray(prev) ? prev.filter((x) => x.id !== n.id) : []
)
await invalidateQuery(listNotes, { projectId, includeArchived })
await refetch()
}
}}
>
Delete
</button>
</div>
</div>
</div>
</li>
))}
{!filteredAndSortedNotes.length && !creating && (
<div className="card bg-base-300 shadow border border-dashed border-base-300">
<div className="card-body items-center text-center p-6">
{notes?.length && searchQuery ? (
<div className="text-lg opacity-70">No notes match your search</div>
) : (
<>
<div className="text-lg opacity-70 mb-3">No notes yet</div>
<button className="btn btn-primary" onClick={() => setCreating(true)}>
Create your first note
</button>
</>
)}
</div>
</div>
)}
</ul>
)}
</div>
)
}