Skip to content

Commit 33401c8

Browse files
author
lucifer
committed
feat: 如果不在力扣站,提示前往
1 parent 23f0ad4 commit 33401c8

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

src/App.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Tabs, Tag, Button, Table, Empty, message } from "antd";
33

44
import { copy } from "./utils";
55
import db from "./db/db";
6+
import { LEETCODE_CN_URL, LEETCODE_URL } from "./constant/index";
67

78
import "antd/dist/antd.css";
89
import "./App.css";
@@ -11,13 +12,17 @@ const { TabPane } = Tabs;
1112
const { problems } = db;
1213
const dataSource = Object.values(problems);
1314

15+
function inLeetCodeWebsite(url) {
16+
return [LEETCODE_CN_URL, LEETCODE_URL].some((u) => url.includes(u));
17+
}
18+
1419
function TagOrLink({ link, text, style, color }) {
1520
return link !== null ? (
16-
<a className="link" href={link} rel="noopener noreferrer" target="_blank">
17-
<Button type="link">{text}</Button>
18-
</a>
21+
<Button type="link" href={link} target="_blank">
22+
{text}
23+
</Button>
1924
) : (
20-
<div style={style}>
25+
<div style={{ display: "inline-block", ...style }}>
2126
<Tag color={color}>{text}</Tag>
2227
</div>
2328
);
@@ -30,16 +35,13 @@ const columns = [
3035
width: "300",
3136
align: "center",
3237
render: (name, row) => (
33-
<a
34-
className="link"
35-
href={`https://leetcode-cn.com/problems/${name}/`}
36-
rel="noopener noreferrer"
38+
<Button
39+
type="link"
40+
href={`${LEETCODE_CN_URL}/problems/${name}/`}
3741
target="_blank"
3842
>
39-
<Button type="link">
40-
{row.id}.{name}
41-
</Button>
42-
</a>
43+
{row.id}.{name}
44+
</Button>
4345
),
4446
},
4547
{
@@ -77,35 +79,49 @@ function App() {
7779
const match = currentUrl.match(/problems\/(.+?)\//);
7880
const problemId = match && match[1];
7981
setProblemId(problemId);
80-
setShow(!!problems[problemId]);
82+
setHasSolution(!!problems[problemId]);
83+
setInLeetCode(inLeetCodeWebsite(currentUrl));
8184
});
8285

8386
const [problemId, setProblemId] = useState("");
84-
const [show, setShow] = useState(false);
87+
const [hasSolution, setHasSolution] = useState(false);
88+
const [inLeetCode, setInLeetCode] = useState(false);
89+
90+
if (!inLeetCode)
91+
return (
92+
<div className="container" style={{ textAlign: "center" }}>
93+
<div>快打开力扣,开始刷题吧~</div>
94+
<Button type="link" href={LEETCODE_CN_URL} target="_blank">
95+
力扣中国
96+
</Button>
97+
<Button type="link" href={LEETCODE_URL} target="_blank">
98+
力扣国际
99+
</Button>
100+
</div>
101+
);
85102

86103
return (
87104
<div className="container">
88-
{show ? (
105+
{hasSolution ? (
89106
<Tabs defaultActiveKey="0">
90107
<TabPane tab="前置知识" key="0">
91-
{problems[problemId].pre.map(({ id, link, text, tag }) => (
92-
<TagOrLink key={id} text={text} link={link} color={tag.color} />
108+
{problems[problemId].pre.map(({ id, link, text, color }) => (
109+
<TagOrLink key={id} text={text} link={link} color={color} />
93110
))}
94111
</TabPane>
95112
<TabPane tab="关键点" key="1">
96-
{problems[problemId].keyPoints.map(({ id, link, text, tag }) => (
97-
<TagOrLink key={text} text={text} link={link} color={tag.color} />
113+
{problems[problemId].keyPoints.map(({ id, link, text, color }) => (
114+
<TagOrLink key={text} text={text} link={link} color={color} />
98115
))}
99116
</TabPane>
100117
<TabPane tab="题解" key="2">
101-
<a
102-
className="link nav"
118+
<Button
119+
type="link"
103120
href={problems[problemId].solution}
104-
rel="noopener noreferrer"
105121
target="_blank"
106122
>
107-
<Button type="link">前往题解</Button>
108-
</a>
123+
前往题解
124+
</Button>
109125
</TabPane>
110126
<TabPane tab="代码" key="3">
111127
<div className="code-block">

src/constant/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const LEETCODE_CN_URL = "https://leetcode-cn.com";
2+
export const LEETCODE_URL = "https://leetcode.com";

0 commit comments

Comments
 (0)