File size: 2,714 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { WorkflowSummaryFields } from "@/lib/workflows";

export interface GenerationSummary extends WorkflowSummaryFields {
  id: string;
  user_id: string;
  source_id: string;
  source_title: string;
  source_type: string;
  source_url?: string | null;
  status: string;
  clips_count: number;
  top_virality_score?: number | null;
  cover_clip_id?: string | null;
  created_at: string;
  updated_at: string;
}

export async function fetchGenerations(): Promise<GenerationSummary[]> {
  const response = await fetch("/api/tasks/", { cache: "no-store" });
  if (!response.ok) throw new Error(`Failed to fetch tasks: ${response.status}`);
  const data = await response.json();
  return (data.tasks || []) as GenerationSummary[];
}

export function coverClipUrl(generation: Pick<GenerationSummary, "id" | "cover_clip_id">) {
  return generation.cover_clip_id ? `/api/tasks/${generation.id}/clips/${generation.cover_clip_id}/file` : null;
}

export function formatClipDuration(seconds: number) {
  const mins = Math.floor(seconds / 60);
  const secs = Math.floor(seconds % 60);
  return `${mins}:${secs.toString().padStart(2, "0")}`;
}

const UNITS: [Intl.RelativeTimeFormatUnit, number][] = [
  ["year", 31_536_000], ["month", 2_592_000], ["week", 604_800],
  ["day", 86_400], ["hour", 3_600], ["minute", 60],
];

const JUST_NOW: Record<string, string> = {
  en: "just now",
  es: "ahora mismo",
  pt: "agora mesmo",
  de: "gerade eben",
  zh: "刚刚",
  id: "baru saja",
};

export function timeAgo(value: string, locale = "en") {
  const intlLocale = locale === "zh" ? "zh-Hans" : locale === "pt" ? "pt-BR" : locale;
  const relative = new Intl.RelativeTimeFormat(intlLocale, { numeric: "auto" });
  const seconds = (new Date(value).getTime() - Date.now()) / 1000;
  for (const [unit, size] of UNITS) {
    if (Math.abs(seconds) >= size) return relative.format(Math.round(seconds / size), unit);
  }
  return JUST_NOW[locale] ?? JUST_NOW.en;
}

/** `timeAgo` capitalized, for standalone labels ("Just now", "Yesterday"). */
export function timeAgoLabel(value: string, locale = "en") {
  const text = timeAgo(value, locale);
  return text.charAt(0).toLocaleUpperCase(locale) + text.slice(1);
}

/** Tailwind classes for a 0–100 virality score. */
export function scoreTone(score: number) {
  if (score >= 80) return "text-brand";
  if (score >= 60) return "text-amber-600";
  return "text-muted-foreground";
}

export function greeting(name?: string | null) {
  const hour = new Date().getHours();
  const part = hour < 5 ? "Up late" : hour < 12 ? "Good morning" : hour < 18 ? "Good afternoon" : "Good evening";
  const first = name?.trim().split(/\s+/)[0];
  return first ? `${part}, ${first}` : part;
}