85 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-08-30 17:26:27 +02:00
import { CodexLogLevel } from "@codex-storage/sdk-js";
2024-08-22 17:41:44 +02:00
import { useMutation, useQueryClient } from "@tanstack/react-query";
2024-08-30 11:47:16 +02:00
import { useState } from "react";
2024-08-22 17:41:44 +02:00
import { CodexSdk } from "../../sdk/codex";
import "./LogLevel.css";
2024-08-30 17:26:27 +02:00
import {
Button,
Select,
Toast,
} from "@codex-storage/marketplace-ui-components";
2024-08-30 11:47:16 +02:00
import { Promises } from "../../utils/promises";
2024-08-30 17:26:27 +02:00
import * as Sentry from "@sentry/browser";
2024-08-22 17:41:44 +02:00
export function LogLevel() {
const queryClient = useQueryClient();
const [level, setLevel] = useState<CodexLogLevel>("DEBUG");
2024-08-30 11:47:16 +02:00
const { mutateAsync, isPending } = useMutation({
2024-08-22 17:41:44 +02:00
mutationKey: ["debug"],
mutationFn: (level: CodexLogLevel) =>
2024-08-30 11:47:16 +02:00
CodexSdk.debug()
.then((debug) => debug.setLogLevel(level))
.then((s) => Promises.rejectOnError(s)),
2024-08-22 17:41:44 +02:00
onSuccess: () => {
setToast({
message: "The log level has been updated successfully.",
time: Date.now(),
2024-08-30 11:47:16 +02:00
variant: "success",
2024-08-22 17:41:44 +02:00
});
queryClient.invalidateQueries({ queryKey: ["debug"] });
},
2024-08-30 11:47:16 +02:00
onError: (error) => {
2024-08-30 17:26:27 +02:00
Sentry.captureException(error);
2024-08-30 11:47:16 +02:00
setToast({
message: "Error when trying to update: " + error,
time: Date.now(),
variant: "error",
});
},
});
const [toast, setToast] = useState({
time: 0,
message: "",
variant: "success",
2024-08-22 17:41:44 +02:00
});
function onChange(e: React.FormEvent<HTMLSelectElement>) {
const value = e.currentTarget.value;
if (value) {
setLevel(value as CodexLogLevel);
}
}
const onClick = () => {
mutateAsync(level);
};
const levels = [
["DEBUG", "DEBUG"],
["TRACE", "TRACE"],
["INFO", "INFO"],
["NOTICE", "NOTICE"],
["WARN", "WARN"],
["ERROR", "ERROR"],
["FATAL", "FATAL"],
] satisfies [string, string][];
return (
2024-08-29 18:45:52 +02:00
<>
2024-08-22 17:41:44 +02:00
<Select
className="logLevel-select"
id="level"
label="Log level"
options={levels}
onChange={onChange}></Select>
<Button
variant="primary"
label="Save changes"
fetching={isPending}
onClick={onClick}></Button>
2024-08-30 11:47:16 +02:00
<Toast message={toast.message} time={toast.time} variant="success" />
2024-08-29 18:45:52 +02:00
</>
2024-08-22 17:41:44 +02:00
);
}