-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathSubComponents.tsx
More file actions
299 lines (273 loc) · 7.27 KB
/
SubComponents.tsx
File metadata and controls
299 lines (273 loc) · 7.27 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import React from 'react';
import { Box, Text } from 'ink';
import TextInput from 'ink-text-input';
import Spinner from 'ink-spinner';
import { parameterizedQueryToSql } from '@hyperdx/common-utils/dist/clickhouse';
import ErrorDisplay from '@/components/ErrorDisplay';
import type { TimeRange } from '@/utils/editor';
import type { Column, SwitchItem } from './types';
import { formatShortDate } from './utils';
// ---- Header --------------------------------------------------------
type HeaderProps = {
sourceName: string;
dbName: string;
tableName: string;
isFollowing: boolean;
loading: boolean;
timeRange: TimeRange;
};
export const Header = React.memo(function Header({
sourceName,
dbName,
tableName,
isFollowing,
loading,
timeRange,
}: HeaderProps) {
return (
<Box>
<Text bold color="cyan">
HyperDX
</Text>
<Text> — </Text>
<Text color="green">{sourceName}</Text>
<Text dimColor>
{' '}
({dbName}.{tableName})
</Text>
<Text dimColor>
{' '}
{formatShortDate(timeRange.start)} → {formatShortDate(timeRange.end)}
</Text>
{isFollowing && <Text color="yellow"> [FOLLOWING]</Text>}
{loading && (
<Text>
{' '}
<Spinner type="dots" />
</Text>
)}
</Box>
);
});
// ---- TabBar --------------------------------------------------------
type TabBarProps = {
items: SwitchItem[];
activeIdx: number;
};
export const TabBar = React.memo(function TabBar({
items,
activeIdx,
}: TabBarProps) {
if (items.length <= 1) return null;
return (
<Box height={1} overflowX="hidden" overflowY="hidden">
{items.map((item, i) => (
<Box key={`${item.type}-${i}`} marginRight={2}>
<Text
color={i === activeIdx ? 'cyan' : undefined}
bold={i === activeIdx}
dimColor={i !== activeIdx}
wrap="truncate"
>
{i === activeIdx ? '▸ ' : ' '}
{item.label}
</Text>
</Box>
))}
</Box>
);
});
// ---- SearchBar -----------------------------------------------------
type SearchBarProps = {
focused: boolean;
query: string;
onChange: (v: string) => void;
onSubmit: () => void;
};
export const SearchBar = React.memo(function SearchBar({
focused,
query,
onChange,
onSubmit,
}: SearchBarProps) {
return (
<Box>
<Text color={focused ? 'cyan' : 'gray'}>Search: </Text>
{focused ? (
<TextInput
value={query}
onChange={onChange}
onSubmit={onSubmit}
placeholder="Lucene query…"
/>
) : (
<Text dimColor>{query || '(empty)'}</Text>
)}
</Box>
);
});
// ---- Footer --------------------------------------------------------
type FooterProps = {
rowCount: number;
cursorPos: number;
wrapLines: boolean;
isFollowing: boolean;
loadingMore: boolean;
paginationError?: Error | null;
scrollInfo?: string;
};
export const Footer = React.memo(function Footer({
rowCount,
cursorPos,
wrapLines,
isFollowing,
loadingMore,
paginationError,
scrollInfo,
}: FooterProps) {
return (
<Box flexDirection="column" marginTop={1}>
{paginationError && (
<ErrorDisplay
error={paginationError}
severity="warning"
detail="Failed to load more results."
compact
/>
)}
<Box justifyContent="space-between">
<Text dimColor>
{isFollowing ? '[FOLLOWING] ' : ''}
{wrapLines ? '[WRAP] ' : ''}
{loadingMore ? '[LOADING…] ' : ''}?=help q=quit
</Text>
<Text dimColor>
{scrollInfo ? `${scrollInfo} ` : ''}
{cursorPos}/{rowCount}
</Text>
</Box>
</Box>
);
});
// ---- HelpScreen ----------------------------------------------------
export const HelpScreen = React.memo(function HelpScreen() {
const keys: Array<[string, string]> = [
['j / ↓', 'Move selection down'],
['k / ↑', 'Move selection up'],
['l / Enter', 'Expand row detail (SELECT *)'],
['h / Esc', 'Close row detail'],
['Tab (detail)', 'Switch Column Values / Trace'],
['G', 'Jump to last item'],
['g', 'Jump to first item'],
['Ctrl+D', 'Page down (half page)'],
['Ctrl+U', 'Page up (half page)'],
['/', 'Search (global or detail filter)'],
['Esc', 'Blur search bar'],
['Tab', 'Next source / saved search'],
['Shift+Tab', 'Previous source / saved search'],
['t', 'Edit time range in $EDITOR'],
['s', 'Edit select clause in $EDITOR'],
['D', 'Show generated SQL'],
['f', 'Toggle follow mode (live tail)'],
['w', 'Toggle line wrap'],
['Ctrl+K', 'Open spotlight (quick navigation)'],
['A (Shift+A)', 'Open alerts page'],
['?', 'Toggle this help'],
['q', 'Quit'],
];
return (
<Box flexDirection="column" paddingX={1} paddingY={1}>
<Text bold color="cyan">
Keybindings
</Text>
<Text> </Text>
{keys.map(([key, desc]) => (
<Box key={key}>
<Box width={20}>
<Text bold color="yellow">
{key}
</Text>
</Box>
<Text>{desc}</Text>
</Box>
))}
<Text> </Text>
<Text dimColor>Press ? or Esc to close</Text>
</Box>
);
});
// ---- TableHeader ---------------------------------------------------
type TableHeaderProps = {
columns: Column[];
};
export const TableHeader = React.memo(function TableHeader({
columns,
}: TableHeaderProps) {
return (
<Box overflowX="hidden">
{columns.map((col, i) => (
<Box key={i} width={col.width} overflowX="hidden">
<Text bold dimColor wrap="truncate">
{col.header}
</Text>
</Box>
))}
</Box>
);
});
// ---- SqlPreviewScreen ----------------------------------------------
type SqlPreviewScreenProps = {
chSql: { sql: string; params: Record<string, unknown> } | null;
scrollOffset: number;
maxRows: number;
};
export const SqlPreviewScreen = React.memo(function SqlPreviewScreen({
chSql,
scrollOffset,
maxRows,
}: SqlPreviewScreenProps) {
let resolvedSql = '';
if (chSql) {
try {
resolvedSql = parameterizedQueryToSql({
sql: chSql.sql,
params: chSql.params,
});
} catch {
// Fall back to the raw parameterized SQL if resolution fails
resolvedSql = chSql.sql;
}
}
const lines = resolvedSql ? resolvedSql.split('\n') : [];
const visibleLines = lines.slice(scrollOffset, scrollOffset + maxRows);
const totalLines = lines.length;
return (
<Box flexDirection="column" paddingX={1} paddingY={1}>
<Text bold color="cyan">
Generated SQL
</Text>
<Text> </Text>
{resolvedSql ? (
visibleLines.map((line, i) => (
<Text key={i} wrap="wrap">
{line}
</Text>
))
) : (
<Text dimColor>No query has been executed yet.</Text>
)}
{totalLines > maxRows && (
<>
<Text> </Text>
<Text dimColor>
Lines {scrollOffset + 1}–
{Math.min(scrollOffset + maxRows, totalLines)} of {totalLines}{' '}
(Ctrl+D/U to scroll)
</Text>
</>
)}
<Text> </Text>
<Text dimColor>Press D or Esc to close</Text>
</Box>
);
});