summaryrefslogtreecommitdiff
path: root/apps/web/lib/hooks/use-offline-access-sync.ts
blob: a324d862139133aa35c429b649721421f9f876e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"use client"

import { useEffect } from "react"
import { TIER_LIMITS, type SubscriptionTier } from "@asa-news/shared"

export function useOfflineAccessSync(tier: SubscriptionTier | undefined) {
  useEffect(() => {
    if (!tier) return

    const allowed = TIER_LIMITS[tier]?.allowsOfflineReading ?? false

    navigator.serviceWorker?.controller?.postMessage({
      type: "SET_OFFLINE_ACCESS",
      allowed,
    })

    navigator.serviceWorker?.ready.then((registration) => {
      registration.active?.postMessage({
        type: "SET_OFFLINE_ACCESS",
        allowed,
      })
    })
  }, [tier])
}