Files
cinny/src/app/hooks/useNotificationMode.ts
T
Lotus Bot 61a1f008d0 chore: upgrade i18next 26, prettier 3, fontsource-variable, domhandler 6, lint-staged 17
- i18next 23->26 + react-i18next 15->17
- prettier 2->3, reformat all files
- replace @fontsource/inter with @fontsource-variable/inter 5, update import path
- domhandler 5->6 (aligns with transitive deps)
- lint-staged 16->17
2026-05-21 23:30:50 -04:00

69 lines
1.9 KiB
TypeScript

import { PushRuleAction, PushRuleActionName, TweakName } from 'matrix-js-sdk';
import { useCallback, useMemo } from 'react';
export enum NotificationMode {
OFF = 'OFF',
Notify = 'Notify',
NotifyLoud = 'NotifyLoud',
}
export const getNotificationMode = (actions: PushRuleAction[]): NotificationMode => {
const soundTweak = actions.find(
(action) => typeof action === 'object' && action.set_tweak === TweakName.Sound,
);
const notify = actions.find(
(action) => typeof action === 'string' && action === PushRuleActionName.Notify,
);
if (notify && soundTweak) return NotificationMode.NotifyLoud;
if (notify) return NotificationMode.Notify;
return NotificationMode.OFF;
};
export type NotificationModeOptions = {
soundValue?: string;
highlight?: boolean;
};
export const getNotificationModeActions = (
mode: NotificationMode,
options?: NotificationModeOptions,
): PushRuleAction[] => {
if (mode === NotificationMode.OFF) return [];
const actions: PushRuleAction[] = [PushRuleActionName.Notify];
if (mode === NotificationMode.NotifyLoud) {
actions.push({
set_tweak: TweakName.Sound,
value: options?.soundValue ?? 'default',
});
}
if (options?.highlight) {
actions.push({
set_tweak: TweakName.Highlight,
value: true,
});
}
return actions;
};
export type GetNotificationModeCallback = (mode: NotificationMode) => PushRuleAction[];
export const useNotificationModeActions = (
options?: NotificationModeOptions,
): GetNotificationModeCallback => {
const getAction: GetNotificationModeCallback = useCallback(
(mode) => getNotificationModeActions(mode, options),
[options],
);
return getAction;
};
export const useNotificationActionsMode = (actions: PushRuleAction[]): NotificationMode => {
const mode: NotificationMode = useMemo(() => getNotificationMode(actions), [actions]);
return mode;
};