-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeCard.tsx
134 lines (119 loc) · 3.8 KB
/
NodeCard.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React, { useMemo } from 'react'
import styled from 'styled-components'
import { convertContentToRawText, MEXIT_FRONTEND_URL_BASE, mog, WORKSPACE_HEADER } from '@mexit/core'
import {
CopyButton,
GenericFlex,
IconButton,
MexIcon,
SnippetCardFooter,
SnippetCardWrapper,
SnippetContentPreview
} from '@mexit/shared'
import { useAuthStore } from '../../Hooks/useAuth'
import { getTitleFromPath } from '../../Hooks/useLinks'
import { useNodes } from '../../Hooks/useNodes'
import { useContentStore } from '../../Stores/useContentStore'
import useDataStore from '../../Stores/useDataStore'
import { useRecentsStore } from '../../Stores/useRecentsStore'
export const NodeCardHeader = styled.div<{ $noHover?: boolean }>`
display: flex;
justify-content: space-between;
align-items: center;
gap: ${({ theme }) => theme.spacing.tiny};
font-size: 1.15rem;
cursor: pointer;
user-select: none;
`
export const NodeCard = ({ nodeId }: { nodeId: string }) => {
const { publicNodes, setNodePrivate, setNodePublic, checkNodePublic } = useDataStore()
const { getNode } = useNodes()
const getContent = useContentStore((store) => store.getContent)
const addInRecents = useRecentsStore((s) => s.addRecent)
const getWorkspaceId = useAuthStore((store) => store.getWorkspaceId)
const isNodePublic = useMemo(() => {
return checkNodePublic(nodeId)
}, [publicNodes])
const node = getNode(nodeId)
const contents = getContent(nodeId)
const flipPublicAccess = async () => {
const workspaceHeaders = () => ({
[WORKSPACE_HEADER]: getWorkspaceId(),
Accept: 'application/json, text/plain, */*'
})
if (isNodePublic) {
const request = {
type: 'PUBLIC_SHARING',
subType: 'MAKE_PRIVATE',
body: {
nodeId
},
headers: workspaceHeaders()
}
chrome.runtime.sendMessage(request, (response) => {
const { message, error } = response
if (error) {
mog('ErrorMakingNodePrivate', error)
} else {
addInRecents(nodeId)
setNodePrivate(nodeId)
}
})
} else {
const request = {
type: 'PUBLIC_SHARING',
subType: 'MAKE_PUBLIC',
body: {
nodeId
},
headers: workspaceHeaders()
}
chrome.runtime.sendMessage(request, (response) => {
const { message, error } = response
if (error) {
mog('ErrorMakingNodePublic', error)
} else {
addInRecents(nodeId)
setNodePublic(nodeId)
}
})
}
}
const onNotePublic = (event) => {
event.stopPropagation()
flipPublicAccess()
}
return (
<SnippetCardWrapper>
<NodeCardHeader $noHover>
<GenericFlex>
<MexIcon $noHover icon="gg:file-document" />
{getTitleFromPath(node?.path)}
</GenericFlex>
<GenericFlex>
{isNodePublic ? (
<IconButton
title="Make Note Public"
size="16px"
icon="material-symbols:public-off-rounded"
onClick={onNotePublic}
/>
) : (
<IconButton title="Make Note Private" size="16px" icon="material-symbols:public" onClick={onNotePublic} />
)}
{isNodePublic && (
<CopyButton
text={`${MEXIT_FRONTEND_URL_BASE}/share/${nodeId}`}
size="16px"
beforeCopyTooltip="Copy link"
afterCopyTooltip="Link copied!"
/>
)}
</GenericFlex>
</NodeCardHeader>
{/* TODO: saving raw content for nodes as well would be grand */}
<SnippetContentPreview>{contents && convertContentToRawText(contents.content, ' ')}</SnippetContentPreview>
<SnippetCardFooter>{/* <TagsLabel tags={}/> */}</SnippetCardFooter>
</SnippetCardWrapper>
)
}