forked from y-scope/yscope-log-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsDialog.tsx
179 lines (166 loc) · 5.85 KB
/
SettingsDialog.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
import React, {
forwardRef,
useCallback,
useContext,
} from "react";
import {
Button,
DialogActions,
DialogContent,
DialogTitle,
FormControl,
FormHelperText,
FormLabel,
Input,
ModalDialog,
} from "@mui/joy";
import {NotificationContext} from "../../../contexts/NotificationContextProvider";
import {Nullable} from "../../../typings/common";
import {
CONFIG_KEY,
LOCAL_STORAGE_KEY,
} from "../../../typings/config";
import {LOG_LEVEL} from "../../../typings/logs";
import {DO_NOT_TIMEOUT_VALUE} from "../../../typings/notifications";
import {
getConfig,
setConfig,
} from "../../../utils/config";
import ThemeSwitchToggle from "./ThemeSwitchToggle";
const CONFIG_FORM_FIELDS = [
{
helperText: `[JSON] Log message conversion pattern: use field placeholders to insert
values from JSON log events. The syntax is
\`{<field-name>[:<formatter-name>[:<formatter-options>]]}\`, where \`field-name\` is
required, while \`formatter-name\` and \`formatter-options\` are optional. For example,
the following placeholder would format a timestamp field with name \`@timestamp\`:
\`{@timestamp:timestamp:YYYY-MM-DD HH\\:mm\\:ss.SSS}\`. The default setting is
an empty string which will print all fields formatted as JSON.`,
initialValue: getConfig(CONFIG_KEY.DECODER_OPTIONS).formatString,
label: "Decoder: Format string",
name: LOCAL_STORAGE_KEY.DECODER_OPTIONS_FORMAT_STRING,
type: "text",
},
{
helperText: "[JSON] Key to extract the log level from.",
initialValue: getConfig(CONFIG_KEY.DECODER_OPTIONS).logLevelKey,
label: "Decoder: Log level key",
name: LOCAL_STORAGE_KEY.DECODER_OPTIONS_LOG_LEVEL_KEY,
type: "text",
},
{
helperText: "[JSON] Key to extract the log timestamp from.",
initialValue: getConfig(CONFIG_KEY.DECODER_OPTIONS).timestampKey,
label: "Decoder: Timestamp key",
name: LOCAL_STORAGE_KEY.DECODER_OPTIONS_TIMESTAMP_KEY,
type: "text",
},
{
helperText: "Number of log messages to display per page.",
initialValue: getConfig(CONFIG_KEY.PAGE_SIZE),
label: "View: Page size",
name: LOCAL_STORAGE_KEY.PAGE_SIZE,
type: "number",
},
];
/**
* Handles the reset event for the configuration form.
*
* @param ev
*/
const handleConfigFormReset = (ev: React.FormEvent) => {
ev.preventDefault();
window.localStorage.clear();
window.location.reload();
};
/**
* Renders a settings dialog for configurations.
*
* @return
*/
const SettingsDialog = forwardRef<HTMLFormElement>((_, ref) => {
const {postPopUp} = useContext(NotificationContext);
const handleConfigFormSubmit = useCallback((ev: React.FormEvent) => {
ev.preventDefault();
const formData = new FormData(ev.target as HTMLFormElement);
const getFormDataValue = (key: string) => formData.get(key) as string;
const formatString = getFormDataValue(LOCAL_STORAGE_KEY.DECODER_OPTIONS_FORMAT_STRING);
const logLevelKey = getFormDataValue(LOCAL_STORAGE_KEY.DECODER_OPTIONS_LOG_LEVEL_KEY);
const timestampKey = getFormDataValue(LOCAL_STORAGE_KEY.DECODER_OPTIONS_TIMESTAMP_KEY);
const pageSize = getFormDataValue(LOCAL_STORAGE_KEY.PAGE_SIZE);
let error: Nullable<string> = null;
error ||= setConfig({
key: CONFIG_KEY.DECODER_OPTIONS,
value: {formatString, logLevelKey, timestampKey},
});
error ||= setConfig({
key: CONFIG_KEY.PAGE_SIZE,
value: Number(pageSize),
});
if (null !== error) {
postPopUp({
level: LOG_LEVEL.ERROR,
message: error,
timeoutMillis: DO_NOT_TIMEOUT_VALUE,
title: "Unable to apply config.",
});
} else {
window.location.reload();
}
}, [postPopUp]);
return (
<form
ref={ref}
tabIndex={-1}
onReset={handleConfigFormReset}
onSubmit={handleConfigFormSubmit}
>
<ModalDialog
minWidth={"md"}
size={"lg"}
>
<DialogTitle className={"settings-dialog-title"}>
<span className={"settings-dialog-title-text"}>
Settings
</span>
<ThemeSwitchToggle/>
</DialogTitle>
<DialogContent>
{CONFIG_FORM_FIELDS.map((field, index) => (
<FormControl
className={"config-form-control"}
key={index}
>
<FormLabel>
{field.label}
</FormLabel>
<Input
defaultValue={field.initialValue}
name={field.name}
type={field.type}/>
<FormHelperText>
{field.helperText}
</FormHelperText>
</FormControl>
))}
</DialogContent>
<DialogActions>
<Button
color={"primary"}
type={"submit"}
>
Apply & Reload
</Button>
<Button
color={"neutral"}
type={"reset"}
>
Reset Default
</Button>
</DialogActions>
</ModalDialog>
</form>
);
});
SettingsDialog.displayName = "SettingsDialog";
export default SettingsDialog;