Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(notifications): add notification badge to favicon (#1115)

Aidan Holloway-Bidwell7e5e73f058fb
diff --git a/js/app/packages/app/component/ReactiveFavicon.tsx b/js/app/packages/app/component/ReactiveFavicon.tsx
index ec41043e90..4758a15985 100644
--- a/js/app/packages/app/component/ReactiveFavicon.tsx
+++ b/js/app/packages/app/component/ReactiveFavicon.tsx
@@ -1,12 +1,43 @@
import { updateFavicon } from '@app/util/favicon';
-import { createEffect } from 'solid-js';
-import { themeReactive } from '../../block-theme/signals/themeReactive';
+import { createEffect, createSignal, onCleanup, onMount } from 'solid-js';
+import { useReactiveColorString } from '../../block-theme/signals/themeReactive';
+import { useGlobalNotificationSource } from './GlobalAppState';
export function ReactiveFavicon() {
+ const [showNotificationBadge, setShowNotificationBadge] = createSignal(false);
+ const [isAppFocused, setIsAppFocused] = createSignal(!document.hidden);
+
+ const globalNotifications = useGlobalNotificationSource();
+
+ // Remove notification badge when app is focused
+ const handleVisibilityChange = () => {
+ setIsAppFocused(!document.hidden);
+ if (!document.hidden) {
+ setShowNotificationBadge(false);
+ }
+ };
+
+ onMount(() => {
+ document.addEventListener('visibilitychange', handleVisibilityChange);
+
+ const unsubscribe = globalNotifications.subscribe(() => {
+ if (!isAppFocused()) {
+ setShowNotificationBadge(true);
+ }
+ });
+
+ onCleanup(() => {
+ document.removeEventListener('visibilitychange', handleVisibilityChange);
+ unsubscribe();
+ });
+ });
+
+ const accentColor = useReactiveColorString('a0');
+ const badgeColor = useReactiveColorString('a1');
+
createEffect(() => {
- const { l, c, h } = themeReactive.a0;
- const col = `oklch(${l[0]()} ${c[0]()} ${h[0]()}deg)`;
- updateFavicon(col);
+ updateFavicon(accentColor(), badgeColor(), showNotificationBadge());
});
+
return null;
}
diff --git a/js/app/packages/app/component/Root.tsx b/js/app/packages/app/component/Root.tsx
index 383e5499c8..1e09a6c173 100644
--- a/js/app/packages/app/component/Root.tsx
+++ b/js/app/packages/app/component/Root.tsx
@@ -78,6 +78,7 @@ import { SuspenseContextComp } from './SuspenseContext';
import { LAYOUT_ROUTE } from './split-layout/SplitLayoutRoute';
import Visor from './Visor';
import { setOpenWhichKey, WhichKey } from './WhichKey';
+import { ReactiveFavicon } from './ReactiveFavicon';
const { track, identify, TrackingEvents } = withAnalytics();
@@ -402,6 +403,7 @@ export function Root() {
<UserInfoSideEffects />
<ConfiguredGlobalAppStateProvider>
<ChannelsContextProvider>
+ <ReactiveFavicon />
<Title>{tabTitle()}</Title>
<MacroJump />
<Visor />
diff --git a/js/app/packages/app/index.tsx b/js/app/packages/app/index.tsx
index a090c4f287..8026f88f9a 100644
--- a/js/app/packages/app/index.tsx
+++ b/js/app/packages/app/index.tsx
@@ -10,7 +10,6 @@ import { platformFetch } from '@core/util/platformFetch';
import * as Observability from '@observability';
import { ErrorBoundary, render } from 'solid-js/web';
import { FatalError } from './component/FatalError';
-import { ReactiveFavicon } from './component/ReactiveFavicon';
import { Root } from './component/Root';
// Override global fetch with platformFetch for Tauri compatibility
@@ -76,7 +75,6 @@ const renderApp = () => {
)}
>
<Root />
- <ReactiveFavicon />
</ErrorBoundary>
),
root
diff --git a/js/app/packages/app/types.d.ts b/js/app/packages/app/types.d.ts
index ae21d348cd..e049c9c605 100644
--- a/js/app/packages/app/types.d.ts
+++ b/js/app/packages/app/types.d.ts
@@ -2,4 +2,4 @@ declare module '*.svg' {
import type { Component, ComponentProps } from 'solid-js';
const c: Component<ComponentProps<'svg'>>;
export default c;
-}
\ No newline at end of file
+}
diff --git a/js/app/packages/app/util/favicon.ts b/js/app/packages/app/util/favicon.ts
index 32e1cfbecd..8ac9224c11 100644
--- a/js/app/packages/app/util/favicon.ts
+++ b/js/app/packages/app/util/favicon.ts
@@ -1,3 +1,8 @@
+import FaviconSvg from '@macro-icons/macro-macro.svg?raw';
+import FaviconBadgeSvg from '@macro-icons/macro-macro-badge.svg?raw';
+
+const FAVICON_SIZE = 48;
+
let currentFaviconLink: HTMLLinkElement | null = null;
/** escapes a color value for use in SVG */
@@ -5,39 +10,88 @@ function escapeColorForSvg(color: string): string {
return color.replace(/"/g, '&quot;').replace(/'/g, '&#39;');
}
-/** generates a favicon data URL for the given theme color */
-export function getFaviconUrl(themeColor: string): string {
- const safeColor = escapeColorForSvg(themeColor);
- const svg = `<svg width="24" height="24" fill="${safeColor}" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m6.25 4.038-2.242 0.8792v5.8184l-1.756-1.6582-2.242 0.8792v6.6766c0 0.2568 0.106 0.502 0.292 0.6784l2.794 2.6422 2.244-0.879v-5.8184l7.084 6.6974 2.244-0.879v-5.8184l7.086 6.6976 2.24-0.8792v-6.6766c0-0.2568-0.104-0.5022-0.292-0.6784l-8.124-7.6816-2.244 0.879v5.8184z"/></svg>`;
- return `data:image/svg+xml,${encodeURIComponent(svg)}`;
+/** insert color and url encode SVG */
+function processSvg(svg: string, color: string) {
+ return `data:image/svg+xml,${encodeURIComponent(svg.replace(/currentColor/g, escapeColorForSvg(color)))}`;
}
-/** updates the favicon with the current accent color */
-export function updateFavicon(themeColor: string): void {
- if (!themeColor || typeof themeColor !== 'string') {
- console.warn('Invalid theme color provided to updateFavicon:', themeColor);
- return;
- }
+/**
+ * Return a data url for the macro logo svg filled with the given color.
+ * @param color
+ * @returns
+ */
+export function getFaviconUrl(color: string) {
+ return processSvg(FaviconSvg, color);
+}
- if (currentFaviconLink && currentFaviconLink.parentNode) {
+/**
+ * Update the site's live favicon with a new color, and optionally a notification
+ * badge with its own color.
+ * @param color
+ * @returns
+ */
+export function updateFavicon(
+ faviconColor: string,
+ badgeColor?: string,
+ hasBadge?: boolean
+): void {
+ if (currentFaviconLink?.parentNode) {
currentFaviconLink.parentNode.removeChild(currentFaviconLink);
currentFaviconLink = null;
}
- const faviconUrl = getFaviconUrl(themeColor);
+ const canvas = document.createElement('canvas');
+ const ctx = canvas.getContext('2d');
+ if (!ctx) return;
- const link = document.createElement('link');
- link.rel = 'icon';
- link.type = 'image/svg+xml';
- link.href = faviconUrl;
+ canvas.width = FAVICON_SIZE;
+ canvas.height = FAVICON_SIZE;
- document.head.appendChild(link);
- currentFaviconLink = link;
+ const img = new Image();
+ img.src = processSvg(hasBadge ? FaviconBadgeSvg : FaviconSvg, faviconColor);
- const existingShortcutIcon = document.querySelector(
- 'link[rel="shortcut icon"]'
- ) as HTMLLinkElement;
- if (existingShortcutIcon) {
- existingShortcutIcon.href = faviconUrl;
- }
+ img.onload = () => {
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
+
+ if (hasBadge) {
+ const badgeRadius = 6;
+ ctx.beginPath();
+ ctx.arc(
+ canvas.width - badgeRadius,
+ badgeRadius,
+ badgeRadius,
+ 0,
+ 2 * Math.PI
+ );
+ ctx.fillStyle = badgeColor || faviconColor;
+ ctx.fill();
+ }
+
+ const faviconUrl = canvas.toDataURL();
+
+ if (currentFaviconLink?.parentNode) {
+ currentFaviconLink.parentNode.removeChild(currentFaviconLink);
+ }
+
+ const existingLinks = document.querySelectorAll('link[rel*="icon"]');
+ existingLinks.forEach((link) => {
+ link.remove();
+ });
+
+ // create and add new favicon
+ const link = document.createElement('link');
+ link.rel = 'icon';
+ link.type = 'image/png';
+ link.href = faviconUrl;
+ document.head.appendChild(link);
+ currentFaviconLink = link;
+
+ // update existing shortcut icon if present
+ const existingShortcutIcon = document.querySelector(
+ 'link[rel="shortcut icon"]'
+ ) as HTMLLinkElement;
+ if (existingShortcutIcon) {
+ existingShortcutIcon.href = faviconUrl;
+ }
+ };
}
diff --git a/js/app/packages/block-theme/signals/themeReactive.ts b/js/app/packages/block-theme/signals/themeReactive.ts
index 8026c9f6c1..0879488472 100644
--- a/js/app/packages/block-theme/signals/themeReactive.ts
+++ b/js/app/packages/block-theme/signals/themeReactive.ts
@@ -18,7 +18,7 @@ export const themeReactive: ThemeReactive = {
c2: {l: createSignal(0), c: createSignal(0), h: createSignal(0)},
c3: {l: createSignal(0), c: createSignal(0), h: createSignal(0)},
c4: {l: createSignal(0), c: createSignal(0), h: createSignal(0)},
-};
+} as const;
const previousTheme: ThemePrevious = {
a0: {l: Number.NaN, c: Number.NaN, h: Number.NaN},
@@ -131,3 +131,8 @@ createEffect(
export function createThemeEffect(cb: () => void) {
return createEffect(on(ALL_THEME_SIGNALS, cb));
}
+
+export function useReactiveColorString(colorKey: keyof ThemeReactive) {
+ const [l, c, h] = [themeReactive[colorKey].l[0], themeReactive[colorKey].c[0], themeReactive[colorKey].h[0]];
+ return () => `oklch(${l()} ${c()} ${h()}deg)`;
+}
diff --git a/js/app/packages/macro-icons/macro-macro-badge.svg b/js/app/packages/macro-icons/macro-macro-badge.svg
new file mode 100644
index 0000000000..9d9fe3029d
--- /dev/null
+++ b/js/app/packages/macro-icons/macro-macro-badge.svg
@@ -0,0 +1,9 @@
+<svg width="100%" height="100%" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
+ <defs>
+ <mask id="hole">
+ <rect width="100%" height="100%" fill="white"/>
+ <circle r="5" cx="21" cy="3" fill="black"/>
+ </mask>
+ </defs>
+ <path mask="url(#hole)" d="m6.25 4.038-2.242 0.8792v5.8184l-1.756-1.6582-2.242 0.8792v6.6766c0 0.2568 0.106 0.502 0.292 0.6784l2.794 2.6422 2.244-0.879v-5.8184l7.084 6.6974 2.244-0.879v-5.8184l7.086 6.6976 2.24-0.8792v-6.6766c0-0.2568-0.104-0.5022-0.292-0.6784l-8.124-7.6816-2.244 0.879v5.8184z"/>
+</svg>