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

Commit e88d1e0

Browse files
committed
Make hook work before push rules are fetched
1 parent 1471862 commit e88d1e0

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/hooks/useUserOnboardingContext.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,37 @@ function useUserOnboardingContextValue<T>(defaultValue: T, callback: (cli: Matri
7777
}
7878

7979
function useShowNotificationsPrompt(): boolean {
80-
const [value, setValue] = useState<boolean>(Notifier.shouldShowPrompt());
80+
const client = useMatrixClientContext();
81+
82+
const [value, setValue] = useState<boolean>(client.pushRules ? Notifier.shouldShowPrompt() : true);
83+
84+
const updateValue = useCallback(() => {
85+
setValue(client.pushRules ? Notifier.shouldShowPrompt() : true);
86+
}, [client]);
87+
8188
useEventEmitter(Notifier, NotifierEvent.NotificationHiddenChange, () => {
82-
setValue(Notifier.shouldShowPrompt());
89+
updateValue();
8390
});
91+
8492
const setting = useSettingValue("notificationsEnabled");
8593
useEffect(() => {
86-
setValue(Notifier.shouldShowPrompt());
87-
}, [setting]);
94+
updateValue();
95+
}, [setting, updateValue]);
96+
97+
// shouldShowPrompt is dependent on the client having push rules. There isn't an event for the client
98+
// fetching its push rules, but we'll know it has them by the time it sync, so we update this on sync.
99+
useEffect(() => {
100+
const onSync = (): void => {
101+
updateValue();
102+
};
103+
104+
client.on(ClientEvent.Sync, onSync);
105+
106+
return () => {
107+
client.removeListener(ClientEvent.Sync, onSync);
108+
};
109+
}, [client, updateValue]);
110+
88111
return value;
89112
}
90113

0 commit comments

Comments
 (0)