mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-02-12 08:43:38 +01:00
add notification mode hook
This commit is contained in:
parent
d3b2e73b6c
commit
feec0a8414
1 changed files with 66 additions and 0 deletions
66
src/app/hooks/useNotificationMode.ts
Normal file
66
src/app/hooks/useNotificationMode.ts
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import { PushRuleAction, PushRuleActionName, TweakName } from 'matrix-js-sdk';
|
||||||
|
import { useCallback, useMemo } from 'react';
|
||||||
|
|
||||||
|
export enum NotificationMode {
|
||||||
|
OFF = 'OFF',
|
||||||
|
Notify = 'Notify',
|
||||||
|
NotifyLoud = 'NotifyLoud',
|
||||||
|
}
|
||||||
|
|
||||||
|
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(() => {
|
||||||
|
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;
|
||||||
|
}, [actions]);
|
||||||
|
|
||||||
|
return mode;
|
||||||
|
};
|
Loading…
Reference in a new issue