From d3b2e73b6cc411cca9f404c9db201622e90db796 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:28:47 +0530 Subject: [PATCH] add push rule hook --- src/app/hooks/usePushRule.ts | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/app/hooks/usePushRule.ts diff --git a/src/app/hooks/usePushRule.ts b/src/app/hooks/usePushRule.ts new file mode 100644 index 00000000..04f560c0 --- /dev/null +++ b/src/app/hooks/usePushRule.ts @@ -0,0 +1,44 @@ +import { IPushRule, IPushRules, PushRuleKind, RuleId } from 'matrix-js-sdk'; +import { useMemo } from 'react'; + +export type PushRuleData = { + kind: PushRuleKind; + pushRule: IPushRule; +}; + +export const orderedPushRuleKinds: PushRuleKind[] = [ + PushRuleKind.Override, + PushRuleKind.ContentSpecific, + PushRuleKind.RoomSpecific, + PushRuleKind.SenderSpecific, + PushRuleKind.Underride, +]; + +export const getPushRule = ( + pushRules: IPushRules, + ruleId: RuleId | string +): PushRuleData | undefined => { + const { global } = pushRules; + + let ruleData: PushRuleData | undefined; + + orderedPushRuleKinds.some((kind) => { + const rules = global[kind]; + const pushRule = rules?.find((r) => r.rule_id === ruleId); + if (pushRule) { + ruleData = { + kind, + pushRule, + }; + return true; + } + return false; + }); + + return ruleData; +}; + +export const usePushRule = ( + pushRules: IPushRules, + ruleId: RuleId | string +): PushRuleData | undefined => useMemo(() => getPushRule(pushRules, ruleId), [pushRules, ruleId]);