single condition check for fetching and disabling button

This commit is contained in:
Josh Hawkins 2025-08-07 16:25:41 -05:00
parent 72b591efb1
commit fe5dd1e67d

View File

@ -165,13 +165,27 @@ export default function NotificationView({
const watchAllEnabled = form.watch("allEnabled"); const watchAllEnabled = form.watch("allEnabled");
const watchCameras = useMemo(() => form.watch("cameras") || [], [form]); const watchCameras = useMemo(() => form.watch("cameras") || [], [form]);
const { data: publicKey } = useSWR( const anyCameraNotificationsEnabled = useMemo(
() =>
config &&
Object.values(config.cameras).some(
(c) =>
c.enabled_in_config &&
c.notifications &&
c.notifications.enabled_in_config,
),
[config],
);
const shouldFetchPubKey = Boolean(
config && config &&
(config.notifications?.enabled || (config.notifications?.enabled || anyCameraNotificationsEnabled) &&
watchAllEnabled || (watchAllEnabled ||
(Array.isArray(watchCameras) && watchCameras.length > 0)) (Array.isArray(watchCameras) && watchCameras.length > 0)),
? "notifications/pubkey" );
: null,
const { data: publicKey } = useSWR(
shouldFetchPubKey ? "notifications/pubkey" : null,
{ revalidateOnFocus: false }, { revalidateOnFocus: false },
); );
@ -525,13 +539,7 @@ export default function NotificationView({
</Heading> </Heading>
<Button <Button
aria-label={t("notification.registerDevice")} aria-label={t("notification.registerDevice")}
disabled={ disabled={!shouldFetchPubKey || publicKey == undefined}
(!config?.notifications.enabled &&
notificationCameras.length === 0 &&
!form.watch("allEnabled") &&
form.watch("cameras").length === 0) ||
publicKey == undefined
}
onClick={() => { onClick={() => {
if (registration == null) { if (registration == null) {
Notification.requestPermission().then((permission) => { Notification.requestPermission().then((permission) => {