Skip to content

Commit f1272f3

Browse files
committed
♻️ Refactor duplicate codes
1 parent e3e4947 commit f1272f3

File tree

5 files changed

+68
-60
lines changed

5 files changed

+68
-60
lines changed

src/components/Usage.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { UsageDiv } from "./styles/Output.styled";
2+
3+
type Props = {
4+
cmd: "themes" | "projects" | "socials";
5+
marginY?: boolean;
6+
};
7+
8+
const arg = {
9+
themes: { placeholder: "theme-name", example: "ubuntu" },
10+
projects: { placeholder: "project-no", example: "ubuntu" },
11+
socials: { placeholder: "social-no", example: "ubuntu" },
12+
};
13+
14+
const Usage: React.FC<Props> = ({ cmd, marginY = false }) => {
15+
const action = cmd === "themes" ? "set" : "go";
16+
return (
17+
<UsageDiv data-testid={`${cmd}-invalid-arg`} marginY={marginY}>
18+
Usage: {cmd} {action} &#60;{arg[cmd].placeholder}&#62; <br />
19+
eg: {cmd} {action} {arg[cmd].example}
20+
</UsageDiv>
21+
);
22+
};
23+
24+
export default Usage;

src/components/commands/Projects.tsx

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { useContext, useEffect } from "react";
2-
import _ from "lodash";
3-
import { checkRedirect, getCurrentCmdArry } from "../../utils/funcs";
4-
import { UsageDiv } from "../styles/Output.styled";
2+
import {
3+
checkRedirect,
4+
getCurrentCmdArry,
5+
isArgInvalid,
6+
} from "../../utils/funcs";
57
import {
68
ProjectContainer,
79
ProjectDesc,
810
ProjectsIntro,
911
ProjectTitle,
1012
} from "../styles/Projects.styled";
1113
import { termContext } from "../Terminal";
14+
import Usage from "../Usage";
1215

1316
const Projects: React.FC = () => {
1417
const { arg, history, rerender } = useContext(termContext);
@@ -26,23 +29,13 @@ const Projects: React.FC = () => {
2629
}, [arg, rerender, currentCommand]);
2730

2831
/* ===== check arg is valid ===== */
29-
const checkArg = (a: string[]) => {
30-
if (
31-
a[0] !== "go" ||
32-
!_.includes([1, 2, 3, 4], parseInt(a[1])) ||
33-
arg.length > 2
34-
)
35-
return (
36-
<UsageDiv data-testid="projects-invalid-arg">
37-
Usage: projects go &#60;project-no&#62; <br />
38-
eg: projects go 1
39-
</UsageDiv>
40-
);
41-
return null;
42-
};
32+
const checkArg = () =>
33+
isArgInvalid(arg, "go", ["1", "2", "3", "4"]) ? (
34+
<Usage cmd="projects" />
35+
) : null;
4336

4437
return arg.length > 0 || arg.length > 2 ? (
45-
checkArg(arg)
38+
checkArg()
4639
) : (
4740
<div data-testid="projects">
4841
<ProjectsIntro>
@@ -55,10 +48,7 @@ const Projects: React.FC = () => {
5548
<ProjectDesc>{desc}</ProjectDesc>
5649
</ProjectContainer>
5750
))}
58-
<UsageDiv marginY>
59-
Usage: projects go &#60;project-no&#62; <br />
60-
eg: projects go 1
61-
</UsageDiv>
51+
<Usage cmd="projects" marginY />
6252
</div>
6353
);
6454
};

src/components/commands/Socials.tsx

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useContext, useEffect } from "react";
2-
import _ from "lodash";
32
import { ProjectsIntro } from "../styles/Projects.styled";
43
import { Cmd, CmdDesc, CmdList, HelpWrapper } from "../styles/Help.styled";
54
import {
65
checkRedirect,
76
generateTabs,
87
getCurrentCmdArry,
8+
isArgInvalid,
99
} from "../../utils/funcs";
10-
import { UsageDiv } from "../styles/Output.styled";
1110
import { termContext } from "../Terminal";
11+
import Usage from "../Usage";
1212

1313
const Socials: React.FC = () => {
1414
const { arg, history, rerender } = useContext(termContext);
@@ -26,23 +26,13 @@ const Socials: React.FC = () => {
2626
}, [arg, rerender, currentCommand]);
2727

