import {AlertContext} from "@/context/AlertContext"; import {SessionContext} from "@/context/SessionContext"; import System from "@/lib/models/System"; import {ChangeEvent, JSX, useContext, useState} from "react"; import InputField from "@/components/form/InputField"; import {faLanguage, faLock, faMagnifyingGlass} from "@fortawesome/free-solid-svg-icons"; import TextInput from "@/components/form/TextInput"; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; import {LangContext, LangContextProps} from "@/context/LangContext"; import {useTranslations} from "next-intl"; import {AIVerbConjugation} from "@/lib/models/QuillSense"; import {AIUsageContext, AIUsageContextProps} from "@/context/AIUsageContext"; interface ConjugationTenses { [tense: string]: { firstPersonSingular?: string; secondPersonSingular?: string; thirdPersonSingular?: string; firstPersonPlural?: string; secondPersonPlural?: string; thirdPersonPlural?: string; présent?: string; passé?: string; } | string; } interface ConjugationResponse { conjugations: { [mode: string]: ConjugationTenses; }; } export default function Conjugator({hasKey}: { hasKey: boolean }): JSX.Element { const {session} = useContext(SessionContext); const {errorMessage} = useContext(AlertContext); const {lang} = useContext(LangContext); const t = useTranslations(); const {setTotalCredits, setTotalPrice} = useContext(AIUsageContext); const [verbToConjugate, setVerbToConjugate] = useState(''); const [inProgress, setInProgress] = useState(false); const [conjugationResponse, setConjugationResponse] = useState(null); async function handleConjugation(): Promise { if (verbToConjugate.trim() === '') { return; } setInProgress(true); try { const response: AIVerbConjugation = await System.authPostToServer( `quillsense/verb-conjugation`, {verb: verbToConjugate}, session.accessToken, lang ); if (!response) { errorMessage(t("conjugator.error.noResponse")); return; } if (response.useYourKey) { setTotalPrice((prevState: number): number => prevState + response.totalPrice) } else { setTotalCredits(response.totalPrice) } setConjugationResponse(response.data as ConjugationResponse); } catch (e: unknown) { if (e instanceof Error) { errorMessage(e.message); } else { errorMessage(t("conjugator.error.unknown")); } } finally { setInProgress(false); } } function renderConjugationTable(tense: string, conjugations: ConjugationTenses[string]): JSX.Element { if (typeof conjugations === 'string') { return (
{conjugations}
); } if (typeof conjugations === 'object' && conjugations !== null) { const hasPersonConjugations = conjugations.firstPersonSingular || conjugations.secondPersonSingular || conjugations.thirdPersonSingular || conjugations.firstPersonPlural || conjugations.secondPersonPlural || conjugations.thirdPersonPlural; if (hasPersonConjugations) { return (

{tense}

{conjugations.firstPersonSingular && (
{t('conjugator.persons.je')} {conjugations.firstPersonSingular}
)} {conjugations.firstPersonPlural && (
{t('conjugator.persons.nous')} {conjugations.firstPersonPlural}
)} {conjugations.secondPersonSingular && (
{t('conjugator.persons.tu')} {conjugations.secondPersonSingular}
)} {conjugations.secondPersonPlural && (
{t('conjugator.persons.vous')} {conjugations.secondPersonPlural}
)} {conjugations.thirdPersonSingular && (
{t('conjugator.persons.il')} {conjugations.thirdPersonSingular}
)} {conjugations.thirdPersonPlural && (
{t('conjugator.persons.ils')} {conjugations.thirdPersonPlural}
)}
); } } return
; } function renderMode(mode: string, tenses: ConjugationTenses): JSX.Element { if (mode === 'infinitif' || mode === 'participe') { return (

{mode}

{Object.entries(tenses).map(([tense, conjugation]: [string, string | ConjugationTenses[string]]) => (

{tense}

{conjugation as string}
))}
); } return (

{mode}

{Object.entries(tenses).map(([tense, conjugations]) => renderConjugationTable(tense, conjugations) )}
); } if (!hasKey) { return (

{t('conjugator.locked.title')}

{t('conjugator.locked.description')}

); } return (
) => setVerbToConjugate(e.target.value)} placeholder={t('conjugator.input.placeholder')} /> } icon={faLanguage} fieldName={t('conjugator.input.label')} actionLabel={t('conjugator.input.action')} actionIcon={faMagnifyingGlass} action={async (): Promise => handleConjugation()} />
{inProgress && (

{t('conjugator.loading')}

)} {!inProgress && conjugationResponse && (

{verbToConjugate}

{Object.entries(conjugationResponse.conjugations).map(([mode, tenses]: [string, ConjugationTenses]) => renderMode(mode, tenses) )}
)} {!inProgress && !conjugationResponse && (

{t('conjugator.welcome.title')}

{t('conjugator.welcome.description')}

)}
); }