1
1
import { useContext } from 'react' ;
2
- import { Link } from 'react-router-dom' ;
2
+ import { useHistory } from 'react-router-dom' ;
3
3
import { SnippetsContext } from '../../store' ;
4
4
import { Snippet } from '../../typescript/interfaces' ;
5
5
import { dateParser } from '../../utils' ;
6
6
import { Button , Card } from '../UI' ;
7
- import Icon from '@mdi/react ' ;
8
- import { mdiPin } from '@mdi/js ' ;
7
+ import copy from 'clipboard-copy ' ;
8
+ import { SnippetPin } from './SnippetPin ' ;
9
9
10
10
interface Props {
11
11
snippet : Snippet ;
@@ -23,21 +23,22 @@ export const SnippetDetails = (props: Props): JSX.Element => {
23
23
isPinned
24
24
} = props . snippet ;
25
25
26
- const { deleteSnippet, toggleSnippetPin, setSnippet } =
27
- useContext ( SnippetsContext ) ;
26
+ const history = useHistory ( ) ;
27
+
28
+ const { deleteSnippet, setSnippet } = useContext ( SnippetsContext ) ;
28
29
29
30
const creationDate = dateParser ( createdAt ) ;
30
31
const updateDate = dateParser ( updatedAt ) ;
31
32
32
33
const copyHandler = ( ) => {
33
- navigator . clipboard . writeText ( code ) ;
34
+ copy ( code ) ;
34
35
} ;
35
36
36
37
return (
37
38
< Card >
38
39
< h5 className = 'card-title d-flex align-items-center justify-content-between' >
39
40
{ title }
40
- { isPinned ? < Icon path = { mdiPin } size = { 0.8 } color = '#212529' /> : '' }
41
+ < SnippetPin id = { id } isPinned = { isPinned } />
41
42
</ h5 >
42
43
< p > { description } </ p >
43
44
@@ -58,33 +59,22 @@ export const SnippetDetails = (props: Props): JSX.Element => {
58
59
< span > Last updated</ span >
59
60
< span > { updateDate . relative } </ span >
60
61
</ div >
61
-
62
62
< hr />
63
63
64
64
{ /* ACTIONS */ }
65
- < div className = 'd-flex justify-content-between' >
66
- < Link
67
- to = { {
68
- pathname : `/editor/${ id } ` ,
69
- state : { from : window . location . pathname }
70
- } }
71
- >
72
- < Button
73
- text = 'Edit'
74
- color = 'dark'
75
- small
76
- outline
77
- classes = 'me-3'
78
- handler = { ( ) => setSnippet ( id ) }
79
- />
80
- </ Link >
65
+ < div className = 'd-grid g-2' style = { { rowGap : '10px' } } >
81
66
< Button
82
- text = { ` ${ isPinned ? 'Unpin snippet' : 'Pin snippet' } ` }
67
+ text = 'Edit'
83
68
color = 'dark'
84
69
small
85
70
outline
86
- handler = { ( ) => toggleSnippetPin ( id ) }
87
- classes = 'me-3'
71
+ handler = { ( ) => {
72
+ setSnippet ( id ) ;
73
+ history . push ( {
74
+ pathname : `/editor/${ id } ` ,
75
+ state : { from : window . location . pathname }
76
+ } ) ;
77
+ } }
88
78
/>
89
79
< Button
90
80
text = 'Delete'
@@ -93,12 +83,6 @@ export const SnippetDetails = (props: Props): JSX.Element => {
93
83
outline
94
84
handler = { ( ) => deleteSnippet ( id ) }
95
85
/>
96
- </ div >
97
-
98
- < hr />
99
-
100
- { /* COPY */ }
101
- < div className = 'd-grid' >
102
86
< Button text = 'Copy code' color = 'dark' small handler = { copyHandler } />
103
87
</ div >
104
88
</ Card >
0 commit comments