-
Notifications
You must be signed in to change notification settings - Fork 5
/
AppEndpointsTable.tsx
206 lines (197 loc) · 7.56 KB
/
AppEndpointsTable.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { ActionIcon, Flex, Menu, TextInput, Tooltip, UnstyledButton } from "@mantine/core"
import { useFetcher, useNavigation, useParams } from "@remix-run/react"
import React, { useMemo, useState } from "react"
import { LuBook, LuPlay } from "react-icons/lu"
import { RiStarFill, RiStarLine } from "react-icons/ri"
import FavoriteChain from "../FavoriteChain"
import Chain from "~/components/Chain"
import { ChainSandboxProvider } from "~/components/ChainSandbox/state"
import ContextMenuTarget from "~/components/ContextMenuTarget"
import CopyTextButton from "~/components/CopyTextButton"
import { DataTable } from "~/components/DataTable"
import useActionNotification, {
ActionNotificationData,
} from "~/hooks/useActionNotification"
import { Blockchain, PortalApp } from "~/models/portal/sdk"
import ChainSandboxSideDrawer from "~/routes/account.$accountId.$appId._index/components/ChainSandboxSideDrawer"
import { AnalyticActions, AnalyticCategories, trackEvent } from "~/utils/analytics"
import { CHAIN_DOCS_URL, getAppEndpointUrl } from "~/utils/chainUtils"
import { DOCS_PATH } from "~/utils/utils"
type AppEndpointsProps = {
app: PortalApp
blockchains: Blockchain[]
searchTerm: string
readOnly: boolean
}
const AppEndpointsTable = ({
app,
blockchains,
searchTerm,
readOnly,
}: AppEndpointsProps) => {
const { appId } = useParams()
const fetcher = useFetcher()
const fetcherData = fetcher.data as ActionNotificationData
const navigation = useNavigation()
const [selectedBlockchain, setSelectedBlockchain] = useState<Blockchain>()
const favoriteChains = app.settings.favoritedChainIDs
// handle notification for menu fetcher action
useActionNotification(fetcherData)
const chains = useMemo(() => {
const fav = blockchains
.filter((chain) => favoriteChains?.includes(chain.id))
.map((c) => ({
...c,
favorite: true,
}))
const other = blockchains
.filter((chain) => !favoriteChains?.includes(chain.id))
.map((c) => ({
...c,
favorite: false,
}))
return [...fav, ...other]
}, [favoriteChains, blockchains])
return (
<>
<ChainSandboxProvider initialStateValue={{ selectedApp: app }}>
<ChainSandboxSideDrawer
blockchain={selectedBlockchain}
chains={chains}
onSideDrawerClose={() => setSelectedBlockchain(undefined)}
/>
</ChainSandboxProvider>
{blockchains && (
<DataTable
data={chains.map((chain) => {
return {
chain: {
element: (
<Flex gap="sm">
<FavoriteChain
blockchain={chain}
favoriteChains={favoriteChains}
readOnly={navigation.state !== "idle"}
/>
<Chain chain={chain} />
</Flex>
),
value: `${chain.description} ${chain.blockchain}`,
cellProps: {
style: { minWidth: "340px" },
width: "35%",
},
},
endpointUrl: {
element: (
<TextInput
readOnly
miw={300}
rightSection={
<CopyTextButton
size={16}
value={getAppEndpointUrl(chain, appId)}
variant="transparent"
width={28}
/>
}
value={getAppEndpointUrl(chain, appId)}
/>
),
},
action: {
element: (
<Flex gap="lg" justify="flex-end">
<Tooltip withArrow label="Try in Sandbox">
<ActionIcon
aria-label={`Open Chain Sandbox for ${chain.description}`}
color="gray"
radius="xl"
size={40}
variant="subtle"
onClick={() => {
setSelectedBlockchain(chain)
trackEvent({
category: AnalyticCategories.app,
action: AnalyticActions.app_chain_sandbox_try,
label: `Blockchain: ${chain.blockchain}`,
})
}}
>
<LuPlay size={18} style={{ position: "relative", left: 2 }} />
</ActionIcon>
</Tooltip>
<Menu>
<ContextMenuTarget variant="subtle" />
<Menu.Dropdown>
{chain.blockchain && CHAIN_DOCS_URL[chain.blockchain] && (
<Menu.Item leftSection={<LuBook size={18} />}>
<UnstyledButton
component="a"
fz="sm"
href={`${DOCS_PATH}/${CHAIN_DOCS_URL[chain.blockchain]}`}
rel="noreferrer"
target="_blank"
onClick={() => {
trackEvent({
category: AnalyticCategories.app,
action: AnalyticActions.app_chain_docs,
label: chain.id,
})
}}
>
Documentation
</UnstyledButton>
</Menu.Item>
)}
{!readOnly && (
<Menu.Item
leftSection={
chain.favorite ? (
<RiStarFill size={18} />
) : (
<RiStarLine size={18} />
)
}
onClick={() => {
trackEvent({
category: AnalyticCategories.app,
action: AnalyticActions.app_chain_favorite,
label: `${chain.favorite ? "Remove" : "Add"} favorite ${
chain.id
}`,
})
fetcher.submit(
{
isFavorite: String(!chain.favorite),
chainId: chain.id,
favoriteChains: JSON.stringify(favoriteChains),
},
{
method: "post",
},
)
}}
>
{chain.favorite ? "Remove favorite" : "Mark as favorite"}
</Menu.Item>
)}
</Menu.Dropdown>
</Menu>
</Flex>
),
cellProps: {
style: { minWidth: "130px" },
width: "130px%",
},
},
}
})}
paginate={false}
searchTerm={searchTerm}
/>
)}
</>
)
}
export default AppEndpointsTable