-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
MultiTabLogViewer.tsx
267 lines (244 loc) · 6.93 KB
/
MultiTabLogViewer.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
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
import { Box, IconButton, Tab, Tabs, Typography } from "@mui/material";
import createStyles from "@mui/styles/createStyles";
import makeStyles from "@mui/styles/makeStyles";
import React, { useEffect, useState } from "react";
import { RiExternalLinkLine, RiSortAsc, RiSortDesc } from "react-icons/ri";
import { Link } from "react-router-dom";
import { useLocalStorage } from "usehooks-ts";
import { useStateApiLogs } from "../pages/log/hooks";
import { LogViewer } from "../pages/log/LogViewer";
import { HideableBlock } from "./CollapsibleSection";
import { ClassNameProps } from "./props";
const useStyles = makeStyles((theme) =>
createStyles({
tabs: {
borderBottom: `1px solid ${theme.palette.divider}`,
},
}),
);
export type MultiTabLogViewerTabDetails = {
title: string;
} & LogViewerData;
export type MultiTabLogViewerProps = {
tabs: MultiTabLogViewerTabDetails[];
otherLogsLink?: string;
/**
* If set, this multi-tab log viewer will remember the last selected tab and start on that tab
* the next time this component is rendered.
*
* Different string values to provide different contexts for this memory. For example, if you
* want all multi-tab log viewers in the actor detail page to share one memory, they should have
* the same string value here.
*/
contextKey?: string;
} & ClassNameProps;
export const MultiTabLogViewer = ({
tabs,
otherLogsLink,
contextKey,
className,
}: MultiTabLogViewerProps) => {
const classes = useStyles();
// DO NOT use `cachedTab` or `setCachedTab` when `contextKey` is undefined!
const [cachedTab, setCachedTab] = useLocalStorage(
`MultiTabLogViewer-tabMemory-${contextKey}`,
null,
);
const [value, setValue] = useState(
contextKey && cachedTab ? cachedTab : tabs[0]?.title,
);
const [expanded, setExpanded] = useState(false);
useEffect(() => {
// If current tab value is not valid, reset to first tab.
if (!tabs.some((tab) => tab.title === value)) {
setValue(tabs[0]?.title);
}
}, [tabs, value]);
const currentTab = tabs.find((tab) => tab.title === value);
if (tabs.length === 0) {
return <Typography>No logs to display.</Typography>;
}
return (
<div className={className}>
<Box
display="flex"
flexDirection="row"
alignItems="flex-start"
justifyContent="space-between"
>
<Box
display="flex"
flexDirection="column"
alignItems="stretch"
flexGrow={1}
>
{(tabs.length > 1 || otherLogsLink) && (
<Tabs
className={classes.tabs}
value={value}
onChange={(_, newValue) => {
if (contextKey) {
setCachedTab(newValue);
}
setValue(newValue);
}}
indicatorColor="primary"
>
{tabs.map(({ title }) => (
<Tab key={title} label={title} value={title} />
))}
{otherLogsLink && (
<Tab
label={
<Box display="flex" alignItems="center">
Other logs <RiExternalLinkLine size={20} />
</Box>
}
onClick={(event) => {
// Prevent the tab from changing by setting value to the current value
setValue(value);
}}
component={Link}
to={otherLogsLink}
target="_blank"
rel="noopener noreferrer"
/>
)}
</Tabs>
)}
{!currentTab ? (
<Typography color="error">Please select a tab.</Typography>
) : (
tabs.map((tab) => {
const { title, ...data } = tab;
return (
<HideableBlock
key={title}
visible={title === currentTab?.title}
keepRendered
>
<StateApiLogViewer
data={data}
height={expanded ? 800 : 300}
/>
</HideableBlock>
);
})
)}
</Box>
<IconButton
onClick={() => {
setExpanded(!expanded);
}}
size="large"
>
{expanded ? <RiSortAsc /> : <RiSortDesc />}
</IconButton>
</Box>
</div>
);
};
type TextData = {
contents: string;
};
type FileData = {
nodeId: string | null;
filename?: string;
};
type ActorData = {
actorId: string | null;
suffix: "out" | "err";
};
type TaskData = {
taskId: string | null;
suffix: "out" | "err";
};
type LogViewerData = TextData | FileData | ActorData | TaskData;
const isLogViewerDataText = (data: LogViewerData): data is TextData =>
"contents" in data;
const isLogViewerDataActor = (data: LogViewerData): data is ActorData =>
"actorId" in data;
const isLogViewerDataTask = (data: LogViewerData): data is TaskData =>
"taskId" in data;
export type StateApiLogViewerProps = {
height?: number;
data: LogViewerData;
};
export const StateApiLogViewer = ({
height = 300,
data,
}: StateApiLogViewerProps) => {
if (isLogViewerDataText(data)) {
return <TextLogViewer height={height} contents={data.contents} />;
} else if (isLogViewerDataActor(data)) {
return <ActorLogViewer height={height} {...data} />;
} else if (isLogViewerDataTask(data)) {
return <TaskLogViewer height={height} {...data} />;
} else {
return <FileLogViewer height={height} {...data} />;
}
};
const TextLogViewer = ({
height = 300,
contents,
}: {
height: number;
contents: string;
}) => {
return <LogViewer log={contents} height={height} />;
};
const FileLogViewer = ({
height = 300,
nodeId,
filename,
}: {
height: number;
} & FileData) => {
const apiData = useStateApiLogs({ nodeId, filename }, filename);
return <ApiLogViewer apiData={apiData} height={height} />;
};
const ActorLogViewer = ({
height = 300,
actorId,
suffix,
}: {
height: number;
} & ActorData) => {
const apiData = useStateApiLogs(
{ actorId, suffix },
`actor-log-${actorId}.${suffix}`,
);
return <ApiLogViewer apiData={apiData} height={height} />;
};
const TaskLogViewer = ({
height = 300,
taskId,
suffix,
}: {
height: number;
} & TaskData) => {
const apiData = useStateApiLogs(
{ taskId, suffix },
`task-log-${taskId}.${suffix}`,
);
return <ApiLogViewer apiData={apiData} height={height} />;
};
const ApiLogViewer = ({
apiData: { downloadUrl, log, path, refresh },
height = 300,
}: {
apiData: ReturnType<typeof useStateApiLogs>;
height: number;
}) => {
return typeof log === "string" ? (
<LogViewer
log={log}
path={path}
downloadUrl={downloadUrl !== null ? downloadUrl : undefined}
height={height}
onRefreshClick={refresh}
/>
) : (
<Typography color="error">Failed to load</Typography>
);
};