2023-05-01 14:06:05 -04:00
|
|
|
import { useContext } from "react";
|
2022-11-04 17:38:33 -04:00
|
|
|
import Image from "next/future/image";
|
|
|
|
|
2023-05-01 14:06:05 -04:00
|
|
|
import { SettingsContext } from "utils/contexts/settings";
|
|
|
|
import { ThemeContext } from "utils/contexts/theme";
|
|
|
|
|
|
|
|
const iconSetURLs = {
|
2023-10-17 23:26:55 -07:00
|
|
|
mdi: "https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/",
|
|
|
|
si: "https://cdn.jsdelivr.net/npm/simple-icons@latest/icons/",
|
2023-05-01 14:06:05 -04:00
|
|
|
};
|
|
|
|
|
2023-01-13 22:45:08 +01:00
|
|
|
export default function ResolvedIcon({ icon, width = 32, height = 32, alt = "logo" }) {
|
2023-05-01 14:06:05 -04:00
|
|
|
const { settings } = useContext(SettingsContext);
|
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
|
2022-11-04 17:38:33 -04:00
|
|
|
// direct or relative URLs
|
|
|
|
if (icon.startsWith("http") || icon.startsWith("/")) {
|
2023-04-23 16:12:08 +00:00
|
|
|
return (
|
|
|
|
<Image
|
|
|
|
src={`${icon}`}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
style={{
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
objectFit: "contain",
|
2023-05-01 11:29:35 -07:00
|
|
|
maxHeight: "100%",
|
|
|
|
maxWidth: "100%",
|
2023-04-23 16:12:08 +00:00
|
|
|
}}
|
|
|
|
alt={alt}
|
|
|
|
/>
|
|
|
|
);
|
2022-11-04 17:38:33 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 14:06:05 -04:00
|
|
|
// check mdi- or si- prefixed icons
|
2023-07-26 02:31:42 +02:00
|
|
|
const prefix = icon.split("-")[0];
|
2023-05-01 14:06:05 -04:00
|
|
|
|
2024-09-11 15:13:36 +03:00
|
|
|
if (prefix === "sh") {
|
|
|
|
const iconName = icon.replace("sh-", "").replace(".svg", "").replace(".png", "").replace(".webp", "");
|
|
|
|
const extension = icon.endsWith(".svg") ? "svg" : icon.endsWith(".webp") ? "webp" : "png";
|
|
|
|
return (
|
|
|
|
<Image
|
|
|
|
src={`https://cdn.jsdelivr.net/gh/selfhst/icons@main/${extension}/${iconName}.${extension}`}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
style={{
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
objectFit: "contain",
|
|
|
|
maxHeight: "100%",
|
|
|
|
maxWidth: "100%",
|
|
|
|
}}
|
|
|
|
alt={alt}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-01 14:06:05 -04:00
|
|
|
if (prefix in iconSetURLs) {
|
2023-07-26 02:31:42 +02:00
|
|
|
// default to theme setting
|
|
|
|
let iconName = icon.replace(`${prefix}-`, "").replace(".svg", "");
|
2023-10-17 23:26:55 -07:00
|
|
|
let iconColor =
|
|
|
|
settings.iconStyle === "theme"
|
|
|
|
? `rgb(var(--color-${theme === "dark" ? 300 : 900}) / var(--tw-text-opacity, 1))`
|
|
|
|
: "linear-gradient(180deg, rgb(var(--color-logo-start)), rgb(var(--color-logo-stop)))";
|
2023-07-26 02:31:42 +02:00
|
|
|
|
|
|
|
// use custom hex color if provided
|
2023-10-17 23:26:55 -07:00
|
|
|
const colorMatches = icon.match(/[#][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]$/i);
|
2023-07-26 02:31:42 +02:00
|
|
|
if (colorMatches?.length) {
|
|
|
|
iconName = icon.replace(`${prefix}-`, "").replace(".svg", "").replace(`-${colorMatches[0]}`, "");
|
|
|
|
iconColor = `${colorMatches[0]}`;
|
|
|
|
}
|
|
|
|
|
2023-05-01 14:06:05 -04:00
|
|
|
const iconSource = `${iconSetURLs[prefix]}${iconName}.svg`;
|
|
|
|
|
2022-11-04 17:38:33 -04:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{
|
2022-12-11 10:30:04 -08:00
|
|
|
width,
|
|
|
|
height,
|
2023-10-17 23:26:55 -07:00
|
|
|
maxWidth: "100%",
|
|
|
|
maxHeight: "100%",
|
2023-07-26 02:31:42 +02:00
|
|
|
background: `${iconColor}`,
|
2023-05-01 14:06:05 -04:00
|
|
|
mask: `url(${iconSource}) no-repeat center / contain`,
|
|
|
|
WebkitMask: `url(${iconSource}) no-repeat center / contain`,
|
2022-11-04 17:38:33 -04:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-10-17 23:26:55 -07:00
|
|
|
|
2022-11-04 17:38:33 -04:00
|
|
|
// fallback to dashboard-icons
|
2023-05-09 23:29:55 +02:00
|
|
|
if (icon.endsWith(".svg")) {
|
|
|
|
const iconName = icon.replace(".svg", "");
|
|
|
|
return (
|
|
|
|
<Image
|
|
|
|
src={`https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/svg/${iconName}.svg`}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
style={{
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
objectFit: "contain",
|
|
|
|
maxHeight: "100%",
|
2023-10-17 23:26:55 -07:00
|
|
|
maxWidth: "100%",
|
2023-05-09 23:29:55 +02:00
|
|
|
}}
|
|
|
|
alt={alt}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-10-17 23:26:55 -07:00
|
|
|
|
2022-11-04 17:38:33 -04:00
|
|
|
const iconName = icon.replace(".png", "");
|
|
|
|
return (
|
|
|
|
<Image
|
|
|
|
src={`https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${iconName}.png`}
|
2022-12-11 10:30:04 -08:00
|
|
|
width={width}
|
|
|
|
height={height}
|
2023-04-23 16:12:08 +00:00
|
|
|
style={{
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
objectFit: "contain",
|
2023-05-01 11:29:35 -07:00
|
|
|
maxHeight: "100%",
|
2023-10-17 23:26:55 -07:00
|
|
|
maxWidth: "100%",
|
2023-04-23 16:12:08 +00:00
|
|
|
}}
|
2023-01-13 22:45:08 +01:00
|
|
|
alt={alt}
|
2022-11-04 17:38:33 -04:00
|
|
|
/>
|
|
|
|
);
|
2023-01-13 22:45:08 +01:00
|
|
|
}
|