client/packages/shared/src/log.tsx
Alois 25c7ee7c26
Some checks failed
/ build-web (push) Failing after 4m33s
/ build-desktop (linux) (push) Failing after 4m36s
/ build-mobile (push) Failing after 7m7s
/ release (push) Has been skipped
(fix): actually migrate all the imports from tensamin/ui to methanium/ui
(feat): update legal loading screen
2026-07-25 18:12:54 +02:00

98 lines
1.9 KiB
TypeScript

import { toast as sonnerToast } from "@methanium/ui";
import { Ban, Check, Info, TriangleAlert } from "lucide-react";
/**
* Log Levels
* - 0: Always logged
* - 1: Errors and warnings
* - 2: Useful information
* - 3: Debug messages
*/
export function log(
logLevel: number,
logger: string,
color:
| "red"
| "green"
| "yellow"
| "purple"
| "blue"
| "cyan"
| "white"
| "black"
| "gray",
...args: unknown[]
) {
const currentLogLevel = Number(localStorage.getItem("log_level") || 0);
if (logLevel > currentLogLevel) return;
const resetCode = "\x1b[0m";
const colorCodes: Record<typeof color, string> = {
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
purple: "\x1b[35m",
blue: "\x1b[34m",
cyan: "\x1b[36m",
white: "\x1b[37m",
black: "\x1b[30m",
gray: "\x1b[90m",
};
if (args.length === 1) {
console.log(`${colorCodes[color]}${logger}${resetCode} ${args[0]}`);
return;
}
console.groupCollapsed(
`${colorCodes[color]}${logger}${resetCode} ${args[0]}`,
);
try {
for (const arg of args.slice(1)) {
console.log(arg);
}
} finally {
console.groupEnd();
}
}
const size = 20;
/**
* Executes toast.
* @param type Parameter type.
* @param message Parameter message.
* @returns unknown.
*/
export function toast(
type: "error" | "info" | "warn" | "success",
message: string,
description?: string,
) {
switch (type) {
case "error":
sonnerToast(message, {
icon: <Ban size={size} />,
description,
});
break;
case "info":
sonnerToast(message, {
icon: <Info size={size} />,
description,
});
break;
case "warn":
sonnerToast(message, {
icon: <TriangleAlert size={size} />,
description,
});
break;
case "success":
sonnerToast(message, {
icon: <Check size={size} />,
description,
});
break;
}
}