gues-kucinako/src/App.tsx
2021-09-12 23:06:42 -07:00

256 lines
7.7 KiB
TypeScript

import React, {useState} from 'react';
import './App.scss';
import {declineSaimiar} from './saimiar_morphology';
import {SaiEntryProps, JutEntryProps, ElesuEntryProps} from './dbtypes';
const backendUrl = 'https://kucinakobackend.ichigo.everydayimshuflin.com';
enum Conlang {
Saimiar = 'saimiar',
Elesu = 'elesu',
Tukvaysi = 'tukvaysi',
Juteyuji = 'juteyuji',
}
enum SearchDirection {
ToConlang,
ToEnglish
}
const renderConlang = (conlang: Conlang): string => {
if (conlang === Conlang.Saimiar) {
return 'Saimiar';
}
if (conlang === Conlang.Elesu) {
return 'Elésu';
}
if (conlang === Conlang.Juteyuji) {
return 'Juteyuji';
}
if (conlang === Conlang.Tukvaysi) {
return 'Tukvaysi';
}
};
function buildRequest(searchTerm: string, conlang: Conlang, direction: SearchDirection, jsonHandler: (json: Object) => void) {
const specForConlang = {
[Conlang.Saimiar]: 'sai',
[Conlang.Juteyuji]: 'sai',
[Conlang.Tukvaysi]: 'tuk',
[Conlang.Elesu]: 'elesu',
};
const conlangDb = conlang.toString();
const field = direction === SearchDirection.ToConlang ? 'eng' : specForConlang[conlang];
const query = `${conlangDb}?${field}=like.*${searchTerm}*`;
const effectiveUri = `${backendUrl}/${query}`;
fetch(`${effectiveUri}`)
.then((resp) => resp.json())
.then((json) => {
jsonHandler(json);
});
}
interface EntryProps {
conlang: Conlang;
entry: SaiEntryProps | JutEntryProps | ElesuEntryProps;
key: string;
}
const Entry = (props: EntryProps) => {
const {conlang} = props;
if (conlang === Conlang.Saimiar) {
return <SaiEntry entry={ props.entry as SaiEntryProps } />;
}
if (conlang === Conlang.Juteyuji) {
return <JutEntry entry={ props.entry as JutEntryProps } />;
}
if (conlang === Conlang.Elesu) {
return <ElesuEntry entry={ props.entry as ElesuEntryProps } />;
}
return <div>Conlang { conlang } not yet supported</div>;
};
const SaiEntry = (props: {entry: SaiEntryProps}) => {
const {entry} = props;
const synCategory = entry.syn_category;
const isNominal = synCategory === 'nominal';
return (
<div className="searchResult" key={ entry.id }>
<b>{ entry.sai }</b> - { entry.eng }
<br />
<span className="synclass">
<i>{ entry.syn_category }</i>
{ entry.morph_type ? `\t\t${entry.morph_type}` : null }
<br/>
{ isNominal ? formatMorphology(entry) : null }
</span>
</div>
);
};
const JutEntry = (props: {entry: JutEntryProps}) => {
const {entry} = props;
return (
<div className="searchResult" key={ entry.id }>
<b>{ entry.jut }</b> - { entry.eng }
<br/>
<span className="synclass">
{ entry.syn_category === 'noun' ? entry.gender : null }
</span>
</div>);
};
const ElesuEntry = (props: {entry: ElesuEntryProps}) => {
const {entry} = props;
return (
<div className="searchResult" key={ entry.id }>
<b>{ entry.elesu }</b> - { entry.eng }
<br/>
<span className="synclass">
{ entry.syn_category }
</span>
</div>);
};
function formatMorphology(entry) {
const decl = declineSaimiar(entry);
if (!decl) {
return '';
}
return (<span style={ {fontSize: 'medium', color: '#6a3131'} } >
Abs: <i>{decl.abs}</i>, Erg: <i>{decl.erg}</i>,
Adp: <i>{decl.adp}</i>,
All: <i>{decl.all}</i>,
Loc: <i>{decl.loc}</i>,
Ell: <i>{decl.ell}</i>,
Inst: <i>{decl.inst}</i>,
Rel: <i>{decl.rel}</i>
</span>);
}
interface ResultsProps {
searchResults: Array<any>;
searchTerm: string;
conlang: Conlang;
direction: SearchDirection;
}
const Results = (props: ResultsProps) => {
const content = () => {
const {conlang} = props;
const num = props.searchResults.length;
const renderedName = renderConlang(conlang);
const searchType = (props.direction === SearchDirection.ToConlang) ? `English -> ${renderedName}` : `${renderedName} -> English`;
const header = (
<div className="searchResultHeader" key="header">
Searched for <b>{ props.searchTerm }</b>, { searchType }, found { num } result(s)
</div>);
const entries = props.searchResults.map(
(entry, _idx) => <Entry entry={ entry } key= { entry.id } conlang={ conlang } />,
);
return [header].concat(entries);
};
const results = props.searchResults;
return (
<div className="results">
{ results ? content() : 'No search' }
</div>
);
};
const App = (_props) => {
const [searchResults, setSearchResults] = useState(null);
const [conlang, setConlang] = useState(Conlang.Saimiar);
const [direction, setDirection] = useState(null);
const [searchTerm, setSearchTerm] = useState(null);
const [searchBoxInput, setSearchBoxInput] = useState('');
const searchConlang = (_evt) => {
const searchTerm = searchBoxInput;
if (searchTerm === '') {
setSearchResults(null);
setSearchTerm(null);
setDirection(null);
return;
}
buildRequest(searchTerm, conlang, SearchDirection.ToEnglish, (json) => {
setSearchResults(json);
setSearchTerm(searchTerm);
setDirection(SearchDirection.ToEnglish);
});
};
const searchEng = (_evt) => {
const searchTerm = searchBoxInput;
if (searchTerm === '') {
setSearchResults(null);
setSearchTerm(null);
setDirection(null);
} else {
buildRequest(searchTerm, conlang, SearchDirection.ToConlang, (json) => {
setSearchResults(json);
setSearchTerm(searchTerm);
setDirection(SearchDirection.ToConlang);
});
}
};
const handleLangChange = (evt) => {
const conlang: Conlang = evt.target.value as Conlang;
setConlang(conlang);
setSearchResults(null);
};
const conlangs = [Conlang.Saimiar, Conlang.Elesu, Conlang.Tukvaysi, Conlang.Juteyuji];
const langSelectDropdown = (
<select value={conlang} onChange={ handleLangChange }>
{conlangs.map((conlang) => <option value={conlang} key={conlang}>{renderConlang(conlang)}</option>)}
</select>
);
return (
<main>
<div className="container">
<div className="search">
<h1>Kucinako - Wordbook of Arzhanai languages</h1>
<p><b>Kucinako</b> is a dictionary of words in various languages of the world Arzhanø, and their English
equivalents.</p>
<div className="textInput">
<input className="textInput" type="text" value={ searchBoxInput } onChange={ (evt) => {
setSearchBoxInput(evt.target.value);
} } />
</div>
<br/>
{ langSelectDropdown }
<button onClick={ searchConlang } className="searchButton">{renderConlang(conlang)}</button>
<button onClick={ searchEng } className="searchButton">English</button>
</div>
<Results
searchResults={ searchResults }
searchTerm= { searchTerm }
conlang={ conlang }
direction={ direction }
/>
</div>
</main>
);
};
export default App;