2828
/* ===== check arg is valid ===== */
29-
const checkArg = (a: string[]) => {
30-
if (
31-
a[0] !== "go" ||
32-
!_.includes([1, 2, 3, 4], parseInt(a[1])) ||
33-
arg.length > 2
34-
)
35-
return (
36-
<UsageDiv data-testid="socials-invalid-arg">
37-
Usage: socials go &#60;social-no&#62; <br />
38-
eg: socials go 1
39-
</UsageDiv>
40-
);
41-
return null;
42-
};
29+
const checkArg = () =>
30+
isArgInvalid(arg, "go", ["1", "2", "3", "4"]) ? (
31+
<Usage cmd="socials" />
32+
) : null;
4333

4434
return arg.length > 0 || arg.length > 2 ? (
45-
checkArg(arg)
35+
checkArg()
4636
) : (
4737
<HelpWrapper data-testid="socials">
4838
<ProjectsIntro>Here are my social links</ProjectsIntro>
@@ -53,10 +43,7 @@ const Socials: React.FC = () => {
5343
<CmdDesc>- {url}</CmdDesc>
5444
</CmdList>
5545
))}
56-
<UsageDiv marginY>
57-
Usage: socials go &#60;social-id&#62; <br />
58-
eg: socials go 1
59-
</UsageDiv>
46+
<Usage cmd="socials" marginY />
6047
</HelpWrapper>
6148
);
6249
};

src/components/commands/Themes.tsx

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { useContext, useEffect } from "react";
22
import _ from "lodash";
33
import { themeContext } from "../../App";
4-
import { UsageDiv, Wrapper } from "../styles/Output.styled";
4+
import { Wrapper } from "../styles/Output.styled";
55
import { ThemeSpan, ThemesWrapper } from "../styles/Themes.styled";
6-
import { checkThemeRedirect, getCurrentCmdArry } from "../../utils/funcs";
6+
import {
7+
checkThemeRedirect,
8+
getCurrentCmdArry,
9+
isArgInvalid,
10+
} from "../../utils/funcs";
711
import { termContext } from "../Terminal";
812
import theme from "../styles/themes";
13+
import Usage from "../Usage";
914

1015
const myThemes = _.keys(theme);
1116

@@ -25,30 +30,19 @@ const Themes: React.FC = () => {
2530
}, [arg, rerender, currentCommand]);
2631

2732
/* ===== check arg is valid ===== */
28-
const checkArg = (a: string[]) => {
29-
if (a[0] !== "set" || !_.includes(myThemes, a[1]) || arg.length > 2)
30-
return (
31-
<UsageDiv data-testid="themes-invalid-arg">
32-
Usage: themes set &#60;theme-name&#62; <br />
33-
eg: themes set dark
34-
</UsageDiv>
35-
);
36-
return null;
37-
};
33+
const checkArg = () =>
34+
isArgInvalid(arg, "set", myThemes) ? <Usage cmd="themes" /> : null;
3835

3936
return arg.length > 0 || arg.length > 2 ? (
40-
checkArg(arg)
37+
checkArg()
4138
) : (
4239
<Wrapper data-testid="themes">
4340
<ThemesWrapper>
4441
{myThemes.map(myTheme => (
4542
<ThemeSpan key={myTheme}>{myTheme}</ThemeSpan>
4643
))}
4744
</ThemesWrapper>
48-
<UsageDiv marginY>
49-
Usage: themes set &#60;theme-name&#62; <br />
50-
eg: themes set dark
51-
</UsageDiv>
45+
<Usage cmd="themes" marginY />
5246
</Wrapper>
5347
);
5448
};

src/utils/funcs.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ export const generateTabs = (num = 0): string => {
1414
return tabs;
1515
};
1616

17+
/**
18+
* Check arg is valid
19+
* @param {string[]} arg - The arg array
20+
* @param {string} action - The action to compare | "go" | "set"
21+
* @param {string[]} options - Option array to compare | "dark" | "1"
22+
* @returns {boolean} boolean
23+
*/
24+
export const isArgInvalid = (
25+
arg: string[],
26+
action: string,
27+
options: string[]
28+
) => arg[0] !== action || !_.includes(options, arg[1]) || arg.length > 2;
29+
1730
/**
1831
* Transform current cmd & arg into array
1932
* then return back the array

0 commit comments

Comments
 (0)