-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCallInfoPanel.tsx
More file actions
131 lines (116 loc) · 3.22 KB
/
CallInfoPanel.tsx
File metadata and controls
131 lines (116 loc) · 3.22 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
/**
* Panel showing call context info for the current instruction.
*
* Displays a banner for invoke/return/revert events and
* lists resolved pointer ref values (arguments, return
* data, etc.).
*/
import React from "react";
import {
useTraceContext,
type ResolvedCallInfo,
type ResolvedPointerRef,
} from "./TraceContext.js";
// CSS is expected to be imported by the consuming application
// import "./CallInfoPanel.css";
export interface CallInfoPanelProps {
/** Whether to show raw pointer JSON */
showPointers?: boolean;
/** Custom class name */
className?: string;
}
function formatBanner(info: ResolvedCallInfo): string {
const name = info.identifier || "(anonymous)";
if (info.kind === "invoke") {
const prefix =
info.callType === "external"
? "Calling (external)"
: info.callType === "create"
? "Creating"
: "Calling";
return `${prefix} ${name}()`;
}
if (info.kind === "return") {
return `Returned from ${name}()`;
}
// revert
if (info.panic !== undefined) {
return `Reverted: panic 0x${info.panic.toString(16)}`;
}
return `Reverted in ${name}()`;
}
function bannerClassName(kind: ResolvedCallInfo["kind"]): string {
if (kind === "invoke") {
return "call-info-banner-invoke";
}
if (kind === "return") {
return "call-info-banner-return";
}
return "call-info-banner-revert";
}
/**
* Shows call context info when the current instruction
* has an invoke, return, or revert context.
*/
export function CallInfoPanel({
showPointers = false,
className = "",
}: CallInfoPanelProps): JSX.Element | null {
const { currentCallInfo } = useTraceContext();
if (!currentCallInfo) {
return null;
}
return (
<div className={`call-info-panel ${className}`.trim()}>
<div
className={`call-info-banner ${bannerClassName(currentCallInfo.kind)}`}
>
{formatBanner(currentCallInfo)}
</div>
{currentCallInfo.pointerRefs.length > 0 && (
<div className="call-info-refs">
{currentCallInfo.pointerRefs.map((ref) => (
<PointerRefItem
key={ref.label}
ref_={ref}
showPointer={showPointers}
/>
))}
</div>
)}
</div>
);
}
interface PointerRefItemProps {
ref_: ResolvedPointerRef;
showPointer: boolean;
}
function PointerRefItem({
ref_,
showPointer,
}: PointerRefItemProps): JSX.Element {
return (
<div className="call-info-ref">
<span className="call-info-ref-label">{ref_.label}:</span>
<span className="call-info-ref-value">
{ref_.error ? (
<span className="call-info-ref-error" title={ref_.error}>
Error: {ref_.error}
</span>
) : ref_.value !== undefined ? (
<code className="call-info-ref-resolved">{ref_.value}</code>
) : (
<span className="call-info-ref-pending">(resolving...)</span>
)}
</span>
{showPointer && !!ref_.pointer && (
<details className="call-info-ref-pointer">
<summary>Pointer</summary>
<pre className="call-info-ref-pointer-json">
{JSON.stringify(ref_.pointer, null, 2)}
</pre>
</details>
)}
</div>
);
}