1
- import { useState } from "react" ;
2
1
import Image from "next/image" ;
3
-
4
2
import { GithubFilled , BookFilled , ExportOutlined } from "@ant-design/icons" ;
5
3
import {
6
4
Space ,
@@ -13,55 +11,66 @@ import {
13
11
Button ,
14
12
Divider ,
15
13
Row ,
14
+ notification ,
16
15
} from "antd" ;
17
16
18
17
import themes from "@/themes.js" ;
19
18
import Logo from "@/images/logo.png" ;
20
19
import useOption from "@/hooks/option.js" ;
21
20
22
21
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 ( ) ;
32
25
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
+ } ) ;
35
32
} ;
36
33
37
- const onPreview = ( ) => {
38
- updateQuerystring ( ) ;
34
+ const onOpenInNewTab = ( ) => {
35
+ window . open ( getImgUrl ( ) , "_blank" ) ;
39
36
} ;
40
37
41
38
const onError = ( ) => {
42
39
setError ( true ) ;
43
- setOpenInNewTabDisabled ( true ) ;
44
- } ;
45
-
46
- const onLoad = ( src ) => {
47
- if ( ! src . currentTarget . src . includes ( "error" ) ) {
48
- setOpenInNewTabDisabled ( false ) ;
49
- }
50
40
} ;
51
41
52
- const onFieldsChange = ( changed_value , values ) => {
42
+ const onFieldsChange = async ( changed_value , values ) => {
53
43
setOptions ( ( prev ) => {
54
- const newValues = {
44
+ const newOptions = {
55
45
...prev ,
56
46
[ changed_value [ 0 ] . name ] : changed_value [ 0 ] . value ,
57
47
} ;
58
- setOpenInNewTabDisabled ( ! checkSame ( newValues ) ) ;
59
- return newValues ;
48
+ if ( changed_value [ 0 ] . name [ 0 ] !== "username" ) {
49
+ updateQuerystring ( newOptions ) ;
50
+ }
51
+ return newOptions ;
60
52
} ) ;
61
53
} ;
62
54
55
+ const onCopyMarkdown = ( ) => {
56
+ navigator . clipboard . writeText (
57
+ `[
61
+ ) + getImgUrl ( )
62
+ } )](https://codeforces.com/profile/${ options . username } )`
63
+ ) ;
64
+ openNotification ( "topRight" ) ;
65
+ } ;
66
+
67
+ const onUsernameEnter = ( ) => {
68
+ updateQuerystring ( options ) ;
69
+ } ;
70
+
63
71
return (
64
72
< >
73
+ { contextHolder }
65
74
< Space className = "main-body" >
66
75
< Card className = "card" >
67
76
< Col className = "card-col" >
@@ -82,7 +91,6 @@ export default function Home() {
82
91
name = "Card Input"
83
92
layout = "horizontal"
84
93
labelCol = { { span : 9 } }
85
- colon = { false }
86
94
initialValues = { options }
87
95
onFieldsChange = { onFieldsChange }
88
96
>
@@ -97,7 +105,7 @@ export default function Home() {
97
105
} ,
98
106
] }
99
107
>
100
- < Input autoComplete = "off" />
108
+ < Input autoComplete = "off" onPressEnter = { onUsernameEnter } />
101
109
</ Form . Item >
102
110
< Form . Item className = "form-item" label = "Theme" name = "theme" >
103
111
< Select
@@ -142,15 +150,19 @@ export default function Home() {
142
150
</ Form . Item >
143
151
< Form . Item className = "form-item" >
144
152
< 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
147
159
</ Button >
148
160
< Button
149
161
type = "default"
150
162
onClick = { onOpenInNewTab }
151
- disabled = { openInNewTabDisabled }
163
+ disabled = { error }
152
164
>
153
- Open in new tab
165
+ Open Image in new tab
154
166
< ExportOutlined />
155
167
</ Button >
156
168
</ Space >
@@ -164,7 +176,6 @@ export default function Home() {
164
176
alt = "Codeforces-Stats"
165
177
fill = "width"
166
178
onError = { onError }
167
- onLoad = { onLoad }
168
179
/>
169
180
</ Col >
170
181
</ Row >
0 commit comments