-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperty.jsx
More file actions
185 lines (168 loc) · 6.98 KB
/
Copy pathproperty.jsx
File metadata and controls
185 lines (168 loc) · 6.98 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
// Property mode.
//
// Wikidata property browser (P-ids). Mirrors `entity.jsx` but routes to the
// property cache store and ID space (P31, P279, …). Adds the same IPA toggle
// and inline tests panel.
(function attachProperty() {
const { toIpa, toIpaForEntity } = window.HumanLanguageApp.ipa;
const { TestsPanel } = window.HumanLanguageApp;
function PropertyMode({ params }) {
const { language, setLanguage, navigate } = React.useContext(window.HumanLanguageApp.AppContext);
const propertyId = (params.id && /^P\d+$/i.test(params.id)) ? params.id.toUpperCase() : 'P31';
const [labels, setLabels] = React.useState({});
const [descriptions, setDescriptions] = React.useState({});
const [statements, setStatements] = React.useState({});
const [availableLanguages, setAvailableLanguages] = React.useState([]);
const [propertyLabels, setPropertyLabels] = React.useState({});
const [entityLabels, setEntityLabels] = React.useState({});
const [isLoading, setIsLoading] = React.useState(true);
const [showIpa, setShowIpa] = React.useState(params.ipa === '1');
const [ipaLabel, setIpaLabel] = React.useState('');
const [ipaDesc, setIpaDesc] = React.useState('');
const { apiClient, cacheManager, labelManager } = window;
React.useEffect(() => {
let cancelled = false;
(async () => {
setIsLoading(true);
setIpaLabel('');
setIpaDesc('');
try {
const preferredLangs = [...new Set([
...(navigator.languages || []).map((l) => l.split('-')[0]),
'en',
])];
const commonLanguages = ['en','fr','de','es','it','pt','ru','zh','ja','ko','ar','hi','bn','ta','te','ml','ur','fa','tr','vi'];
const allLanguages = [...new Set([...preferredLangs, ...commonLanguages])];
const languages = allLanguages.join('|');
let cached = await cacheManager.getFromCache(cacheManager.stores.PROPERTIES, propertyId);
let data;
if (!cached || !cacheManager.isCachedDataComplete(cached, languages)) {
data = await apiClient.fetchProperty(propertyId, languages);
if (data) await cacheManager.saveToCache(cacheManager.stores.PROPERTIES, propertyId, data);
} else {
data = cached.data;
}
if (cancelled) return;
if (!data) {
setLabels({}); setDescriptions({}); setStatements({}); setAvailableLanguages([]);
setIsLoading(false);
return;
}
setLabels(data.labels || {});
setDescriptions(data.descriptions || {});
setStatements(data.claims || {});
const langSet = new Set([
...Object.keys(data.labels || {}),
...Object.keys(data.descriptions || {}),
]);
setAvailableLanguages(Array.from(langSet).sort());
if (!langSet.has(language)) {
const fallback = preferredLangs.find((l) => langSet.has(l)) || 'en';
setLanguage(fallback);
}
const { propertyLabels: pl, entityLabels: el } =
await labelManager.loadAllLabels(data.claims || {}, languages);
if (!cancelled) {
setPropertyLabels(pl);
setEntityLabels(el);
setIsLoading(false);
}
} catch {
if (!cancelled) {
setLabels({}); setDescriptions({}); setStatements({}); setAvailableLanguages([]);
setIsLoading(false);
}
}
})();
return () => { cancelled = true; };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [propertyId]);
React.useEffect(() => {
if (!showIpa) {
setIpaLabel('');
setIpaDesc('');
return;
}
let cancelled = false;
(async () => {
const baseDesc = descriptions?.[language]?.value || descriptions?.en?.value || '';
const entity = { labels, descriptions, claims: statements };
const [ipa1, ipa2] = await Promise.all([
toIpaForEntity(entity, language),
baseDesc ? toIpa(baseDesc, language) : Promise.resolve(''),
]);
if (cancelled) return;
setIpaLabel(ipa1 || '');
setIpaDesc(ipa2 || '');
})();
return () => { cancelled = true; };
}, [showIpa, language, propertyId, labels, descriptions, statements]);
const currentLabel = labels?.[language]?.value || 'No label available';
const otherLabels = (availableLanguages || [])
.filter((l) => l !== language && labels?.[l]?.value)
.slice(0, 5);
const descriptionsList = [language, ...otherLabels]
.map((l) => descriptions?.[l]?.value).filter(Boolean);
const getLabel = labelManager.createGetLabelFunction
? labelManager.createGetLabelFunction(propertyId, labels, entityLabels, propertyLabels, language)
: (id) => id;
const StatementsSection = window.StatementComponents?.StatementsSection;
const LoadingComponent = window.LoadingComponents?.LoadingComponent;
if (isLoading && LoadingComponent) return <LoadingComponent />;
const testRunners = [
{
label: 'Quick smoke',
run: async () => {
console.log('Fetching P31 (instance of)…');
const data = await apiClient.fetchProperty('P31', 'en');
console.log('Got labels.en =', data?.labels?.en?.value);
},
},
];
return (
<section aria-label="Property browser">
<div className="toolbar">
<span>Property: <strong>{propertyId}</strong></span>
<button
type="button"
className={showIpa ? 'active' : ''}
onClick={() => setShowIpa((v) => !v)}
>
{showIpa ? '✓ IPA' : 'Show in IPA'}
</button>
<a
className="entity-info"
style={{ marginLeft: 'auto' }}
href={`https://www.wikidata.org/wiki/Property:${propertyId}`}
target="_blank"
rel="noopener noreferrer"
>
View on Wikidata ↗
</a>
</div>
<h1>{showIpa && ipaLabel ? <span className="ipa-token">{ipaLabel}</span> : currentLabel}</h1>
{otherLabels.map((l) => <h2 key={l}>{labels[l].value}</h2>)}
{descriptionsList.length > 0 ? (
descriptionsList.map((d, i) => (
<p key={i}>{i === 0 && showIpa && ipaDesc ? <span className="ipa-token">{ipaDesc}</span> : d}</p>
))
) : (
<p>No description available.</p>
)}
{StatementsSection ? (
<StatementsSection
statements={statements}
subjectId={propertyId}
getLabel={getLabel}
onEntityClick={(id) => navigate('entity', { id })}
onPropertyClick={(id) => navigate('property', { id })}
selectedLanguage={language}
/>
) : null}
<TestsPanel title="Run tests" runners={testRunners} />
</section>
);
}
window.HumanLanguageApp.modes = window.HumanLanguageApp.modes || {};
window.HumanLanguageApp.modes.property = PropertyMode;
})();