File size: 2,475 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { type CodexPlanType } from '@/store/app-store';

const WINDOW_DEFAULT_LABEL = 'Usage window';
const RESET_LABEL = 'Resets';
const UNKNOWN_LABEL = 'Unknown';
const DAY_UNIT = 'day';
const HOUR_UNIT = 'hour';
const MINUTE_UNIT = 'min';
const WINDOW_SUFFIX = 'window';
const MINUTES_PER_HOUR = 60;
const MINUTES_PER_DAY = 24 * MINUTES_PER_HOUR;
const MILLISECONDS_PER_SECOND = 1000;
const SESSION_HOURS = 5;
const DAYS_PER_WEEK = 7;
const SESSION_WINDOW_MINS = SESSION_HOURS * MINUTES_PER_HOUR;
const WEEKLY_WINDOW_MINS = DAYS_PER_WEEK * MINUTES_PER_DAY;
const SESSION_TITLE = 'Session Usage';
const SESSION_SUBTITLE = '5-hour rolling window';
const WEEKLY_TITLE = 'Weekly';
const WEEKLY_SUBTITLE = 'All models';
const FALLBACK_TITLE = 'Usage Window';
const PLAN_TYPE_LABELS: Record<CodexPlanType, string> = {
  free: 'Free',
  plus: 'Plus',
  pro: 'Pro',
  team: 'Team',
  business: 'Business',
  enterprise: 'Enterprise',
  edu: 'Education',
  unknown: UNKNOWN_LABEL,
};

export function formatCodexWindowDuration(minutes: number | null): string {
  if (!minutes || minutes <= 0) return WINDOW_DEFAULT_LABEL;
  if (minutes % MINUTES_PER_DAY === 0) {
    const days = minutes / MINUTES_PER_DAY;
    return `${days} ${DAY_UNIT}${days === 1 ? '' : 's'} ${WINDOW_SUFFIX}`;
  }
  if (minutes % MINUTES_PER_HOUR === 0) {
    const hours = minutes / MINUTES_PER_HOUR;
    return `${hours} ${HOUR_UNIT}${hours === 1 ? '' : 's'} ${WINDOW_SUFFIX}`;
  }
  return `${minutes} ${MINUTE_UNIT} ${WINDOW_SUFFIX}`;
}

export type CodexWindowLabel = {
  title: string;
  subtitle: string;
  isPrimary: boolean;
};

export function getCodexWindowLabel(windowDurationMins: number | null): CodexWindowLabel {
  if (windowDurationMins === SESSION_WINDOW_MINS) {
    return { title: SESSION_TITLE, subtitle: SESSION_SUBTITLE, isPrimary: true };
  }
  if (windowDurationMins === WEEKLY_WINDOW_MINS) {
    return { title: WEEKLY_TITLE, subtitle: WEEKLY_SUBTITLE, isPrimary: false };
  }
  return {
    title: FALLBACK_TITLE,
    subtitle: formatCodexWindowDuration(windowDurationMins),
    isPrimary: false,
  };
}

export function formatCodexResetTime(resetsAt: number | null): string | null {
  if (!resetsAt) return null;
  const date = new Date(resetsAt * MILLISECONDS_PER_SECOND);
  return `${RESET_LABEL} ${date.toLocaleString()}`;
}

export function formatCodexPlanType(plan: CodexPlanType | null): string {
  if (!plan) return UNKNOWN_LABEL;
  return PLAN_TYPE_LABELS[plan] ?? plan;
}