-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmorpheme.tsx
More file actions
144 lines (135 loc) · 4.03 KB
/
morpheme.tsx
File metadata and controls
144 lines (135 loc) · 4.03 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
import { groupBy } from "lodash-es"
import React from "react"
import { ReactNode } from "react"
import { MdClose } from "react-icons/md/index"
import { IconButton, Link } from "src/components"
import * as Dailp from "src/graphql/dailp"
import * as css from "./morpheme.css"
import { documentWordPath, glossaryRoute } from "./routes"
type BasicMorphemeSegment = NonNullable<Dailp.FormFieldsFragment["segments"]>[0]
/** Specific details about some morpheme */
export const MorphemeDetails = (props: {
documentId: string
segment: BasicMorphemeSegment
cherokeeRepresentation: Dailp.CherokeeOrthography
hideDialog: () => void
}) => {
// Use the right tag name from the jump.
const matchingTag = props.segment.matchingTag
const gloss = matchingTag?.tag || props.segment.gloss
const occurrences = <h3>Known Occurrences of "{gloss}"</h3>
// Get the morpheme title and definition from the server.
// TODO Only request the specific definition we need, not all three.
const [tag] = Dailp.useTagQuery({
pause: !props.segment.gloss,
variables: {
gloss: props.segment.gloss,
system: props.cherokeeRepresentation,
},
})
let titleArea: ReactNode | null = null
let content = occurrences
if (tag.data?.tag) {
const matchingTag = tag.data.tag
titleArea = matchingTag?.title ? (
<h2 className={css.margined}>{matchingTag.title}</h2>
) : null
content = (
<>
{matchingTag?.definition ? <p>{matchingTag.definition}</p> : null}
<p>
<Link href={glossaryRoute(tag.data.tag.tag)}>View in glossary</Link>
</p>
{occurrences}
</>
)
}
return (
<>
{titleArea}
<IconButton
className={css.closeButton}
aria-label="Close Dialog"
onClick={props.hideDialog}
>
<MdClose size={32} />
</IconButton>
<div className={css.scrollable}>
{content}
<SimilarMorphemeList
documentId={props.documentId}
gloss={props.segment.gloss!}
hideDialog={props.hideDialog}
isGlobal={!!props.segment.matchingTag}
/>
</div>
</>
)
}
/**
* List of morphemes that share the given gloss, and all words that contain
* those morphemes.
*/
const SimilarMorphemeList = (props: {
gloss: string
documentId: string
isGlobal: boolean
hideDialog: () => void
}) => {
const [{ data, fetching, error }] = Dailp.useMorphemeQuery({
pause: !props.gloss,
variables: {
documentId: props.isGlobal ? null : props.documentId,
morphemeGloss: props.gloss,
},
})
if (fetching) {
return <p>Loading...</p>
} else if (error) {
return <p>Error! {error}</p>
} else if (!data || !data.documents) {
return <p>None Found</p>
} else {
const docs = data.documents as Dailp.WordsInDocument[]
const docTypes = groupBy(docs, "documentType")
const similarWords = Object.entries(docTypes).map(([ty, documents]) => (
<section key={ty}>
<h4>{documentTypeToHeading(ty)}</h4>
<ul>
{documents.map((m: any, i: number) =>
m.forms.map((word: any, j: number) => (
<li key={i * 10000 + j}>
{word.normalizedSource ?? word.source}:{" "}
{word.englishGloss.join(", ")} (
{!!word.document ? (
<>
{word.index ? (
<Link
href={documentWordPath(word.document?.slug, word.index)}
>
{word.document.slug.toUpperCase()}
</Link>
) : (
<>{word.document.slug.toUpperCase()}</>
)}
</>
) : null}
)
</li>
))
)}
</ul>
</section>
))
return <>{similarWords}</>
}
}
function documentTypeToHeading(ty: string) {
if (ty === "REFERENCE") {
return "In Reference Materials"
} else if (ty === "CORPUS") {
return "In Documents"
} else {
return "Miscellaneous"
}
}