Skip to content

use makeIconClass for widgets, add 'brands' support #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions frontend/app/workspace/workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,6 @@ async function handleWidgetSelect(blockDef: BlockDef) {
createBlock(blockDef);
}

function isIconValid(icon: string): boolean {
if (util.isBlank(icon)) {
return false;
}
return icon.match(iconRegex) != null;
}

function getIconClass(icon: string): string {
if (!isIconValid(icon)) {
return "fa fa-regular fa-browser fa-fw";
}
return `fa fa-solid fa-${icon} fa-fw`;
}

const Widget = React.memo(({ widget }: { widget: WidgetConfigType }) => {
return (
<div
Expand All @@ -102,7 +88,7 @@ const Widget = React.memo(({ widget }: { widget: WidgetConfigType }) => {
title={widget.description || widget.label}
>
<div className="widget-icon" style={{ color: widget.color }}>
<i className={getIconClass(widget.icon)}></i>
<i className={util.makeIconClass(widget.icon, true, { defaultIcon: "browser" })}></i>
</div>
{!util.isBlank(widget.label) ? <div className="widget-label">{widget.label}</div> : null}
</div>
Expand Down
15 changes: 13 additions & 2 deletions frontend/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ function jsonDeepEqual(v1: any, v2: any): boolean {
return false;
}

function makeIconClass(icon: string, fw: boolean, opts?: { spin: boolean }): string {
if (icon == null) {
function makeIconClass(icon: string, fw: boolean, opts?: { spin?: boolean; defaultIcon?: string }): string {
if (isBlank(icon)) {
if (opts?.defaultIcon != null) {
return makeIconClass(opts.defaultIcon, fw, { spin: opts?.spin });
}
return null;
}
if (icon.match(/^(solid@)?[a-z0-9-]+$/)) {
Expand All @@ -95,6 +98,14 @@ function makeIconClass(icon: string, fw: boolean, opts?: { spin: boolean }): str
icon = icon.replace(/^regular@/, "");
return clsx(`fa fa-sharp fa-regular fa-${icon}`, fw ? "fa-fw" : null, opts?.spin ? "fa-spin" : null);
}
if (icon.match(/^brands@[a-z0-9-]+$/)) {
// strip off the "brands@" prefix if it exists
icon = icon.replace(/^brands@/, "");
return clsx(`fa fa-brands fa-${icon}`, fw ? "fa-fw" : null, opts?.spin ? "fa-spin" : null);
}
if (opts?.defaultIcon != null) {
return makeIconClass(opts.defaultIcon, fw, { spin: opts?.spin });
}
return null;
}

Expand Down