({
webPush,
onToggle,
})
| 17 | } |
| 18 | |
| 19 | export const WebPushButton: FC<WebPushButtonProps> = ({ |
| 20 | webPush, |
| 21 | onToggle, |
| 22 | }) => { |
| 23 | const internalWebPush = useWebpushNotifications(); |
| 24 | const webPushState = webPush ?? internalWebPush; |
| 25 | |
| 26 | if (!webPushState.enabled) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | const handleClick = async () => { |
| 31 | if (onToggle) { |
| 32 | await onToggle(); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | try { |
| 37 | if (webPushState.subscribed) { |
| 38 | await webPushState.unsubscribe(); |
| 39 | } else { |
| 40 | await webPushState.subscribe(); |
| 41 | } |
| 42 | } catch (error) { |
| 43 | const action = webPushState.subscribed ? "disable" : "enable"; |
| 44 | toast.error(getErrorMessage(error, `Failed to ${action} notifications.`)); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | return ( |
| 49 | <Tooltip> |
| 50 | <TooltipTrigger asChild> |
| 51 | <Button |
| 52 | variant="subtle" |
| 53 | size="icon" |
| 54 | disabled={webPushState.loading} |
| 55 | onClick={handleClick} |
| 56 | aria-label={ |
| 57 | webPushState.subscribed |
| 58 | ? "Disable notifications" |
| 59 | : "Enable notifications" |
| 60 | } |
| 61 | className="size-7 text-content-secondary hover:text-content-primary" |
| 62 | > |
| 63 | {webPushState.loading ? ( |
| 64 | <Spinner size="sm" loading /> |
| 65 | ) : webPushState.subscribed ? ( |
| 66 | <BellIcon className="text-content-success" /> |
| 67 | ) : ( |
| 68 | <BellOffIcon className="text-content-secondary" /> |
| 69 | )} |
| 70 | </Button> |
| 71 | </TooltipTrigger> |
| 72 | <TooltipContent> |
| 73 | {webPushState.subscribed |
| 74 | ? "Disable notifications" |
| 75 | : "Enable notifications"} |
| 76 | </TooltipContent> |
nothing calls this directly
no test coverage detected