|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useCallback, useEffect, useState } from 'react' |
| 4 | + |
| 5 | +export type DeviceVersionCandidate = { |
| 6 | + _id: string |
| 7 | + brand: string |
| 8 | + model: string |
| 9 | + name?: string | null |
| 10 | + appVersionCode?: number | null |
| 11 | + appVersionInfo?: { |
| 12 | + versionCode?: number | null |
| 13 | + } | null |
| 14 | +} |
| 15 | + |
| 16 | +export const DEFAULT_LATEST_APP_VERSION_CODE = 17 |
| 17 | +export const UPDATE_APP_REMIND_LATER_MS = 6 * 60 * 60 * 1000 |
| 18 | +export const UPDATE_APP_DONT_ASK_AGAIN_MS = 30 * 24 * 60 * 60 * 1000 |
| 19 | + |
| 20 | +const UPDATE_APP_SNOOZE_KEY = 'update_app_prompt_snooze_until' |
| 21 | +const UPDATE_APP_SNOOZE_EVENT = 'update-app-prompt-snooze-changed' |
| 22 | + |
| 23 | +const envLatestVersionCode = Number.parseInt( |
| 24 | + process.env.NEXT_PUBLIC_LATEST_APP_VERSION_CODE?.trim() ?? '', |
| 25 | + 10 |
| 26 | +) |
| 27 | + |
| 28 | +export const latestAppVersionCode = |
| 29 | + Number.isFinite(envLatestVersionCode) && envLatestVersionCode > 0 |
| 30 | + ? envLatestVersionCode |
| 31 | + : DEFAULT_LATEST_APP_VERSION_CODE |
| 32 | + |
| 33 | +export function getDeviceVersionCode(device: DeviceVersionCandidate) { |
| 34 | + const heartbeatVersionCode = device.appVersionInfo?.versionCode |
| 35 | + |
| 36 | + if (typeof heartbeatVersionCode === 'number') { |
| 37 | + return heartbeatVersionCode |
| 38 | + } |
| 39 | + |
| 40 | + return typeof device.appVersionCode === 'number' ? device.appVersionCode : null |
| 41 | +} |
| 42 | + |
| 43 | +export function isDeviceOutdated( |
| 44 | + device: DeviceVersionCandidate, |
| 45 | + latestVersionCode = latestAppVersionCode |
| 46 | +) { |
| 47 | + const deviceVersionCode = getDeviceVersionCode(device) |
| 48 | + |
| 49 | + if (deviceVersionCode === null) { |
| 50 | + return false |
| 51 | + } |
| 52 | + |
| 53 | + return deviceVersionCode < latestVersionCode |
| 54 | +} |
| 55 | + |
| 56 | +export function getOutdatedDevices( |
| 57 | + devices: DeviceVersionCandidate[] | undefined, |
| 58 | + latestVersionCode = latestAppVersionCode |
| 59 | +) { |
| 60 | + if (!devices?.length) { |
| 61 | + return [] |
| 62 | + } |
| 63 | + |
| 64 | + return devices.filter((device) => isDeviceOutdated(device, latestVersionCode)) |
| 65 | +} |
| 66 | + |
| 67 | +export function summarizeOutdatedDeviceNames( |
| 68 | + devices: DeviceVersionCandidate[], |
| 69 | + formatDeviceName: (device: DeviceVersionCandidate) => string, |
| 70 | + visibleCount = 3 |
| 71 | +) { |
| 72 | + const visibleDevices = devices.slice(0, visibleCount).map(formatDeviceName) |
| 73 | + const remainingCount = Math.max(devices.length - visibleDevices.length, 0) |
| 74 | + |
| 75 | + if (visibleDevices.length === 0) { |
| 76 | + return '' |
| 77 | + } |
| 78 | + |
| 79 | + if (visibleDevices.length === 1) { |
| 80 | + return visibleDevices[0] |
| 81 | + } |
| 82 | + |
| 83 | + if (visibleDevices.length === 2) { |
| 84 | + return remainingCount > 0 |
| 85 | + ? `${visibleDevices[0]}, ${visibleDevices[1]} and ${remainingCount} more device${ |
| 86 | + remainingCount > 1 ? 's' : '' |
| 87 | + }` |
| 88 | + : `${visibleDevices[0]} and ${visibleDevices[1]}` |
| 89 | + } |
| 90 | + |
| 91 | + const namedPrefix = `${visibleDevices[0]}, ${visibleDevices[1]} and ${visibleDevices[2]}` |
| 92 | + |
| 93 | + return remainingCount > 0 |
| 94 | + ? `${namedPrefix} and ${remainingCount} more device${ |
| 95 | + remainingCount > 1 ? 's' : '' |
| 96 | + }` |
| 97 | + : namedPrefix |
| 98 | +} |
| 99 | + |
| 100 | +function readUpdatePromptSnoozeUntil() { |
| 101 | + if (typeof window === 'undefined') { |
| 102 | + return 0 |
| 103 | + } |
| 104 | + |
| 105 | + const value = window.localStorage.getItem(UPDATE_APP_SNOOZE_KEY) |
| 106 | + const parsedValue = value ? Number.parseInt(value, 10) : 0 |
| 107 | + |
| 108 | + return Number.isFinite(parsedValue) ? parsedValue : 0 |
| 109 | +} |
| 110 | + |
| 111 | +function emitUpdatePromptSnoozeChanged() { |
| 112 | + if (typeof window === 'undefined') { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + window.dispatchEvent(new Event(UPDATE_APP_SNOOZE_EVENT)) |
| 117 | +} |
| 118 | + |
| 119 | +export function setUpdatePromptSnooze(durationMs: number) { |
| 120 | + if (typeof window === 'undefined') { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + window.localStorage.setItem( |
| 125 | + UPDATE_APP_SNOOZE_KEY, |
| 126 | + (Date.now() + durationMs).toString() |
| 127 | + ) |
| 128 | + emitUpdatePromptSnoozeChanged() |
| 129 | +} |
| 130 | + |
| 131 | +export function useUpdatePromptSnooze() { |
| 132 | + const [snoozeUntil, setSnoozeUntil] = useState(0) |
| 133 | + |
| 134 | + const refreshSnoozeUntil = useCallback(() => { |
| 135 | + setSnoozeUntil(readUpdatePromptSnoozeUntil()) |
| 136 | + }, []) |
| 137 | + |
| 138 | + useEffect(() => { |
| 139 | + refreshSnoozeUntil() |
| 140 | + |
| 141 | + const handleVisibilityChange = () => { |
| 142 | + if (document.visibilityState === 'visible') { |
| 143 | + refreshSnoozeUntil() |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + window.addEventListener('storage', refreshSnoozeUntil) |
| 148 | + window.addEventListener( |
| 149 | + UPDATE_APP_SNOOZE_EVENT, |
| 150 | + refreshSnoozeUntil as EventListener |
| 151 | + ) |
| 152 | + document.addEventListener('visibilitychange', handleVisibilityChange) |
| 153 | + |
| 154 | + return () => { |
| 155 | + window.removeEventListener('storage', refreshSnoozeUntil) |
| 156 | + window.removeEventListener( |
| 157 | + UPDATE_APP_SNOOZE_EVENT, |
| 158 | + refreshSnoozeUntil as EventListener |
| 159 | + ) |
| 160 | + document.removeEventListener('visibilitychange', handleVisibilityChange) |
| 161 | + } |
| 162 | + }, [refreshSnoozeUntil]) |
| 163 | + |
| 164 | + return { |
| 165 | + isSnoozed: snoozeUntil > Date.now(), |
| 166 | + snoozeUntil, |
| 167 | + refreshSnoozeUntil, |
| 168 | + } |
| 169 | +} |
0 commit comments