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-09-13 22:40:31 +02:00
|
|
|
CodexSdk.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 18:02:15 +02:00
|
|
|
if (import.meta.env.PROD) {
|
|
|
|
|
Sentry.captureException(error);
|
|
|
|
|
}
|
2024-08-30 17:26:27 +02:00
|
|
|
|
2024-08-30 11:47:16 +02:00
|
|
|
setToast({
|
2024-09-13 22:40:31 +02:00
|
|
|
message: "Error when trying to update: " + error.message,
|
2024-08-30 11:47:16 +02:00
|
|
|
time: Date.now(),
|
|
|
|
|
variant: "error",
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const [toast, setToast] = useState({
|
|
|
|
|
time: 0,
|
|
|
|
|
message: "",
|
2024-09-13 22:40:31 +02:00
|
|
|
variant: "success" as "success" | "error" | "default",
|
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-09-13 22:40:31 +02:00
|
|
|
<Toast
|
|
|
|
|
message={toast.message}
|
|
|
|
|
time={toast.time}
|
|
|
|
|
variant={toast.variant}
|
|
|
|
|
/>
|
2024-08-29 18:45:52 +02:00
|
|
|
</>
|
2024-08-22 17:41:44 +02:00
|
|
|
);
|
|
|
|
|
}
|