/
passage.tsx
148 lines (134 loc) · 5.13 KB
/
passage.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
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
import React, { useId, useRef } from 'react'
import { Inline, ParsedPassage } from '~/lib/parseEsv'
import { Paragraph } from './paragraph'
import { Keystroke } from '~/lib/keystroke'
import { Cursor } from '~/components/cursor'
import { atom, PrimitiveAtom, Provider } from 'jotai'
import { useRect } from '~/lib/hooks/useRect'
import { useHydrateAtoms } from 'jotai/react/utils'
import { api } from '~/utils/api'
import { useSession } from 'next-auth/react'
import { useRouter } from 'next/router'
export const PassageContext = React.createContext<{
rect: DOMRect | null
}>({
rect: null,
})
export const positionAtom = atom<Inline[]>([])
export const keystrokesAtom = atom<Keystroke[]>([])
export const isPassageActiveAtom = atom(false)
export const isPassageFocusedAtom = atom(false)
export const currentVerseAtom = atom<string>('')
export const autofocusAtom = atom(false)
export const passageIdAtom = atom<string>('')
type InitialValues<T> = [PrimitiveAtom<T>, T]
function HydrateAtoms({
initialValues,
children,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initialValues: InitialValues<any>[]
children: React.ReactNode
}) {
useHydrateAtoms(initialValues)
return children
}
export function Passage({
passage,
autofocus = true,
autoSelect = true,
}: {
passage: ParsedPassage
autofocus?: boolean
autoSelect?: boolean
}) {
const passageId = useId()
const passageRef = useRef<HTMLDivElement>(null)
const passageRect = useRect(passageRef)
const { data: sessionData } = useSession()
const typingSession = api.typingSession.getOrCreateTypingSession.useQuery(
undefined,
{
enabled: sessionData?.user?.id != null,
},
)
const chapterHistory = api.chapterHistory.getChapterHistory.useQuery(
{ chapter: passage.firstVerse.chapter, book: passage.firstVerse.book },
{
enabled: sessionData?.user?.id != null,
},
)
const isRootPath = useRouter().pathname === '/'
const H2Component = isRootPath ? 'h2' : 'h1'
return (
<Provider>
<HydrateAtoms
initialValues={[
[
currentVerseAtom,
autoSelect ? passage.firstVerse.value : '',
],
[autofocusAtom, autofocus],
[passageIdAtom, passageId],
]}
>
<div
ref={passageRef}
id={passageId}
className="passage prose relative z-0 w-full dark:prose-invert"
>
<PassageContext.Provider
value={{
rect: passageRect,
}}
>
{passage.nodes.map((node, pIndex) => {
switch (node.type) {
case 'paragraph':
return (
<Paragraph
key={pIndex}
node={node}
passage={passage}
typingSession={typingSession}
chapterHistory={chapterHistory}
/>
)
case 'h2':
return (
<H2Component
key={pIndex}
className="prose-h2 mb-4 mt-2 text-2xl"
>
{node.text}
</H2Component>
)
case 'h3':
return (
<h3
className="prose-h3 mt-0 text-xl font-semibold tracking-wide"
key={pIndex}
>
{node.text}
</h3>
)
case 'h4':
return (
<h4
className="prose-h4 text-lg font-medium tracking-wide"
key={pIndex}
>
{node.text}
</h4>
)
default:
break
}
})}
</PassageContext.Provider>
<Cursor passageId={passageId} />
</div>
</HydrateAtoms>
</Provider>
)
}