| 1 | import type { RelayClientDeviceRecord } from "@t3tools/contracts/relay"; |
| 2 | |
| 3 | const mobileClientUpdatedAtFormatter = new Intl.DateTimeFormat(undefined, { |
| 4 | dateStyle: "medium", |
| 5 | timeStyle: "short", |
| 6 | }); |
| 7 | |
| 8 | const NOTIFICATION_PREFERENCES = [ |
| 9 | ["notifyOnApproval", "approvals"], |
| 10 | ["notifyOnInput", "input requests"], |
| 11 | ["notifyOnCompletion", "completions"], |
| 12 | ["notifyOnFailure", "failures"], |
| 13 | ] as const satisfies ReadonlyArray< |
| 14 | readonly [keyof RelayClientDeviceRecord["notifications"], string] |
| 15 | >; |
| 16 | |
| 17 | export function mobileClientPlatformLabel(device: RelayClientDeviceRecord): string { |
| 18 | const platform = |
| 19 | device.platform === "android" |
| 20 | ? "Android" |
| 21 | : device.iosMajorVersion === null |
| 22 | ? "iOS" |
| 23 | : `iOS ${device.iosMajorVersion}`; |
| 24 | return `${platform}${device.appVersion ? ` · The Knife ${device.appVersion}` : ""}`; |
| 25 | } |
| 26 | |
| 27 | export function mobileClientNotificationDetail(device: RelayClientDeviceRecord): string { |
| 28 | if (!device.notifications.enabled) { |
| 29 | return "Push notifications are disabled on this device."; |
| 30 | } |
| 31 | |
| 32 | const enabledPreferences = NOTIFICATION_PREFERENCES.flatMap(([preference, label]) => |
| 33 | device.notifications[preference] ? [label] : [], |
| 34 | ); |
| 35 | return enabledPreferences.length > 0 |
| 36 | ? `Alerts enabled for ${enabledPreferences.join(", ")}.` |
| 37 | : "Push notifications are enabled, but no alert types are selected."; |
| 38 | } |
| 39 | |
| 40 | export function mobileClientUpdatedAtLabel(updatedAt: string): string { |
| 41 | const date = new Date(updatedAt); |
| 42 | return Number.isNaN(date.getTime()) |
| 43 | ? "Update time unavailable" |
| 44 | : `Updated ${mobileClientUpdatedAtFormatter.format(date)}`; |
| 45 | } |