diff options
Diffstat (limited to 'frontend/lib')
| -rw-r--r-- | frontend/lib/api.ts | 7 | ||||
| -rw-r--r-- | frontend/lib/format.ts | 35 | ||||
| -rw-r--r-- | frontend/lib/overview.ts | 2 |
3 files changed, 42 insertions, 2 deletions
diff --git a/frontend/lib/api.ts b/frontend/lib/api.ts index 13c73bf..2635af5 100644 --- a/frontend/lib/api.ts +++ b/frontend/lib/api.ts @@ -1,4 +1,4 @@ -import type { FilingsResponse, FinancialsResponse, HistoryPoint, InsidersResponse, MarketIndex, RatiosResponse, SearchResult, TickerOverview, ValuationResponse, WatchlistResponse } from "@/types/api"; +import type { FilingsResponse, FinancialsResponse, HistoryPoint, InsidersResponse, MarketIndex, NewsResponse, RatiosResponse, SearchResult, TickerOverview, ValuationResponse, WatchlistResponse } from "@/types/api"; const API_BASE = (process.env.NEXT_PUBLIC_API_BASE_URL || "").trim().replace(/\/$/, ""); @@ -74,4 +74,9 @@ export const api = { `/api/tickers/${encodeURIComponent(symbol)}/filings` ); }, + news(symbol: string) { + return request<NewsResponse>( + `/api/tickers/${encodeURIComponent(symbol)}/news` + ); + }, }; diff --git a/frontend/lib/format.ts b/frontend/lib/format.ts index 34bffd8..6a1fcc2 100644 --- a/frontend/lib/format.ts +++ b/frontend/lib/format.ts @@ -29,3 +29,38 @@ export function deltaClass(value?: number | null): string { if (value === null || value === undefined) return "neutral"; return value >= 0 ? "positive" : "negative"; } + +export function formatNewsTimestamp(iso: string): { display: string; iso: string } { + const date = new Date(iso); + const now = new Date(); + const tz: Intl.DateTimeFormatOptions = { timeZone: "America/Los_Angeles" }; + const diffMs = now.getTime() - date.getTime(); + const diffMins = Math.floor(diffMs / 60_000); + const diffHours = Math.floor(diffMs / 3_600_000); + + if (diffMins < 1) { + return { display: "Just now", iso }; + } + if (diffMins < 60) { + return { display: `${diffMins}m ago`, iso }; + } + if (diffHours < 24) { + return { display: `${diffHours}h ago`, iso }; + } + + const inTz = (options: Intl.DateTimeFormatOptions) => + date.toLocaleString("en-US", { ...options, ...tz }); + + if (diffHours < 48) { + const time = inTz({ hour: "numeric", minute: "2-digit" }); + return { display: `Yesterday ${time}`, iso }; + } + + const yearNow = Number(now.toLocaleString("en-US", { year: "numeric", ...tz })); + const yearDate = Number(inTz({ year: "numeric" })); + + if (yearDate === yearNow) { + return { display: inTz({ month: "short", day: "numeric" }), iso }; + } + return { display: inTz({ month: "short", day: "numeric", year: "numeric" }), iso }; +} diff --git a/frontend/lib/overview.ts b/frontend/lib/overview.ts index 62995d6..a196dd5 100644 --- a/frontend/lib/overview.ts +++ b/frontend/lib/overview.ts @@ -22,7 +22,7 @@ export const OVERVIEW_NAV_ITEMS: NavItem[] = [ { key: "options", label: "Options", icon: "window" }, { key: "insiders", label: "Insiders", icon: "pulse" }, { key: "filings", label: "Filings", icon: "folder" }, - { key: "news", label: "News", icon: "terminal", disabled: true } + { key: "news", label: "News", icon: "terminal" } ]; export function buildIdentityLine(overview: TickerOverview): string { |
