update-banner-rule.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * When a release is worth a banner, and for how long.
  3. *
  4. * Torqvoice ships most days, and a strip at the top of every screen for
  5. * every patch stopped being read: nobody pressed the X, and the next release
  6. * arrived before the notice had let itself out. So a patch (1.2.53 to 1.2.54)
  7. * says nothing, and only a minor or major release (1.2.x to 1.3.0) is
  8. * announced. Which is which is decided by the tag.
  9. *
  10. * The clock is on the user record, once per release across every device:
  11. * one hour from when the banner first appeared to that person, wherever
  12. * that was. Past that it is marked seen and does not come back.
  13. */
  14. /** One hour: long enough to be read, short enough to never become furniture. */
  15. export const UPDATE_BANNER_TTL_MS = 60 * 60 * 1000
  16. /** Versions the app runs under that are not releases at all. */
  17. const NOT_A_RELEASE = new Set(['development', ''])
  18. interface Parsed {
  19. major: number
  20. minor: number
  21. patch: number
  22. }
  23. /** `v1.2.54`, `1.2.54` or `1.2.54-rc1`. Anything else is not a release. */
  24. export function parseVersion(version: string | null | undefined): Parsed | null {
  25. if (!version || NOT_A_RELEASE.has(version)) return null
  26. const m = /^v?(\d+)\.(\d+)\.(\d+)/.exec(version.trim())
  27. if (!m) return null
  28. return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]) }
  29. }
  30. /**
  31. * Whether going from the version the user last saw to the one running is
  32. * worth telling them about. Only a step up in the major or minor part is; a
  33. * patch, a rollback, or a version that is not a release is not.
  34. *
  35. * A user who has never been seeded is never announced to here: the first
  36. * load seeds them silently, as it always has.
  37. */
  38. export function isAnnounced(lastSeen: string | null, current: string): boolean {
  39. const seen = parseVersion(lastSeen)
  40. const now = parseVersion(current)
  41. if (!seen || !now) return false
  42. if (now.major !== seen.major) return now.major > seen.major
  43. return now.minor > seen.minor
  44. }
  45. export type UpdateBannerDecision =
  46. /** Nothing to show; write the version as seen so the next comparison starts here. */
  47. | { kind: 'silent' }
  48. /** Show it, and stamp when it first appeared if that has not happened yet. */
  49. | { kind: 'show'; expiresAt: number; stamp: boolean }
  50. /** Its hour is over: mark seen, show nothing. */
  51. | { kind: 'expired' }
  52. export function decideUpdateBanner(
  53. user: {
  54. lastSeenVersion: string | null
  55. updateBannerVersion: string | null
  56. updateBannerShownAt: Date | string | null
  57. },
  58. current: string,
  59. now = Date.now()
  60. ): UpdateBannerDecision {
  61. if (user.lastSeenVersion === current) return { kind: 'silent' }
  62. if (!isAnnounced(user.lastSeenVersion, current)) return { kind: 'silent' }
  63. const shownAt =
  64. user.updateBannerVersion === current && user.updateBannerShownAt
  65. ? new Date(user.updateBannerShownAt).getTime()
  66. : null
  67. if (shownAt === null) return { kind: 'show', expiresAt: now + UPDATE_BANNER_TTL_MS, stamp: true }
  68. const expiresAt = shownAt + UPDATE_BANNER_TTL_MS
  69. if (expiresAt <= now) return { kind: 'expired' }
  70. return { kind: 'show', expiresAt, stamp: false }
  71. }