-
Notifications
You must be signed in to change notification settings - Fork 268
/
DebugToolsMenu.tsx
228 lines (214 loc) · 7.32 KB
/
DebugToolsMenu.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type {Heartbeat} from '../heartbeat';
import type {ReactNode} from 'react';
import type {ExclusiveOr} from 'shared/typeUtils';
import {holdingAltAtom} from '../ChangedFile';
import {debugLogMessageTraffic} from '../ClientToServerAPI';
import {DropdownField, DropdownFields} from '../DropdownFields';
import {InlineErrorBadge} from '../ErrorNotice';
import {Subtle} from '../Subtle';
import {Tooltip} from '../Tooltip';
import {DagCommitInfo} from '../dag/dagCommitInfo';
import {useHeartbeat} from '../heartbeat';
import {t, T} from '../i18n';
import {atomWithOnChange, readAtom} from '../jotaiUtils';
import {dagWithPreviews} from '../previews';
import {RelativeDate} from '../relativeDate';
import {
latestCommitsData,
latestUncommittedChangesData,
mergeConflicts,
repositoryInfo,
} from '../serverAPIState';
import {ComponentExplorerButton} from './ComponentExplorer';
import {readInterestingAtoms, serializeAtomsState} from './getInterestingAtoms';
import {VSCodeBadge, VSCodeButton, VSCodeCheckbox} from '@vscode/webview-ui-toolkit/react';
import {atom, useAtom, useAtomValue} from 'jotai';
import {useState, useCallback, useEffect} from 'react';
import './DebugToolsMenu.css';
export default function DebugToolsMenu({dismiss}: {dismiss: () => unknown}) {
return (
<DropdownFields
title={<T>Internal Debugging Tools</T>}
icon="pulse"
data-testid="internal-debug-tools-dropdown"
className="internal-debug-tools-dropdown">
<Subtle>
<T>
This data is only intended for debugging Interactive Smartlog and may not capture all
issues.
</T>
</Subtle>
<DropdownField title={<T>Performance</T>}>
<DebugPerfInfo />
</DropdownField>
<DropdownField title={<T>Commit graph</T>}>
<DebugDagInfo />
</DropdownField>
<DropdownField title={<T>Internal Jotai State</T>}>
<InternalState />
</DropdownField>
<DropdownField title={<T>Server/Client Messages</T>}>
<ServerClientMessageLogging />
</DropdownField>
<DropdownField title={<T>Component Explorer</T>}>
<ComponentExplorerButton dismiss={dismiss} />
</DropdownField>
</DropdownFields>
);
}
function InternalState() {
const [successMessage, setSuccessMessage] = useState<null | string>(null);
const generate = () => {
// No need for useAtomValue - no need to re-render or recalculate this function.
const needSerialize = readAtom(holdingAltAtom);
const atomsState = readInterestingAtoms();
const value = needSerialize ? serializeAtomsState(atomsState) : atomsState;
// eslint-disable-next-line no-console
console.log(value);
setSuccessMessage(`logged to console!${needSerialize ? ' (serialized)' : ''}`);
};
return (
<div className="internal-debug-tools-atom-state">
<Tooltip
placement="bottom"
title={t(
'Capture a snapshot of selected Jotai atom states, log it to the dev tools console.',
)}>
<VSCodeButton onClick={generate} appearance="secondary">
<T>Take Snapshot</T>
</VSCodeButton>
{successMessage && <Subtle>{successMessage}</Subtle>}
</Tooltip>
</div>
);
}
const logMessagesState = atomWithOnChange(atom(debugLogMessageTraffic.shoudlLog), newValue => {
debugLogMessageTraffic.shoudlLog = newValue;
// eslint-disable-next-line no-console
console.log(`----- ${newValue ? 'Enabled' : 'Disabled'} Logging Messages -----`);
});
function ServerClientMessageLogging() {
const [shouldLog, setShouldLog] = useAtom(logMessagesState);
return (
<div>
<VSCodeCheckbox
checked={shouldLog}
onChange={e => setShouldLog((e.target as HTMLInputElement).checked)}>
<T>Log messages</T>
</VSCodeCheckbox>
</div>
);
}
function DebugPerfInfo() {
const latestStatus = useAtomValue(latestUncommittedChangesData);
const latestLog = useAtomValue(latestCommitsData);
const latestConflicts = useAtomValue(mergeConflicts);
const heartbeat = useHeartbeat();
const repoInfo = useAtomValue(repositoryInfo);
let commandName = 'sl';
if (repoInfo?.type === 'success') {
commandName = repoInfo.command;
}
return (
<div>
{heartbeat.type === 'timeout' ? (
<InlineErrorBadge error={new Error(t('Heartbeat timeout'))}>
<T>Heartbeat timed out</T>
</InlineErrorBadge>
) : (
<FetchDurationInfo
name={<T>ISL Server Ping</T>}
duration={(heartbeat as Heartbeat & {type: 'success'})?.rtt}
/>
)}
<FetchDurationInfo
name={<T replace={{$cmd: commandName}}>$cmd status</T>}
start={latestStatus.fetchStartTimestamp}
end={latestStatus.fetchCompletedTimestamp}
/>
<FetchDurationInfo
name={<T replace={{$cmd: commandName}}>$cmd log</T>}
start={latestLog.fetchStartTimestamp}
end={latestLog.fetchCompletedTimestamp}
/>
<FetchDurationInfo
name={<T>Merge Conflicts</T>}
start={latestConflicts?.fetchStartTimestamp}
end={latestConflicts?.fetchCompletedTimestamp}
/>
</div>
);
}
function FetchDurationInfo(
props: {name: ReactNode} & ExclusiveOr<{start?: number; end?: number}, {duration: number}>,
) {
const {name} = props;
const {end, start, duration} = props;
const deltaMs = duration != null ? duration : end == null || start == null ? null : end - start;
const assessment =
deltaMs == null ? 'none' : deltaMs < 1000 ? 'fast' : deltaMs < 3000 ? 'ok' : 'slow';
return (
<div className={`fetch-duration-info fetch-duration-${assessment}`}>
{name} <VSCodeBadge>{deltaMs == null ? 'N/A' : `${deltaMs}ms`}</VSCodeBadge>
{end == null ? null : (
<Subtle>
<Tooltip title={new Date(end).toLocaleString()} placement="right">
<RelativeDate date={end} />
</Tooltip>
</Subtle>
)}
</div>
);
}
function useMeasureDuration(slowOperation: () => void): number | null {
const [measured, setMeasured] = useState<null | number>(null);
useEffect(() => {
requestIdleCallback(() => {
const startTime = performance.now();
slowOperation();
const endTime = performance.now();
setMeasured(endTime - startTime);
});
return () => setMeasured(null);
}, [slowOperation]);
return measured;
}
function DebugDagInfo() {
const dag = useAtomValue(dagWithPreviews);
const dagRenderBenchmark = useCallback(() => {
// Slightly change the dag to invalidate its caches.
const noise = performance.now();
const newDag = dag.add([DagCommitInfo.fromCommitInfo({hash: `dummy-${noise}`, parents: []})]);
newDag.renderToRows(newDag.subsetForRendering());
}, [dag]);
const dagSize = dag.all().size;
const dagDisplayedSize = dag.subsetForRendering().size;
const dagSortMs = useMeasureDuration(dagRenderBenchmark);
return (
<div>
<T>Size: </T>
{dagSize}
<br />
<T>Displayed: </T>
{dagDisplayedSize}
<br />
<>
<T>Render calculation: </T>
{dagSortMs == null ? (
<T>(Measuring)</T>
) : (
<>
{dagSortMs.toFixed(1)} <T>ms</T>
</>
)}
<br />
</>
</div>
);
}