-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathDocRow.tsx
147 lines (129 loc) · 3.92 KB
/
DocRow.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
import { useContext, useMemo, useEffect, useState } from 'react'
import cn from 'classnames'
import { MDXRemote } from 'next-mdx-remote'
import { IReferenceItem, IItemDoc, IGasDoc } from 'types'
import { EthereumContext } from 'context/ethereumContext'
import { GITHUB_REPO_URL } from 'util/constants'
import { parseGasPrices, findMatchingForkName } from 'util/gas'
import * as Doc from 'components/ui/Doc'
import DynamicFee from './DynamicFee'
type Props = {
itemDoc: IItemDoc
referenceItem: IReferenceItem
gasDocs: IGasDoc
dynamicFeeForkName: string
}
const docComponents = {
h1: Doc.H1,
h2: Doc.H2,
h3: Doc.H3,
p: Doc.P,
ul: Doc.UL,
ol: Doc.OL,
li: Doc.LI,
table: Doc.Table,
th: Doc.TH,
td: Doc.TD,
a: Doc.A,
pre: Doc.Pre,
}
const API_DYNAMIC_FEE_DOC_URL = '/api/getDynamicDoc'
const DocRow = ({
itemDoc,
referenceItem,
gasDocs,
dynamicFeeForkName,
}: Props) => {
const { common, forks, selectedFork } = useContext(EthereumContext)
const [dynamicFeeDocMdx, setDynamicFeeDocMdx] = useState()
const dynamicFeeDoc = useMemo(() => {
if (!gasDocs) {
return null
}
const fork = findMatchingForkName(forks, Object.keys(gasDocs), selectedFork)
return fork && common ? parseGasPrices(common, gasDocs[fork]) : null
}, [forks, selectedFork, gasDocs, common])
useEffect(() => {
let controller: AbortController | null = new AbortController()
const fetchDynamicFeeDoc = async () => {
try {
const response = await fetch(API_DYNAMIC_FEE_DOC_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content: dynamicFeeDoc }),
signal: controller?.signal,
})
const data = await response.json()
setDynamicFeeDocMdx(data.mdx)
controller = null
} catch (error) {
setDynamicFeeDocMdx(undefined)
}
}
if (dynamicFeeDoc) {
fetchDynamicFeeDoc()
}
return () => controller?.abort()
}, [dynamicFeeDoc])
return (
<div className="text-sm px-4 md:px-8 py-8 bg-indigo-50 dark:bg-black-600">
{itemDoc && (
<>
<table className="table-auto mb-6 bg-indigo-100 dark:bg-black-500 rounded font-medium">
<thead>
<tr className="text-gray-500 uppercase text-xs tracking-wide">
<td className="pt-3 px-4">Since</td>
{itemDoc.meta.group && <td className="pt-3 px-4">Group</td>}
</tr>
</thead>
<tbody>
<tr>
<td className="pb-3 px-4">{itemDoc.meta.fork}</td>
{itemDoc.meta.group && (
<td className="pb-3 px-4">{itemDoc.meta.group}</td>
)}
</tr>
</tbody>
</table>
<div className="flex flex-col lg:flex-row">
<div
className={cn({
'flex-1 lg:pr-8': referenceItem.dynamicFee,
})}
>
<MDXRemote {...itemDoc.mdxSource} components={docComponents} />
{dynamicFeeForkName && dynamicFeeDocMdx && (
<MDXRemote
compiledSource=""
{...(dynamicFeeDocMdx ? dynamicFeeDocMdx : {})}
components={docComponents}
/>
)}
</div>
{dynamicFeeForkName && (
<DynamicFee
referenceItem={referenceItem}
fork={dynamicFeeForkName}
/>
)}
</div>
</>
)}
{!itemDoc && (
<div>
There is no reference doc for this yet. Why not{' '}
<a
className="underline font-medium"
href={`${GITHUB_REPO_URL}/new/main/docs/opcodes`}
target="_blank"
rel="noreferrer"
>
contribute?
</a>{' '}
;)
</div>
)}
</div>
)
}
export default DocRow