Skip to content

Commit 538d4be

Browse files
authored
use makeIconClass for widgets, add 'brands' support (#920)
1 parent 9173db4 commit 538d4be

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

frontend/app/workspace/workspace.tsx

+1-15
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,6 @@ async function handleWidgetSelect(blockDef: BlockDef) {
8080
createBlock(blockDef);
8181
}
8282

83-
function isIconValid(icon: string): boolean {
84-
if (util.isBlank(icon)) {
85-
return false;
86-
}
87-
return icon.match(iconRegex) != null;
88-
}
89-
90-
function getIconClass(icon: string): string {
91-
if (!isIconValid(icon)) {
92-
return "fa fa-regular fa-browser fa-fw";
93-
}
94-
return `fa fa-solid fa-${icon} fa-fw`;
95-
}
96-
9783
const Widget = React.memo(({ widget }: { widget: WidgetConfigType }) => {
9884
return (
9985
<div
@@ -102,7 +88,7 @@ const Widget = React.memo(({ widget }: { widget: WidgetConfigType }) => {
10288
title={widget.description || widget.label}
10389
>
10490
<div className="widget-icon" style={{ color: widget.color }}>
105-
<i className={getIconClass(widget.icon)}></i>
91+
<i className={util.makeIconClass(widget.icon, true, { defaultIcon: "browser" })}></i>
10692
</div>
10793
{!util.isBlank(widget.label) ? <div className="widget-label">{widget.label}</div> : null}
10894
</div>

frontend/util/util.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ function jsonDeepEqual(v1: any, v2: any): boolean {
8181
return false;
8282
}
8383

84-
function makeIconClass(icon: string, fw: boolean, opts?: { spin: boolean }): string {
85-
if (icon == null) {
84+
function makeIconClass(icon: string, fw: boolean, opts?: { spin?: boolean; defaultIcon?: string }): string {
85+
if (isBlank(icon)) {
86+
if (opts?.defaultIcon != null) {
87+
return makeIconClass(opts.defaultIcon, fw, { spin: opts?.spin });
88+
}
8689
return null;
8790
}
8891
if (icon.match(/^(solid@)?[a-z0-9-]+$/)) {
@@ -95,6 +98,14 @@ function makeIconClass(icon: string, fw: boolean, opts?: { spin: boolean }): str
9598
icon = icon.replace(/^regular@/, "");
9699
return clsx(`fa fa-sharp fa-regular fa-${icon}`, fw ? "fa-fw" : null, opts?.spin ? "fa-spin" : null);
97100
}
101+
if (icon.match(/^brands@[a-z0-9-]+$/)) {
102+
// strip off the "brands@" prefix if it exists
103+
icon = icon.replace(/^brands@/, "");
104+
return clsx(`fa fa-brands fa-${icon}`, fw ? "fa-fw" : null, opts?.spin ? "fa-spin" : null);
105+
}
106+
if (opts?.defaultIcon != null) {
107+
return makeIconClass(opts.defaultIcon, fw, { spin: opts?.spin });
108+
}
98109
return null;
99110
}
100111

0 commit comments

Comments
 (0)