Started working on added task icons to the application.
This commit is contained in:
parent
7fd8440ca6
commit
2478d81ee4
105
src/components/common/FontAwesomeStringIcon.tsx
Normal file
105
src/components/common/FontAwesomeStringIcon.tsx
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
import React from "react";
|
||||||
|
import {
|
||||||
|
library,
|
||||||
|
type IconName,
|
||||||
|
type IconPrefix,
|
||||||
|
type IconProp,
|
||||||
|
} from "@fortawesome/fontawesome-svg-core";
|
||||||
|
import { fas } from "@fortawesome/free-solid-svg-icons";
|
||||||
|
import { fad } from "@fortawesome/pro-duotone-svg-icons";
|
||||||
|
import { fal } from "@fortawesome/pro-light-svg-icons";
|
||||||
|
import { far } from "@fortawesome/pro-regular-svg-icons";
|
||||||
|
import { fat } from "@fortawesome/pro-thin-svg-icons";
|
||||||
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
|
|
||||||
|
type FontAwesomeStringIconProps = {
|
||||||
|
icon?: string;
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const iconPrefixByToken: Record<string, IconPrefix> = {
|
||||||
|
"fa-brands": "fab",
|
||||||
|
fab: "fab",
|
||||||
|
"fa-duotone": "fad",
|
||||||
|
fad: "fad",
|
||||||
|
"fa-light": "fal",
|
||||||
|
fal: "fal",
|
||||||
|
"fa-regular": "far",
|
||||||
|
far: "far",
|
||||||
|
"fa-solid": "fas",
|
||||||
|
fas: "fas",
|
||||||
|
"fa-thin": "fat",
|
||||||
|
fat: "fat",
|
||||||
|
};
|
||||||
|
|
||||||
|
let hasRegisteredIconPacks = false;
|
||||||
|
|
||||||
|
function ensureIconPacksRegistered(): void {
|
||||||
|
if (hasRegisteredIconPacks) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
library.add(fas, far, fal, fat, fad);
|
||||||
|
hasRegisteredIconPacks = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toKebabCase(value: string): string {
|
||||||
|
return value
|
||||||
|
.replace(/^fa/, "")
|
||||||
|
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
||||||
|
.replace(/_/g, "-")
|
||||||
|
.replace(/\s+/g, "-")
|
||||||
|
.replace(/^-+/, "")
|
||||||
|
.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseIcon(icon?: string): IconProp | undefined {
|
||||||
|
if (!icon) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tokens = icon.trim().split(/\s+/).filter(Boolean);
|
||||||
|
let prefix: IconPrefix = "fas";
|
||||||
|
let iconName = "";
|
||||||
|
|
||||||
|
tokens.forEach((token) => {
|
||||||
|
const mappedPrefix = iconPrefixByToken[token];
|
||||||
|
if (mappedPrefix) {
|
||||||
|
prefix = mappedPrefix;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token.startsWith("fa-")) {
|
||||||
|
iconName = token.slice(3);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!iconName) {
|
||||||
|
iconName = token;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!iconName) {
|
||||||
|
iconName = toKebabCase(icon);
|
||||||
|
} else if (!iconName.includes("-")) {
|
||||||
|
iconName = toKebabCase(iconName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return iconName ? [prefix, iconName as IconName] : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const FontAwesomeStringIcon: React.FC<FontAwesomeStringIconProps> = ({
|
||||||
|
icon,
|
||||||
|
className,
|
||||||
|
}) => {
|
||||||
|
const resolvedIcon = React.useMemo(() => parseIcon(icon), [icon]);
|
||||||
|
|
||||||
|
if (!resolvedIcon) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureIconPacksRegistered();
|
||||||
|
return <FontAwesomeIcon icon={resolvedIcon} className={className} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default FontAwesomeStringIcon;
|
||||||
@ -46,6 +46,7 @@ export interface CreateWorkflowTemplateVersion extends FormData {
|
|||||||
export interface TaskMetadata {
|
export interface TaskMetadata {
|
||||||
taskType: string;
|
taskType: string;
|
||||||
displayName: string;
|
displayName: string;
|
||||||
|
icon: string;
|
||||||
capabilities: string[];
|
capabilities: string[];
|
||||||
outcomeLabel: string;
|
outcomeLabel: string;
|
||||||
outcomes: string[];
|
outcomes: string[];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user