Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 511b4a5

Browse files
committed
Add new hooks
1 parent 7fe75a0 commit 511b4a5

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

src/hooks/useAsyncRefreshMemo.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { DependencyList, useCallback, useEffect, useState } from "react";
18+
19+
type Fn<T> = () => Promise<T>;
20+
21+
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue: T): [T, () => void];
22+
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): [T | undefined, () => void];
23+
export function useAsyncRefreshMemo<T>(fn: Fn<T>, deps: DependencyList, initialValue?: T): [T | undefined, () => void] {
24+
const [value, setValue] = useState<T | undefined>(initialValue);
25+
const refresh = useCallback(() => {
26+
let discard = false;
27+
fn()
28+
.then((v) => {
29+
if (!discard) {
30+
setValue(v);
31+
}
32+
})
33+
.catch((err) => console.error(err));
34+
return () => {
35+
discard = true;
36+
};
37+
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
38+
useEffect(refresh, [refresh]);
39+
return [value, refresh];
40+
}

src/hooks/useNotificationSettings.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { IPushRules, MatrixClient } from "matrix-js-sdk/src/matrix";
18+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
19+
20+
import { NotificationSettings } from "../models/notificationsettings/NotificationSettings";
21+
import { PushRuleDiff } from "../models/notificationsettings/PushRuleDiff";
22+
import { reconcileNotificationSettings } from "../models/notificationsettings/reconcileNotificationSettings";
23+
import { toNotificationSettings } from "../models/notificationsettings/toNotificationSettings";
24+
25+
async function applyChanges(cli: MatrixClient, changes: PushRuleDiff): Promise<void> {
26+
await Promise.all(changes.deleted.map((change) => cli.deletePushRule("global", change.kind, change.rule_id)));
27+
await Promise.all(changes.added.map((change) => cli.addPushRule("global", change.kind, change.rule_id, change)));
28+
await Promise.all(
29+
changes.updated.map(async (change) => {
30+
if (change.enabled !== undefined) {
31+
await cli.setPushRuleEnabled("global", change.kind, change.rule_id, change.enabled);
32+
}
33+
if (change.actions !== undefined) {
34+
await cli.setPushRuleActions("global", change.kind, change.rule_id, change.actions);
35+
}
36+
}),
37+
);
38+
}
39+
40+
type UseNotificationSettings = {
41+
model: NotificationSettings | null;
42+
hasPendingChanges: boolean;
43+
reconcile: (model: NotificationSettings) => void;
44+
};
45+
46+
export function useNotificationSettings(cli: MatrixClient): UseNotificationSettings {
47+
const supportsIntentionalMentions = useMemo(() => cli.supportsIntentionalMentions(), [cli]);
48+
49+
const pushRules = useRef<IPushRules | null>(null);
50+
const [model, setModel] = useState<NotificationSettings | null>(null);
51+
const [hasPendingChanges, setPendingChanges] = useState<boolean>(false);
52+
const updatePushRules = useCallback(async () => {
53+
const rules = await cli.getPushRules();
54+
const model = toNotificationSettings(rules, supportsIntentionalMentions);
55+
const pendingChanges = reconcileNotificationSettings(rules, model, supportsIntentionalMentions);
56+
pushRules.current = rules;
57+
setPendingChanges(
58+
pendingChanges.updated.length > 0 || pendingChanges.added.length > 0 || pendingChanges.deleted.length > 0,
59+
);
60+
setModel(model);
61+
}, [cli, supportsIntentionalMentions]);
62+
63+
useEffect(() => {
64+
updatePushRules().catch((err) => console.error(err));
65+
}, [cli, updatePushRules]);
66+
67+
const reconcile = useCallback(
68+
(model: NotificationSettings) => {
69+
setModel(model);
70+
const changes = reconcileNotificationSettings(pushRules.current, model, supportsIntentionalMentions);
71+
applyChanges(cli, changes)
72+
.then(updatePushRules)
73+
.catch((err) => console.error(err));
74+
},
75+
[cli, updatePushRules, supportsIntentionalMentions],
76+
);
77+
78+
return { model, hasPendingChanges, reconcile };
79+
}

src/hooks/usePushers.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { IPusher, MatrixClient } from "matrix-js-sdk/src/matrix";
18+
19+
import { useAsyncRefreshMemo } from "./useAsyncRefreshMemo";
20+
21+
export function usePushers(client: MatrixClient): [IPusher[], () => void] {
22+
return useAsyncRefreshMemo<IPusher[]>(() => client.getPushers().then((it) => it.pushers), [client], []);
23+
}

src/hooks/useThreepids.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
18+
import { IThreepid } from "matrix-js-sdk/src/@types/threepids";
19+
20+
import { useAsyncRefreshMemo } from "./useAsyncRefreshMemo";
21+
22+
export function useThreepids(client: MatrixClient): [IThreepid[], () => void] {
23+
return useAsyncRefreshMemo<IThreepid[]>(() => client.getThreePids().then((it) => it.threepids), [client], []);
24+
}

0 commit comments

Comments
 (0)