Skip to content

Commit a8ce3dd

Browse files
authored
Merge pull request #29 from kWeglinski/fix/tab-choice
fi tab choices
2 parents 958b991 + eec4b7e commit a8ce3dd

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 0.14.1
2+
3+
- Fix: when user wanted to use only libretranslate the whole app crashed due to invalid tab choices.
4+
15
### 0.14.0
26

37
- Fix: enforce font color (chrome bug) on text-area, based on theme. No more dark text on dark bg

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sth-libretranslate",
33
"private": true,
4-
"version": "0.14.0",
4+
"version": "0.14.1",
55
"type": "module",
66
"scripts": {
77
"dev": "concurrently \"npm run node\" \"vite\"",

src/switcher.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,28 @@ function a11yProps(index: number) {
3636
};
3737
}
3838

39+
const ToolMap: { [key: number]: string } = {
40+
0: "libreTranslate",
41+
1: "languageTool",
42+
};
43+
44+
const getInitTab = (
45+
browserTab: number,
46+
tools: Record<string, boolean>
47+
) => {
48+
const tool = ToolMap[browserTab];
49+
if (!tool || !tools[tool]) {
50+
const newTarget = tools.libreTranslate ? 0 : 1;
51+
localStorage.setItem("tab", newTarget.toString());
52+
return newTarget;
53+
}
54+
return browserTab;
55+
};
56+
3957
export const Switcher = () => {
4058
const { libreTranslate, languageTool } = useSystemStatus();
41-
const initTab = parseInt(localStorage.getItem("tab") ?? "0");
59+
const browserTab = parseInt(localStorage.getItem("tab") ?? "-1");
60+
const initTab = getInitTab(browserTab, { libreTranslate, languageTool });
4261

4362
const [tab, setTab] = useState(initTab);
4463
const tabSetter = (val: number) => {
@@ -55,8 +74,12 @@ export const Switcher = () => {
5574
onChange={(_, val) => tabSetter(val)}
5675
aria-label="basic tabs example"
5776
>
58-
{libreTranslate && <Tab label="Translate" {...a11yProps(0)} />}
59-
{languageTool && <Tab label="Language Check" {...a11yProps(1)} />}
77+
{libreTranslate && (
78+
<Tab value={0} label="Translate" {...a11yProps(0)} />
79+
)}
80+
{languageTool && (
81+
<Tab value={1} label="Language Check" {...a11yProps(1)} />
82+
)}
6083
</Tabs>
6184
</Box>
6285
<CustomTabPanel value={tab} index={0}>

0 commit comments

Comments
 (0)