The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg } from "gt-react-native"; |
| 2 | |
| 3 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 4 | |
| 5 | const MINUTE = 60_000; |
| 6 | const HOUR = 60 * MINUTE; |
| 7 | const DAY = 24 * HOUR; |
| 8 | |
| 9 | const INTERVAL_UNITS = [ |
| 10 | [7 * DAY, msg("Every week"), msg("{count, plural, one {{count} week} other {{count} weeks}}")], |
| 11 | [DAY, msg("Every day"), msg("{count, plural, one {{count} day} other {{count} days}}")], |
| 12 | [HOUR, msg("Every hour"), msg("{count, plural, one {{count} hour} other {{count} hours}}")], |
| 13 | [ |
| 14 | MINUTE, |
| 15 | msg("Every minute"), |
| 16 | msg("{count, plural, one {{count} minute} other {{count} minutes}}"), |
| 17 | ], |
| 18 | [ |
| 19 | 1_000, |
| 20 | msg("Every second"), |
| 21 | msg("{count, plural, one {{count} second} other {{count} seconds}}"), |
| 22 | ], |
| 23 | [ |
| 24 | 1, |
| 25 | msg("Every millisecond"), |
| 26 | msg("{count, plural, one {{count} millisecond} other {{count} milliseconds}}"), |
| 27 | ], |
| 28 | ] as const; |
| 29 | |
| 30 | export function formatScheduledTaskInterval(everyMs: number, resolve: MessageResolver = m): string { |
| 31 | let remaining = everyMs; |
| 32 | const parts: string[] = []; |
| 33 | for (const [size, every, amount] of INTERVAL_UNITS) { |
| 34 | const count = Math.floor(remaining / size); |
| 35 | if (count === 0) continue; |
| 36 | if (count === 1 && remaining === everyMs && remaining === size) return resolve(every); |
| 37 | parts.push(resolve(amount, { count })); |
| 38 | remaining %= size; |
| 39 | } |
| 40 | return resolve(msg("Every {duration}", { duration: parts.join(" ") })); |
| 41 | } |
| 42 | |
| 43 | function localCalendarDay(date: Date): number { |
| 44 | // Compare calendar days, not 24-hour spans: DST days can have 23 or 25 hours. |
| 45 | return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) / DAY; |
| 46 | } |
| 47 | |
| 48 | const WEEKDAY_KEYS = [ |
| 49 | "sunday", |
| 50 | "monday", |
| 51 | "tuesday", |
| 52 | "wednesday", |
| 53 | "thursday", |
| 54 | "friday", |
| 55 | "saturday", |
| 56 | ] as const; |
| 57 | |
| 58 | export function formatNextScheduledTaskRun( |
| 59 | nextRunAt: string, |
| 60 | now: number, |
| 61 | resolve: MessageResolver = m, |
| 62 | ): string { |
| 63 | const next = new Date(nextRunAt); |
| 64 | const current = new Date(now); |
| 65 | const remaining = next.getTime() - now; |
| 66 | if (!Number.isFinite(remaining)) return resolve(msg("Next run unavailable")); |
| 67 | if (remaining <= 0) return resolve(msg("Next run due")); |
| 68 | |
| 69 | const days = localCalendarDay(next) - localCalendarDay(current); |
| 70 | if (days === 0) { |
| 71 | if (remaining < MINUTE) return resolve(msg("Next run in less than a minute")); |
| 72 | const minutes = Math.round(remaining / MINUTE); |
| 73 | if (minutes < 60) { |
| 74 | return resolve( |
| 75 | msg( |
| 76 | "{count, plural, one {Next run in {count} minute} other {Next run in {count} minutes}}", |
| 77 | { count: minutes }, |
| 78 | ), |
| 79 | ); |
| 80 | } |
| 81 | return resolve( |
| 82 | msg("{count, plural, one {Next run in {count} hour} other {Next run in {count} hours}}", { |
| 83 | count: Math.round(remaining / HOUR), |
| 84 | }), |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | const time = next.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" }); |
| 89 | if (days === 1) return resolve(msg("Next run tomorrow at {time}", { time })); |
| 90 | if (days <= 7) { |
| 91 | return resolve( |
| 92 | msg( |
| 93 | "{day, select, sunday {Next run next Sunday at {time}} monday {Next run next Monday at {time}} tuesday {Next run next Tuesday at {time}} wednesday {Next run next Wednesday at {time}} thursday {Next run next Thursday at {time}} friday {Next run next Friday at {time}} other {Next run next Saturday at {time}}}", |
| 94 | { day: WEEKDAY_KEYS[next.getDay()] ?? "saturday", time }, |
| 95 | ), |
| 96 | ); |
| 97 | } |
| 98 | const date = next.toLocaleDateString([], { |
| 99 | month: "short", |
| 100 | day: "numeric", |
| 101 | ...(next.getFullYear() !== current.getFullYear() ? { year: "numeric" as const } : {}), |
| 102 | }); |
| 103 | return resolve(msg("Next run {date} at {time}", { date, time })); |
| 104 | } |