Skip to content

Commit 14f26f2

Browse files
committed
small frontend changes
1 parent dfe2428 commit 14f26f2

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

src/hooks/option.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useState } from "react";
1+
import { useState } from "react";
22
import qs from "fast-querystring";
33
import Error from "@/images/error.svg";
44

@@ -7,7 +7,7 @@ const defaultOption = {
77
theme: "default",
88
disable_animations: false,
99
show_icons: true,
10-
force_username: false,
10+
force_username: true,
1111
};
1212

1313
const useOption = () => {
@@ -19,22 +19,18 @@ const useOption = () => {
1919
return error ? Error.src : `/api/card?${query}`;
2020
};
2121

22-
const updateQuerystring = () => {
22+
const updateQuerystring = (newOptions) => {
2323
setError(false);
24-
setQuerystring(qs.stringify(options));
25-
};
26-
27-
const checkSame = (values) => {
28-
return qs.stringify(values) === querystring;
24+
setQuerystring(qs.stringify(newOptions));
2925
};
3026

3127
return {
3228
options,
3329
setOptions,
3430
getImgUrl,
31+
error,
3532
setError,
3633
updateQuerystring,
37-
checkSame,
3834
};
3935
};
4036

src/pages/index.js

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { useState } from "react";
21
import Image from "next/image";
3-
42
import { GithubFilled, BookFilled, ExportOutlined } from "@ant-design/icons";
53
import {
64
Space,
@@ -13,55 +11,66 @@ import {
1311
Button,
1412
Divider,
1513
Row,
14+
notification,
1615
} from "antd";
1716

1817
import themes from "@/themes.js";
1918
import Logo from "@/images/logo.png";
2019
import useOption from "@/hooks/option.js";
2120

2221
export default function Home() {
23-
const {
24-
options,
25-
setOptions,
26-
getImgUrl,
27-
setError,
28-
updateQuerystring,
29-
checkSame,
30-
} = useOption();
31-
const [openInNewTabDisabled, setOpenInNewTabDisabled] = useState(true);
22+
const { options, setOptions, getImgUrl, error, setError, updateQuerystring } =
23+
useOption();
24+
const [api, contextHolder] = notification.useNotification();
3225

33-
const onOpenInNewTab = () => {
34-
window.open(getImgUrl(), "_blank");
26+
const openNotification = (placement) => {
27+
api.info({
28+
message: "Success",
29+
description: `Copied to clipboard!`,
30+
placement,
31+
});
3532
};
3633

37-
const onPreview = () => {
38-
updateQuerystring();
34+
const onOpenInNewTab = () => {
35+
window.open(getImgUrl(), "_blank");
3936
};
4037

4138
const onError = () => {
4239
setError(true);
43-
setOpenInNewTabDisabled(true);
44-
};
45-
46-
const onLoad = (src) => {
47-
if (!src.currentTarget.src.includes("error")) {
48-
setOpenInNewTabDisabled(false);
49-
}
5040
};
5141

52-
const onFieldsChange = (changed_value, values) => {
42+
const onFieldsChange = async (changed_value, values) => {
5343
setOptions((prev) => {
54-
const newValues = {
44+
const newOptions = {
5545
...prev,
5646
[changed_value[0].name]: changed_value[0].value,
5747
};
58-
setOpenInNewTabDisabled(!checkSame(newValues));
59-
return newValues;
48+
if (changed_value[0].name[0] !== "username") {
49+
updateQuerystring(newOptions);
50+
}
51+
return newOptions;
6052
});
6153
};
6254

55+
const onCopyMarkdown = () => {
56+
navigator.clipboard.writeText(
57+
`[![Codeforces Stats](${
58+
window.location.href.substring(
59+
0,
60+
window.location.href.lastIndexOf("/")
61+
) + getImgUrl()
62+
})](https://codeforces.com/profile/${options.username})`
63+
);
64+
openNotification("topRight");
65+
};
66+
67+
const onUsernameEnter = () => {
68+
updateQuerystring(options);
69+
};
70+
6371
return (
6472
<>
73+
{contextHolder}
6574
<Space className="main-body">
6675
<Card className="card">
6776
<Col className="card-col">
@@ -82,7 +91,6 @@ export default function Home() {
8291
name="Card Input"
8392
layout="horizontal"
8493
labelCol={{ span: 9 }}
85-
colon={false}
8694
initialValues={options}
8795
onFieldsChange={onFieldsChange}
8896
>
@@ -97,7 +105,7 @@ export default function Home() {
97105
},
98106
]}
99107
>
100-
<Input autoComplete="off" />
108+
<Input autoComplete="off" onPressEnter={onUsernameEnter} />
101109
</Form.Item>
102110
<Form.Item className="form-item" label="Theme" name="theme">
103111
<Select
@@ -142,15 +150,19 @@ export default function Home() {
142150
</Form.Item>
143151
<Form.Item className="form-item">
144152
<Space className="submit-wrapper">
145-
<Button type="primary" onClick={onPreview}>
146-
Preview
153+
<Button
154+
type="primary"
155+
onClick={onCopyMarkdown}
156+
disabled={error}
157+
>
158+
Copy Markdown
147159
</Button>
148160
<Button
149161
type="default"
150162
onClick={onOpenInNewTab}
151-
disabled={openInNewTabDisabled}
163+
disabled={error}
152164
>
153-
Open in new tab
165+
Open Image in new tab
154166
<ExportOutlined />
155167
</Button>
156168
</Space>
@@ -164,7 +176,6 @@ export default function Home() {
164176
alt="Codeforces-Stats"
165177
fill="width"
166178
onError={onError}
167-
onLoad={onLoad}
168179
/>
169180
</Col>
170181
</Row>

0 commit comments

Comments
 (0)