Skip to content

Commit

Permalink
feat: localize keyboard shortcut (#325)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandre Fauquette <alex.fauquette@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 19, 2022
1 parent e5d97cc commit e2a49e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
32 changes: 32 additions & 0 deletions src/l10n-shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getLang } from "./localeStorageManager";

type Shortcuts = {
yes: string;
no: string;
skip: string;
};

type ShortcutLocalization = {
en: Shortcuts;
[locale: string]: Partial<Shortcuts>;
};

const SHORTCUT_LOCALISATION = {
en: {
yes: "y",
no: "n",
skip: "k",
},
fr: {
yes: "o",
no: "n",
skip: "k",
},
};

export const getShortcuts = (lang?: string): ShortcutLocalization => {
return {
...SHORTCUT_LOCALISATION.en,
...SHORTCUT_LOCALISATION[lang ?? getLang()],
};
};
16 changes: 9 additions & 7 deletions src/pages/questions/QuestionDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "../../const";
import { reformatValueTag } from "../../utils";
import robotoff from "../../robotoff";
import { getShortcuts } from "../../l10n-shortcuts";
import { getQuestionSearchParams } from "../../components/QuestionFilter/useFilterSearch";
import CroppedLogo from "../../components/CroppedLogo";
import ZoomableImage from "../../components/ZoomableImage";
Expand Down Expand Up @@ -91,6 +92,7 @@ const QuestionDisplay = ({
const [nbOfPotentialQuestion, setNbOfPotentialQuestions] =
React.useState(null);

const shortcuts = getShortcuts();
const theme = useTheme();
const isDesktop = useMediaQuery(theme.breakpoints.up("md"));

Expand Down Expand Up @@ -127,20 +129,20 @@ const QuestionDisplay = ({
function handleShortCut(event) {
const preventShortCut = event.target.tagName.toUpperCase() === "INPUT";
if (question?.insight_id && !preventShortCut) {
switch (event.keyCode) {
case 75: // K
switch (event.key) {
case shortcuts.skip:
answerQuestion({
value: SKIPPED_INSIGHT,
insightId: question.insight_id,
});
break;
case 79: // O
case shortcuts.yes:
answerQuestion({
value: CORRECT_INSIGHT,
insightId: question.insight_id,
});
break;
case 78: // N
case shortcuts.no:
answerQuestion({
value: WRONG_INSIGHT,
insightId: question.insight_id,
Expand Down Expand Up @@ -292,7 +294,7 @@ const QuestionDisplay = ({
sx={{ display: "flex", flexDirection: "column", flexGrow: 1 }}
>
<DeleteIcon />
{t("questions.no")} (n)
{t("questions.no")} ({shortcuts.no})
</Button>
<Button
onClick={() =>
Expand All @@ -307,7 +309,7 @@ const QuestionDisplay = ({
size="large"
sx={{ display: "flex", flexDirection: "column", flexGrow: 1 }}
>
{t("questions.yes")} (o)
{t("questions.yes")} ({shortcuts.yes})
</Button>
</Stack>
<Button
Expand All @@ -323,7 +325,7 @@ const QuestionDisplay = ({
autoFocus
sx={{ py: "1rem" }}
>
{t("questions.skip")} (k)
{t("questions.skip")} ({shortcuts.skip})
</Button>
</Stack>
);
Expand Down

0 comments on commit e2a49e7

Please sign in to comment.