diff --git a/package.json b/package.json index a2e5ec4..6976c4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "1.8.5", + "version": "1.8.6", "author": "lingdocs.com", "description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations", "homepage": "https://verbs.lingdocs.com", diff --git a/src/App.tsx b/src/App.tsx index 1acd745..68e8418 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,7 +6,7 @@ * */ -import { useEffect, useState } from "react"; +import { useEffect } from "react"; import verbs from "./verbs"; import nounsAdjs from "./nouns-adjs"; import Pashto from "./components/Pashto"; @@ -26,7 +26,7 @@ import * as T from "./types"; import { isNounEntry } from "./lib/type-predicates"; import defualtTextOptions from "./lib/default-text-options"; import PhraseBuilder from "./components/vp-explorer/VPExplorer"; -const textOptionsLocalStorageName = "textOptions2"; +import { useStickyState } from "./library"; type VerbType = "simple" | "stative compound" | "dynamic compound"; const verbTypes: VerbType[] = [ "simple", @@ -48,13 +48,13 @@ const allVerbs = verbs.map((v: { entry: T.DictionaryEntry, complement?: T.Dictio })); function App() { - const [verbTs, setVerbTs] = useState(0); - const [verbTypeShowing, setVerbTypeShowing] = useState("simple"); - const [regularIrregular, setRegularIrregular] = useState<"regular" | "irregular">("regular"); - const [transitivityShowing, setTransitivityShowing] = useState("intransitive"); - const [showingTextOptions, setShowingTextOptions] = useState(false); - const [textOptions, setTextOptions] = useState(defualtTextOptions); - const [theme, setTheme] = useState<"light" | "dark">("light"); + const [verbTs, setVerbTs] = useStickyState(0, "verbTs1"); + const [verbTypeShowing, setVerbTypeShowing] = useStickyState("simple", "vTypeShowing"); + const [regularIrregular, setRegularIrregular] = useStickyState<"regular" | "irregular">("regular", "regIrreg"); + const [transitivityShowing, setTransitivityShowing] = useStickyState("intransitive", "transitivityShowing1"); + const [showingTextOptions, setShowingTextOptions] = useStickyState(false, "showTextOpts1"); + const [textOptions, setTextOptions] = useStickyState(defualtTextOptions, "textOpts1"); + const [theme, setTheme] = useStickyState<"light" | "dark">("light", "theme1"); // const onlyGrammTrans = (arr: Transitivity[]) => ( // arr.length === 1 && arr[0] === "grammatically transitive" // ); @@ -64,45 +64,6 @@ function App() { // } // } - useEffect(() => { - const verbTsRaw = localStorage.getItem("verbTs"); - const verbTypeShowing = localStorage.getItem("verbTypeShowing") as undefined | VerbType; - const regularIrregular = localStorage.getItem("regularIrregular") as "regular" | "irregular"; - const transitivitiyShowing = localStorage.getItem("transitivityShowing") as undefined | T.Transitivity; - const theme = localStorage.getItem("theme"); - const textOptionst = localStorage.getItem(textOptionsLocalStorageName); - if (regularIrregular) { - setRegularIrregular(regularIrregular); - } - if (verbTsRaw) { - setVerbTs(JSON.parse(verbTsRaw)); - } - if (verbTypeShowing) { - setVerbTypeShowing(verbTypeShowing); - } - if (transitivitiyShowing) { - setTransitivityShowing(transitivitiyShowing); - } - if (theme) { - setTheme(theme as "light" | "dark"); - } - if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) { - setTheme("dark"); - } - if (textOptionst) { - setTextOptions(JSON.parse(textOptionst) as T.TextOptions); - } - }, []); - - useEffect(() => { - localStorage.setItem("verbTs", verbTs.toString()); - localStorage.setItem("regularIrregular", regularIrregular); - localStorage.setItem("verbTypeShowing", verbTypeShowing); - localStorage.setItem("transitivityShowing", transitivityShowing); - localStorage.setItem(textOptionsLocalStorageName, JSON.stringify(textOptions)); - localStorage.setItem("theme", theme); - }); - useEffect(() => { document.documentElement.setAttribute("data-theme", theme); }, [theme]) diff --git a/src/components/np-picker/NPPronounPicker.tsx b/src/components/np-picker/NPPronounPicker.tsx index eb19711..694bb3a 100644 --- a/src/components/np-picker/NPPronounPicker.tsx +++ b/src/components/np-picker/NPPronounPicker.tsx @@ -60,7 +60,7 @@ function NPPronounPicker({ onChange, pronoun, asObject, clearButton, opts }: { clearButton?: JSX.Element, opts: T.TextOptions, }) { - const [display, setDisplay] = useStickyState<"persons" | "p" | "e">("persons", "prounoun-picker-display"); + const [display, setDisplay] = useStickyState<"persons" | "p" | "e">("e", "prounoun-picker-display"); const p = personToPickerState(pronoun.person); function handleClick(row: number, col: number) { diff --git a/src/components/vp-explorer/VPExplorer.tsx b/src/components/vp-explorer/VPExplorer.tsx index 7e015a7..66b1bd7 100644 --- a/src/components/vp-explorer/VPExplorer.tsx +++ b/src/components/vp-explorer/VPExplorer.tsx @@ -41,21 +41,40 @@ export function VPExplorer(props: { getNounByTs: (ts: number) => T.NounEntry | undefined, getVerbByTs: (ts: number) => T.VerbEntry | undefined, })) { + console.log("passedVerb", props.verb); const [subject, setSubject] = useStickyState(undefined, "subjectNPSelection"); + // not quite working with stickyState const [mode, setMode] = useStickyState<"charts" | "phrases" | "quiz">("phrases", "verbExplorerMode"); + // const [mode, setMode] = useState<"charts" | "phrases" | "quiz">("charts"); const [showAnswer, setShowAnswer] = useState(false); const passedVerb = props.verb; - const [verb, setVerb] = useStickyState( - passedVerb - ? (old) => makeVerbSelection(passedVerb, setSubject, old) - : undefined, - "verbExplorerVerb", - ); + // this isn't quite working + // const [verb, setVerb] = useStickyState( + // passedVerb + // ? (old) => makeVerbSelection(passedVerb, setSubject, old) + // : undefined, + // "verbExplorerVerb", + // ); + const [verb, setVerb] = useState( + passedVerb ? makeVerbSelection(passedVerb, setSubject) : undefined + ) + useEffect(() => { + if (mode === "quiz") { + if (!verb) setMode("phrases"); + handleResetQuiz(); + } + // TODO: better system with all this + // eslint-disable-next-line + }, []); useEffect(() => { if (!passedVerb) { setVerb(undefined); } else { setVerb(o => makeVerbSelection(passedVerb, setSubject, o)); + if (mode === "quiz") { + // TODO: Better + setMode("charts"); + } } // eslint-disable-next-line }, [passedVerb]); @@ -204,6 +223,8 @@ export function VPExplorer(props: { } {(verb && (mode === "charts")) && } + {mode === "quiz" &&
+
} } diff --git a/src/lib/useStickyState.ts b/src/lib/useStickyState.ts index 6e86a04..3d21b1a 100644 --- a/src/lib/useStickyState.ts +++ b/src/lib/useStickyState.ts @@ -8,7 +8,7 @@ import { useEffect, useState } from "react"; * @param key a key for saving the state in locolStorage * @returns */ -export default function useStickyState(defaultValue: T | ((old: T | undefined) => T), key: string): [ +export default function useStickyState(defaultValue: T | ((old: T | undefined) => T), key: string): [ value: T, setValue: React.Dispatch>, ] {