-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfind-word.web-view.tsx
More file actions
130 lines (118 loc) · 4.58 KB
/
Copy pathfind-word.web-view.tsx
File metadata and controls
130 lines (118 loc) · 4.58 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
import type { NetworkObject } from '@papi/core';
import papi, { logger } from '@papi/frontend';
import { useLocalizedStrings } from '@papi/frontend/react';
import type { IEntry, IEntryService, LexiconWebViewProps, PartialEntry } from 'lexicon';
import { SearchBar } from 'platform-bible-react';
import { debounce } from 'platform-bible-utils';
import { useCallback, useEffect, useMemo, useState } from 'react';
import AddNewEntryButton from '../components/add-new-entry-button';
import EntryList from '../components/entry-list';
import EntryListWrapper from '../components/entry-list-wrapper';
import { LOCALIZED_STRING_KEYS } from '../types/localized-string-keys';
globalThis.webViewComponent = function LexiconFindWord({
analysisLanguage,
projectId,
vernacularLanguage,
word,
}: LexiconWebViewProps) {
const [localizedStrings] = useLocalizedStrings(LOCALIZED_STRING_KEYS);
const [matchingEntries, setMatchingEntries] = useState<IEntry[] | undefined>();
const [lexiconNetworkObject, setLexiconNetworkObject] = useState<
NetworkObject<IEntryService> | undefined
>();
const [isFetching, setIsFetching] = useState(false);
const [searchTerm, setSearchTerm] = useState(word ?? '');
useEffect(() => {
papi.networkObjects
.get<IEntryService>('lexicon.entryService')
// eslint-disable-next-line promise/always-return
.then((networkObject) => {
logger.info('Got network object:', networkObject);
setLexiconNetworkObject(networkObject);
})
.catch((e) => logger.error(`${localizedStrings['%lexicon_error_gettingNetworkObject%']}`, e));
}, [localizedStrings]);
const fetchEntries = useCallback(
async (untrimmedSurfaceForm: string) => {
if (!projectId || !lexiconNetworkObject) {
const errMissingParam = localizedStrings['%lexicon_error_missingParam%'];
if (!projectId) logger.warn(`${errMissingParam}projectId`);
if (!lexiconNetworkObject) logger.warn(`${errMissingParam}lexiconNetworkObject`);
return;
}
const surfaceForm = untrimmedSurfaceForm.trim();
if (!surfaceForm) {
logger.warn('No word provided for search');
return;
}
logger.info(`Fetching entries for ${surfaceForm}`);
setIsFetching(true);
const entries = await lexiconNetworkObject.getEntries(projectId, { surfaceForm });
setIsFetching(false);
setMatchingEntries(entries ?? []);
},
[lexiconNetworkObject, localizedStrings, projectId],
);
const debouncedFetchEntries = useMemo(() => debounce(fetchEntries, 500), [fetchEntries]);
const onSearch = useCallback(
(searchQuery: string) => {
setSearchTerm(searchQuery);
debouncedFetchEntries(searchQuery);
},
[debouncedFetchEntries],
);
const addEntry = useCallback(
async (entry: PartialEntry) => {
if (!projectId || !lexiconNetworkObject) {
const errMissingParam = localizedStrings['%lexicon_error_missingParam%'];
if (!projectId) logger.warn(`${errMissingParam}projectId`);
if (!lexiconNetworkObject) logger.warn(`${errMissingParam}lexiconNetworkObject`);
return;
}
logger.info(`Adding entry: ${JSON.stringify(entry)}`);
const addedEntry = await lexiconNetworkObject.addEntry(projectId, entry);
if (addedEntry) {
onSearch(Object.values<string | undefined>(addedEntry.lexemeForm).pop() ?? '');
await papi.commands.sendCommand('lexicon.displayEntry', projectId, addedEntry.id);
} else {
logger.error(`${localizedStrings['%lexicon_error_failedToAddEntry%']}`);
}
},
[lexiconNetworkObject, localizedStrings, onSearch, projectId],
);
return (
<EntryListWrapper
elementHeader={
<div className="tw:flex tw:gap-2">
<div className="tw:w-full tw:max-w-72">
<SearchBar
isFullWidth
onSearch={onSearch}
placeholder={localizedStrings['%lexicon_findWord_textField%']}
value={searchTerm}
/>
</div>
<div>
<AddNewEntryButton
addEntry={addEntry}
analysisLanguage={analysisLanguage ?? ''}
headword={searchTerm}
vernacularLanguage={vernacularLanguage ?? ''}
/>
</div>
</div>
}
elementList={
matchingEntries ? (
<EntryList
analysisLanguage={analysisLanguage ?? ''}
entries={matchingEntries}
vernacularLanguage={vernacularLanguage ?? ''}
/>
) : undefined
}
isLoading={isFetching}
hasItems={!!matchingEntries?.length}
/>
);
};