mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-01-31 01:39:08 +01:00
add push rule hook
This commit is contained in:
parent
643da9e191
commit
d3b2e73b6c
1 changed files with 44 additions and 0 deletions
44
src/app/hooks/usePushRule.ts
Normal file
44
src/app/hooks/usePushRule.ts
Normal file
|
@ -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]);
|
Loading…
Reference in a new issue