| 1 | import type { PreviewSessionSnapshot } from "@t3tools/contracts"; |
| 2 | |
| 3 | /** The tab the agent touched last, the default when nothing is selected. */ |
| 4 | export function latestBrowserTab(tabs: ReadonlyArray<PreviewSessionSnapshot>) { |
| 5 | return tabs.reduce<PreviewSessionSnapshot | null>( |
| 6 | (latest, tab) => (latest === null || tab.updatedAt > latest.updatedAt ? tab : latest), |
| 7 | null, |
| 8 | ); |
| 9 | } |
| 10 | |
| 11 | export function browserTabUrl(tab: PreviewSessionSnapshot) { |
| 12 | return tab.navStatus._tag === "Idle" ? "" : tab.navStatus.url; |
| 13 | } |
| 14 | |
| 15 | export function browserTabTitle(tab: PreviewSessionSnapshot) { |
| 16 | if (tab.navStatus._tag === "Idle") return "New tab"; |
| 17 | if (tab.navStatus.title.trim()) return tab.navStatus.title; |
| 18 | try { |
| 19 | return new URL(tab.navStatus.url).host || tab.navStatus.url; |
| 20 | } catch { |
| 21 | return tab.navStatus.url; |
| 22 | } |
| 23 | } |