Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
/**
|
|
* Pure, dependency-light helpers for rendering in-app notifications: a compact
|
|
* relative-time formatter, a per-kind visual descriptor (icon + accent), and
|
|
* the deep-link target for a notification. Kept free of JSX so it can be reused
|
|
* by the bell, the list, and the full-page center.
|
|
*/
|
|
|
|
import {
|
|
Bell,
|
|
CheckCircle2,
|
|
Coins,
|
|
Gift,
|
|
XCircle,
|
|
type LucideIcon,
|
|
} from "lucide-react";
|
|
import type { Notification } from "@/lib/types";
|
|
|
|
/**
|
|
* Format an ISO-8601 timestamp as a compact "time ago" label:
|
|
* < 1m -> "just now"; < 60m -> "5m"; < 24h -> "3h"; < 7d -> "2d"; else a short
|
|
* date. Returns "" for a missing/unparseable timestamp so callers can omit it.
|
|
*/
|
|
export function formatRelativeTime(iso?: string | null): string {
|
|
if (!iso) return "";
|
|
const then = new Date(iso).getTime();
|
|
if (!Number.isFinite(then)) return "";
|
|
|
|
const diffMs = Date.now() - then;
|
|
if (diffMs < 0) return "just now";
|
|
|
|
const minutes = Math.floor(diffMs / 60_000);
|
|
if (minutes < 1) return "just now";
|
|
if (minutes < 60) return `${minutes}m`;
|
|
|
|
const hours = Math.floor(minutes / 60);
|
|
if (hours < 24) return `${hours}h`;
|
|
|
|
const days = Math.floor(hours / 24);
|
|
if (days < 7) return `${days}d`;
|
|
|
|
return new Date(then).toLocaleDateString("en-US", {
|
|
month: "short",
|
|
day: "numeric",
|
|
});
|
|
}
|
|
|
|
/** Visual descriptor for a notification kind (icon + Tailwind accent color). */
|
|
export interface NotificationKindMeta {
|
|
readonly icon: LucideIcon;
|
|
/** Tailwind text-color class for the icon. */
|
|
readonly accent: string;
|
|
}
|
|
|
|
const KIND_META: Record<string, NotificationKindMeta> = {
|
|
job_complete: { icon: CheckCircle2, accent: "text-success" },
|
|
job_failed: { icon: XCircle, accent: "text-destructive" },
|
|
low_credits: { icon: Coins, accent: "text-accent" },
|
|
referral_earned: { icon: Gift, accent: "text-primary" },
|
|
};
|
|
|
|
const DEFAULT_KIND_META: NotificationKindMeta = {
|
|
icon: Bell,
|
|
accent: "text-muted-foreground",
|
|
};
|
|
|
|
/** Resolve the icon + accent for a notification kind (generic fallback). */
|
|
export function kindMeta(kind: string): NotificationKindMeta {
|
|
return KIND_META[kind] ?? DEFAULT_KIND_META;
|
|
}
|
|
|
|
/** Safely read a string field from a notification's opaque `data` map. */
|
|
function dataString(
|
|
data: Record<string, unknown>,
|
|
key: string,
|
|
): string | null {
|
|
const value = data[key];
|
|
return typeof value === "string" && value.length > 0 ? value : null;
|
|
}
|
|
|
|
/**
|
|
* The in-app route a notification deep-links to, or `null` when it is purely
|
|
* informational. Prefers the job (when `data.job_id` is present); low-credit
|
|
* nudges point at billing and referral payouts at the referral page.
|
|
*/
|
|
export function notificationHref(notification: Notification): string | null {
|
|
const jobId = dataString(notification.data, "job_id");
|
|
if (jobId) return `/jobs/${encodeURIComponent(jobId)}`;
|
|
|
|
if (notification.kind === "low_credits") return "/billing";
|
|
if (notification.kind === "referral_earned") return "/referral";
|
|
return null;
|
|
}
|