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.

chore(notifications): unify notification setting and persisted signal logic (#384)

teo6bfdf2419dbc
diff --git a/js/app/packages/app/component/settings/Notification.tsx b/js/app/packages/app/component/settings/Notification.tsx
index e450cf4a91..6a71a391a4 100644
--- a/js/app/packages/app/component/settings/Notification.tsx
+++ b/js/app/packages/app/component/settings/Notification.tsx
@@ -1,27 +1,35 @@
import { TabContent } from '@core/component/TabContent';
import { Switch } from '@kobalte/core/switch';
import { Show } from 'solid-js';
-import { usePlatformNotificationState } from '@notifications';
+import {
+ type SupportedNotificationSettings,
+ useNotificationSettings,
+} from '@notifications';
export function Notification() {
- const notificationState = usePlatformNotificationState();
+ const settings = useNotificationSettings();
- // this property is not reactive nor should it be
- if (notificationState === 'not-supported')
- return <NotificationNotSupported />;
+ return (
+ <Show
+ when={settings.isSupported && settings}
+ fallback={<NotificationNotSupported />}
+ >
+ {(s) => <NotificationSettings settings={s()} />}
+ </Show>
+ );
+}
+function NotificationSettings(props: {
+ settings: SupportedNotificationSettings;
+}) {
return (
<TabContent title="Notifications">
<div class="flex flex-col gap-2 max-w-64 select-none">
<div class="flex items-center justify-between mt-2">
<div class="text-sm">Notifications</div>
<Switch
- checked={notificationState.permission.latest === 'granted'}
- onChange={(enabled) =>
- enabled
- ? notificationState.requestPermission()
- : notificationState.unregisterNotification()
- }
+ checked={props.settings.isEnabled()}
+ onChange={props.settings.toggle}
class="focus-bracket-within"
>
<Switch.Input class="sr-only" />
@@ -30,46 +38,6 @@ export function Notification() {
</Switch.Control>
</Switch>
</div>
-
- <div
- class="transition-all transition-discrete"
- classList={{
- 'hidden opacity-0':
- notificationState.permission.latest !== 'granted',
- 'block opacity-100 starting:opacity-0':
- notificationState.permission.latest === 'granted',
- }}
- >
- <Show when={import.meta.env.MODE === 'development'}>
- <div class="flex items-center justify-between mt-2 pl-2">
- <div class="text-sm">New Message</div>
- <Switch class="focus-bracket-within" disabled>
- <Switch.Input class="sr-only" />
- <Switch.Control class="mt-1 inline-flex h-6 w-11 hover:ring-1 hover:ring-edge rounded-full border-2 border-transparent transition-colors bg-edge focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 data-[checked]:bg-accent">
- <Switch.Thumb class="block h-5 w-5 rounded-full bg-dialog transition-transform data-[checked]:translate-x-5" />
- </Switch.Control>
- </Switch>
- </div>
- <div class="flex items-center justify-between mt-2 pl-2">
- <div class="text-sm">@ Mention</div>
- <Switch class="focus-bracket-within" disabled>
- <Switch.Input class="sr-only" />
- <Switch.Control class="mt-1 inline-flex h-6 w-11 hover:ring-1 hover:ring-edge rounded-full border-2 border-transparent transition-colors bg-edge focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 data-[checked]:bg-accent">
- <Switch.Thumb class="block h-5 w-5 rounded-full bg-dialog transition-transform data-[checked]:translate-x-5" />
- </Switch.Control>
- </Switch>
- </div>
- <div class="flex items-center justify-between mt-2 pl-2">
- <div class="text-sm">Shared Document</div>
- <Switch class="focus-bracket-within" disabled>
- <Switch.Input class="sr-only" />
- <Switch.Control class="mt-1 inline-flex h-6 w-11 hover:ring-1 hover:ring-edge rounded-full border-2 border-transparent transition-colors bg-edge focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 data-[checked]:bg-accent">
- <Switch.Thumb class="block h-5 w-5 rounded-full bg-dialog transition-transform data-[checked]:translate-x-5" />
- </Switch.Control>
- </Switch>
- </div>
- </Show>
- </div>
</div>
</TabContent>
);
diff --git a/js/app/packages/notifications/components/BrowserNotificationModal.tsx b/js/app/packages/notifications/components/BrowserNotificationModal.tsx
index fbc1ec07cf..57a4d2666d 100644
--- a/js/app/packages/notifications/components/BrowserNotificationModal.tsx
+++ b/js/app/packages/notifications/components/BrowserNotificationModal.tsx
@@ -1,105 +1,61 @@
import { useIsAuthenticated } from '@core/auth';
import { Dialog } from '@kobalte/core/dialog';
-import { createMemo, createSignal, Show } from 'solid-js';
-import { usePlatformNotificationState } from './PlatformNotificationProvider';
+import { createMemo } from 'solid-js';
+import { useNotificationSettings } from '../notification-settings';
-const DISMISSED_KEY = 'browser-notification-modal-dismissed';
-
-function useBrowserNotificationModal() {
+export const BrowserNotificationModal = () => {
const isAuthenticated = useIsAuthenticated();
- const notificationState = usePlatformNotificationState();
- const [isDismissed, setIsDismissed] = createSignal(
- !!localStorage.getItem(DISMISSED_KEY)
- );
-
- const shouldShow = createMemo(() => {
- if (notificationState === 'not-supported') {
- return false;
- }
+ const settings = useNotificationSettings();
- if (!isAuthenticated()) {
- return false;
- }
-
- if (isDismissed()) {
- return false;
- }
+ if (!settings.isSupported) return null;
- const permission = notificationState.permission();
- return (
- permission !== undefined &&
- permission !== 'granted' &&
- permission !== 'denied' &&
- permission !== 'disabled-in-ui'
- );
- });
-
- const handleEnableNotifications = async () => {
- if (notificationState === 'not-supported') {
- return;
- }
+ const shouldShow = createMemo(
+ () => isAuthenticated() && settings.shouldPrompt()
+ );
+ const handleEnable = async () => {
try {
- await notificationState.requestPermission();
+ await settings.toggle(true);
} catch (error) {
console.error('Failed to enable notifications:', error);
}
};
- const handleDismiss = () => {
- localStorage.setItem(DISMISSED_KEY, 'true');
- setIsDismissed(true);
- };
-
- return {
- shouldShow,
- handleEnableNotifications,
- handleDismiss,
- isSupported: notificationState !== 'not-supported',
- };
-}
-
-export const BrowserNotificationModal = () => {
- const { shouldShow, handleEnableNotifications, handleDismiss, isSupported } =
- useBrowserNotificationModal();
-
return (
- <Show when={isSupported}>
- <Dialog open={shouldShow()} modal={true}>
- <Dialog.Portal>
- <Dialog.Overlay class="fixed inset-0 z-modal bg-modal-overlay" />
- <div class="fixed inset-0 z-modal w-screen h-screen flex items-center justify-center">
- <Dialog.Content class="flex items-center justify-center">
- <div class="pointer-events-auto max-w-xl bg-menu border border-edge w-lg h-fit p-2">
- <div class="w-full my-1">
- <h2 class="text-xl mb-3">Enable Browser Notifications</h2>
-
- <div class="mb-4">
- <p class="text-ink-muted text-sm">
- Get notified about new messages, mentions, comments, and
- emails.
- </p>
- </div>
- <div class="flex justify-end mt-2 tex-sm pt-2 gap-2">
- <button
- class="py-1 px-3 font-mono text-sm"
- onClick={handleDismiss}
- >
- Not Now
- </button>
- <button
- class="uppercase py-1 px-3 font-mono text-sm bg-accent text-menu"
- onClick={handleEnableNotifications}
- >
- Enable
- </button>
- </div>
+ <Dialog open={shouldShow()} modal={true}>
+ <Dialog.Portal>
+ <Dialog.Overlay class="fixed inset-0 z-modal bg-modal-overlay" />
+ <div class="fixed inset-0 z-modal w-screen h-screen flex items-center justify-center">
+ <Dialog.Content class="flex items-center justify-center">
+ <div class="pointer-events-auto max-w-xl bg-menu border border-edge w-lg h-fit p-2">
+ <div class="w-full my-1">
+ <h2 class="text-xl mb-3">Enable Browser Notifications</h2>
+
+ <div class="mb-4">
+ <p class="text-ink-muted text-sm">
+ Get notified about new messages, mentions, comments, and
+ emails.
+ </p>
+ </div>
+ <div class="flex justify-end mt-2 tex-sm pt-2 gap-2">
+ <button
+ class="py-1 px-3 font-mono text-sm"
+ onClick={settings.dismissPrompt}
+ >
+ Not Now
+ </button>
+ <button
+ class="uppercase py-1 px-3 font-mono text-sm bg-accent text-menu"
+ onClick={handleEnable}
+ >
+ Enable
+ </button>
</div>
</div>
- </Dialog.Content>
- </div>
- </Dialog.Portal>
- </Dialog>
- </Show>
+ </div>
+ </Dialog.Content>
+ </div>
+ </Dialog.Portal>
+ </Dialog>
);
};
diff --git a/js/app/packages/notifications/components/PlatformNotificationProvider.tsx b/js/app/packages/notifications/components/PlatformNotificationProvider.tsx
index 12acfac23d..e26f504f77 100644
--- a/js/app/packages/notifications/components/PlatformNotificationProvider.tsx
+++ b/js/app/packages/notifications/components/PlatformNotificationProvider.tsx
@@ -207,7 +207,8 @@ export function PlatformNotificationProvider(props: {
overrideDefault?: CreateAppNotificationInterface;
}) {
const [manuallyDisabled, setManuallyDisabled] = makePersisted(
- createSignal<UserSetting>('allowed')
+ createSignal<UserSetting>('allowed'),
+ { name: 'notification-manually-disabled' }
);
const setDisabled = async () => {
diff --git a/js/app/packages/notifications/index.ts b/js/app/packages/notifications/index.ts
index 9a6682f65a..5e84924949 100644
--- a/js/app/packages/notifications/index.ts
+++ b/js/app/packages/notifications/index.ts
@@ -75,6 +75,11 @@ export {
DefaultNotificationBlockNameResolver,
DefaultUserNameResolver,
} from './notification-resolvers';
+export type {
+ NotificationSettings,
+ SupportedNotificationSettings,
+} from './notification-settings';
+export { useNotificationSettings } from './notification-settings';
export type { NotificationSource } from './notification-source';
export { createNotificationSource } from './notification-source';
export { fetchNotificationsForEntities } from './queries/entities-notifications-query';
diff --git a/js/app/packages/notifications/notification-settings.ts b/js/app/packages/notifications/notification-settings.ts
new file mode 100644
index 0000000000..c55cb9e348
--- /dev/null
+++ b/js/app/packages/notifications/notification-settings.ts
@@ -0,0 +1,94 @@
+import type { Accessor, Resource } from 'solid-js';
+import { createSignal } from 'solid-js';
+import {
+ type PlatformNotificationState,
+ usePlatformNotificationState,
+} from './components/PlatformNotificationProvider';
+
+type UiDisabled = 'disabled-in-ui';
+
+function createPersistedDismissed(
+ key: string
+): [isDismissed: Accessor<boolean>, dismiss: () => void] {
+ const [isDismissed, setIsDismissed] = createSignal(
+ !!localStorage.getItem(key)
+ );
+
+ const dismiss = () => {
+ localStorage.setItem(key, 'true');
+ setIsDismissed(true);
+ };
+
+ return [isDismissed, dismiss];
+}
+
+function createIsEnabled(
+ permission: Resource<NotificationPermission | UiDisabled>
+): Accessor<boolean> {
+ return () => permission.latest === 'granted';
+}
+
+function createShouldPrompt(
+ permission: Resource<NotificationPermission | UiDisabled>,
+ isDismissed: Accessor<boolean>
+): Accessor<boolean> {
+ return () => {
+ if (isDismissed()) return false;
+ const p = permission();
+ return (
+ p !== undefined &&
+ p !== 'granted' &&
+ p !== 'denied' &&
+ p !== 'disabled-in-ui'
+ );
+ };
+}
+
+function createToggle(
+ state: PlatformNotificationState
+): (enabled: boolean) => Promise<void> {
+ return async (enabled: boolean) => {
+ if (enabled) {
+ await state.requestPermission();
+ } else {
+ await state.unregisterNotification();
+ }
+ };
+}
+
+const PROMPT_DISMISSED_KEY = 'notification-prompt-dismissed';
+
+export type SupportedNotificationSettings = {
+ isSupported: true;
+ /** Whether notifications are currently enabled (granted and not disabled in UI) */
+ isEnabled: Accessor<boolean>;
+ /** Toggle notifications on/off */
+ toggle: (enabled: boolean) => Promise<void>;
+ /** Whether the enable prompt should be shown (permission not yet decided, not dismissed) */
+ shouldPrompt: Accessor<boolean>;
+ /** Dismiss the enable prompt */
+ dismissPrompt: () => void;
+};
+
+export type NotificationSettings =
+ | SupportedNotificationSettings
+ | { isSupported: false };
+
+export function useNotificationSettings(): NotificationSettings {
+ const state = usePlatformNotificationState();
+
+ if (state === 'not-supported') {
+ return { isSupported: false };
+ }
+
+ const [isDismissed, dismissPrompt] =
+ createPersistedDismissed(PROMPT_DISMISSED_KEY);
+
+ return {
+ isSupported: true,
+ isEnabled: createIsEnabled(state.permission),
+ toggle: createToggle(state),
+ shouldPrompt: createShouldPrompt(state.permission, isDismissed),
+ dismissPrompt,
+ };
+}