-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path$noteId.tsx
65 lines (58 loc) · 1.58 KB
/
$noteId.tsx
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
import { createFileRoute } from '@tanstack/react-router'
import { Helmet } from 'react-helmet-async'
import { z } from 'zod'
import { Layout } from '#/browser/layout'
import { SideMenu } from '#/browser/side-menu'
import { trpcClient } from '#/browser/trpc-client'
import { NoteList } from '#/notes/note-list'
import { NoteTextarea } from '#/notes/note-textarea'
import { useNoteSubscriptions } from '#/notes/use-note-subscriptions'
export const Route = createFileRoute('/notes/$noteId')({
parseParams: (params) => ({
noteId: z.string().parse(params.noteId),
}),
async loader({ context, params }) {
if (context.queries) {
const note = await context.queries.note.byId(params.noteId)
if (!note) {
// this redirect is read server side
// before rendering the React tree
context.redirect.to = '/notes'
}
return {
note,
notes: await context.queries.note.list(),
}
}
},
component: NoteComponent,
})
function NoteComponent() {
useNoteSubscriptions()
const { noteId } = Route.useParams()
const loaderData = Route.useLoaderData()
const noteQuery = trpcClient.note.get.useQuery(
{ id: noteId },
{
initialData: loaderData?.note,
},
)
return (
<Layout sidebar={<SideMenu withBookmarks={false} />}>
<div className='flex flex-col md:flex-row'>
<Helmet>
<title>{`Fastrat - ${noteQuery.data?.content.substring(
0,
20,
)}`}</title>
</Helmet>
<div className='flex-1'>
<NoteTextarea note={noteQuery.data} />
</div>
<div className='flex-1'>
<NoteList notes={loaderData?.notes} />
</div>
</div>
</Layout>
)
}