forked from solana-foundation/explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseInstructionCard.tsx
More file actions
165 lines (159 loc) · 6.79 KB
/
BaseInstructionCard.tsx
File metadata and controls
165 lines (159 loc) · 6.79 KB
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
import { Address } from '@components/common/Address';
import { useScrollAnchor } from '@providers/scroll-anchor';
import { cn } from '@shared/utils';
import { ParsedInstruction, SignatureResult, TransactionInstruction } from '@solana/web3.js';
import getInstructionCardScrollAnchorId from '@utils/get-instruction-card-scroll-anchor-id';
import React from 'react';
import { Code } from 'react-feather';
import { BaseRawDetails } from './BaseRawDetails';
import { BaseRawParsedDetails } from './BaseRawParsedDetails';
type InstructionProps = {
title: string;
children?: React.ReactNode;
result: SignatureResult;
index: number;
ix: TransactionInstruction | ParsedInstruction;
defaultRaw?: boolean;
innerCards?: JSX.Element[];
eventCards?: JSX.Element[];
childIndex?: number;
// raw can be used to display raw instruction information
raw?: TransactionInstruction;
// will be triggered on requesting raw data for instruction, if present
onRequestRaw?: () => void;
// Extra buttons rendered in the card header next to Raw
headerButtons?: React.ReactNode;
// Show a Collapse/Expand button that hides all card content
collapsible?: boolean;
};
export function BaseInstructionCard({
title,
children,
result,
index,
ix,
defaultRaw,
eventCards,
innerCards,
childIndex,
raw,
onRequestRaw,
headerButtons,
collapsible = false,
}: InstructionProps) {
const [resultClass] = ixResult(result, index);
const [showRaw, setShowRaw] = React.useState(defaultRaw || false);
const [expanded, setExpanded] = React.useState(true);
const rawClickHandler = () => {
if (!defaultRaw && !showRaw && !raw) {
// trigger handler to simulate behaviour for the InstructionCard for the transcation which contains logic in it to fetch raw transaction data
onRequestRaw?.();
}
return setShowRaw(r => !r);
};
const scrollAnchorRef = useScrollAnchor(
getInstructionCardScrollAnchorId(childIndex != null ? [index + 1, childIndex + 1] : [index + 1]),
);
return (
<div className="card" ref={scrollAnchorRef}>
<div className="card-header">
<h3 className="card-header-title mb-0 d-flex align-items-center">
<span className={`badge bg-${resultClass}-soft me-2`}>
#{index + 1}
{childIndex !== undefined ? `.${childIndex + 1}` : ''}
</span>
{title}
</h3>
<div className="d-flex align-items-center gap-2">
{headerButtons}
{collapsible && (
<button
className="btn btn-sm d-flex align-items-center btn-white"
onClick={() => setExpanded(v => !v)}
>
{expanded ? 'Collapse' : 'Expand'}
</button>
)}
<button
disabled={defaultRaw || !expanded}
className={cn(
'btn btn-sm d-flex align-items-center',
showRaw ? 'btn-black active' : 'btn-white',
(defaultRaw || !expanded) && '!e-pointer-events-auto e-cursor-not-allowed',
)}
onClick={rawClickHandler}
>
<Code className="me-2" size={13} /> Raw
</button>
</div>
</div>
{expanded && (
<div className="table-responsive mb-0">
<table className="table table-sm table-nowrap card-table">
<tbody className="list">
{showRaw ? (
<>
<tr>
<td>Program</td>
<td className="text-lg-end">
<Address pubkey={ix.programId} alignRight link />
</td>
</tr>
{'parsed' in ix ? (
<BaseRawParsedDetails ix={ix}>
{raw ? <BaseRawDetails ix={raw} /> : null}
</BaseRawParsedDetails>
) : (
<BaseRawDetails ix={ix} />
)}
</>
) : (
children
)}
{innerCards && innerCards.length > 0 && (
<>
<tr className="table-sep">
<td colSpan={3}>Inner Instructions</td>
</tr>
<tr>
<td colSpan={3}>
{/* !e-m-0 overrides the 1.5rem margin from inner-cards
so the card aligns with the "Inner Instructions" label above */}
<div className="inner-cards !e-m-0">{innerCards}</div>
</td>
</tr>
</>
)}
{eventCards && eventCards.length > 0 && (
<>
<tr className="table-sep">
<td colSpan={3}>Events</td>
</tr>
<tr>
<td colSpan={3}>
<div className="inner-cards">{eventCards}</div>
</td>
</tr>
</>
)}
</tbody>
</table>
</div>
)}
</div>
);
}
function ixResult(result: SignatureResult, index: number) {
if (result.err) {
const err = result.err as any;
const ixError = err['InstructionError'];
if (ixError && Array.isArray(ixError)) {
const [errorIndex, error] = ixError;
if (Number.isInteger(errorIndex) && errorIndex === index) {
return ['warning', `Error: ${JSON.stringify(error)}`];
}
}
return ['dark'];
}
return ['success'];
}