File size: 2,846 Bytes
2b2ba56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { useCallback, useSyncExternalStore } from 'react'

import type { Translations } from '@/i18n'

interface SessionSliceStore<T> {
  get(): Record<string, T | undefined>
  listen(listener: () => void): () => void
}

/** Subscribe to one stable per-session store entry without reacting to other sessions' writes. */
export function useSessionValue<T>(store: SessionSliceStore<T>, sessionId: string | null): T | undefined {
  const subscribe = useCallback((onChange: () => void) => store.listen(onChange), [store])

  return useSyncExternalStore(subscribe, () => (sessionId ? store.get()[sessionId] : undefined))
}

export interface ConfirmState {
  title: string
  description: string
  destructive?: boolean
  confirmLabel?: string
  onConfirm: () => Promise<void> | void
}

export function formatApproximateTime(targetTimestamp: number): string {
  if (!targetTimestamp) {
    return ''
  }

  const targetMs = targetTimestamp > 1e11 ? targetTimestamp : targetTimestamp * 1000
  const diffMs = targetMs - Date.now()

  if (diffMs <= 0) {
    return '~0s'
  }

  const seconds = Math.round(diffMs / 1000)

  if (seconds < 60) {
    return `~${seconds}s`
  }

  const minutes = Math.round(seconds / 60)

  if (minutes < 60) {
    return `~${minutes}m`
  }

  const hours = Math.round(minutes / 60)

  if (hours < 24) {
    return `~${hours}h`
  }

  const days = Math.round(hours / 24)

  return `~${days}d`
}

/** Formats a countdown precisely without exposing the user's local timezone. */
export function formatHeartbeatCountdown(targetTimestamp: number, now = Date.now()): string {
  const targetMs = targetTimestamp > 1e11 ? targetTimestamp : targetTimestamp * 1000
  const remainingSeconds = Math.max(0, Math.ceil((targetMs - now) / 1000))
  const days = Math.floor(remainingSeconds / 86_400)
  const hours = Math.floor((remainingSeconds % 86_400) / 3_600)
  const minutes = Math.floor((remainingSeconds % 3_600) / 60)
  const seconds = remainingSeconds % 60
  const padded = (value: number) => String(value).padStart(2, '0')
  const clock = `${padded(hours)}:${padded(minutes)}:${padded(seconds)}`

  return days > 0 ? `${days}d ${clock}` : clock
}

export function formatInterval(seconds: number, t: Translations): string {
  if (seconds % 3600 === 0) {
    return t.statusStack.control.loopEveryHours(seconds / 3600)
  }

  if (seconds % 60 === 0) {
    return t.statusStack.control.loopEveryMinutes(seconds / 60)
  }

  return t.statusStack.control.loopEverySeconds(seconds)
}

export function formatHeartbeatInterval(seconds: number, t: Translations): string {
  if (seconds % 3600 === 0) {
    return t.statusStack.control.heartbeatEveryHours(seconds / 3600)
  }

  if (seconds % 60 === 0) {
    return t.statusStack.control.heartbeatEveryMinutes(seconds / 60)
  }

  return t.statusStack.control.heartbeatEverySeconds(seconds)
}