flotilla/src/app/util/routes.ts

211 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-08-16 17:50:38 +00:00
import type {Page} from "@sveltejs/kit"
import {get} from "svelte/store"
2025-06-05 20:37:50 +00:00
import * as nip19 from "nostr-tools/nip19"
2025-06-05 17:55:16 +00:00
import {goto} from "$app/navigation"
2026-02-16 23:28:25 +00:00
import {page} from "$app/stores"
2026-02-16 22:45:54 +00:00
import {nthEq} from "@welshman/lib"
2025-06-05 17:55:16 +00:00
import type {TrustedEvent} from "@welshman/util"
2026-02-04 01:09:30 +00:00
import {getAddress} from "@welshman/util"
2026-01-06 22:52:13 +00:00
import {tracker, loadRelay} from "@welshman/app"
2025-02-06 17:29:30 +00:00
import {identity} from "@welshman/lib"
2025-06-05 17:55:16 +00:00
import {
getTagValue,
MESSAGE,
THREAD,
2026-02-03 19:00:13 +00:00
CLASSIFIED,
2025-07-07 22:28:36 +00:00
ZAP_GOAL,
2025-06-05 17:55:16 +00:00
EVENT_TIME,
getPubkeyTagValues,
2025-06-05 17:55:16 +00:00
} from "@welshman/util"
import {
makeChatId,
entityLink,
decodeRelay,
encodeRelay,
userSpaceUrls,
2025-10-06 18:23:19 +00:00
hasNip29,
2026-02-13 23:37:17 +00:00
DM_KINDS,
ROOM,
} from "@app/core/state"
import {lastPageBySpaceUrl, lastChatUrl} from "@app/util/history"
2025-02-06 17:29:30 +00:00
export const makeSpacePath = (url: string, ...extra: (string | undefined)[]) => {
2024-10-21 17:30:12 +00:00
let path = `/spaces/${encodeRelay(url)}`
2024-10-31 19:24:40 +00:00
if (extra.length > 0) {
2025-02-06 17:29:30 +00:00
path +=
"/" +
extra
.filter(identity)
.map(s => encodeURIComponent(s as string))
.join("/")
}
return path
}
2024-08-15 19:08:07 +00:00
export const goToSpace = async (url: string) => {
const prevPath = lastPageBySpaceUrl.get(encodeRelay(url))
if (prevPath) {
goto(prevPath)
} else if (hasNip29(await loadRelay(url))) {
goto(makeSpacePath(url, "recent"))
} else {
2025-11-05 01:25:50 +00:00
goto(makeSpacePath(url, "chat"))
}
}
export const goToLastChat = () => {
goto(lastChatUrl ?? "/chat")
}
2026-01-06 22:52:13 +00:00
export const makeChatPath = (pubkeys: string[]) => `/chat/${makeChatId(pubkeys)}`
2024-10-03 22:28:06 +00:00
2025-10-30 22:33:34 +00:00
export const makeRoomPath = (url: string, h: string) => `/spaces/${encodeRelay(url)}/${h}`
2024-10-23 23:41:11 +00:00
2025-06-25 00:36:14 +00:00
export const makeSpaceChatPath = (url: string) => makeRoomPath(url, "chat")
2026-02-16 22:45:54 +00:00
export const makeMessagePath = (url: string, event: TrustedEvent) => {
const h = getTagValue(ROOM, event.tags)
const path = h ? makeRoomPath(url, h) : makeSpaceChatPath(url)
const qp = new URLSearchParams({at: String(event.created_at)})
return path + "?" + qp.toString()
}
export const makeGoalPath = (url: string, id?: string) => makeSpacePath(url, "goals", id)
2025-07-07 22:28:36 +00:00
export const makeThreadPath = (url: string, id?: string) => makeSpacePath(url, "threads", id)
2024-12-16 18:30:42 +00:00
2026-02-04 01:09:30 +00:00
export const makeClassifiedPath = (url: string, address?: string) =>
makeSpacePath(url, "classifieds", address)
2026-02-03 19:00:13 +00:00
2026-02-04 01:09:30 +00:00
export const makeCalendarPath = (url: string, address?: string) =>
makeSpacePath(url, "calendar", address)
2024-10-23 21:20:24 +00:00
2024-08-16 18:12:14 +00:00
export const getPrimaryNavItem = ($page: Page) => $page.route?.id?.split("/")[1]
export const getPrimaryNavItemIndex = ($page: Page) => {
const urls = get(userSpaceUrls)
switch (getPrimaryNavItem($page)) {
2024-08-16 17:50:38 +00:00
case "discover":
return urls.length + 2
2024-09-11 18:09:25 +00:00
case "spaces": {
2025-10-13 20:35:02 +00:00
const routeUrl = decodeRelay($page.params.relay || "")
return urls.findIndex(url => url === routeUrl) + 1
2024-09-11 18:09:25 +00:00
}
2024-08-16 17:50:38 +00:00
case "settings":
return urls.length + 3
default:
return 0
}
}
2025-06-05 17:55:16 +00:00
2026-02-16 23:28:25 +00:00
export const scrollToEvent = (id: string) => {
const element = document.querySelector(`[data-event="${id}"]`) as any
if (element) {
element.scrollIntoView({behavior: "smooth", block: "center"})
element.style = "filter: brightness(1.5); transition-property: all; transition-duration: 400ms;"
setTimeout(() => {
element.style = "transition-property: all; transition-duration: 300ms;"
}, 800)
setTimeout(() => {
element.style = ""
}, 800 + 400)
}
return Boolean(element)
}
2026-02-16 22:45:54 +00:00
export const goToEvent = (event: TrustedEvent, options: Record<string, any> = {}) => {
const urls = Array.from(tracker.getRelays(event.id))
2026-02-16 22:45:54 +00:00
const path = getEventPath(event, urls)
2025-06-05 17:55:16 +00:00
if (path.includes("://")) {
window.open(path)
2026-02-16 23:28:25 +00:00
} else if (!scrollToEvent(event.id)) {
const replaceState = path.replace(/\?.*$/, "") === get(page).url.pathname
goto(path, {replaceState, ...options})
2025-06-19 17:22:17 +00:00
}
}
2026-02-16 22:45:54 +00:00
export const getEventPath = (event: TrustedEvent, urls: string[]) => {
2026-02-13 23:37:17 +00:00
if (DM_KINDS.includes(event.kind)) {
return makeChatPath([event.pubkey, ...getPubkeyTagValues(event.tags)])
}
2025-06-05 20:37:50 +00:00
if (urls.length > 0) {
const url = urls[0]
2025-07-07 22:28:36 +00:00
if (event.kind === ZAP_GOAL) {
return makeGoalPath(url, event.id)
}
2025-06-05 17:55:16 +00:00
if (event.kind === THREAD) {
2025-06-19 17:22:17 +00:00
return makeThreadPath(url, event.id)
2025-06-05 17:55:16 +00:00
}
2026-02-03 19:00:13 +00:00
if (event.kind === CLASSIFIED) {
return makeClassifiedPath(url, getAddress(event))
2026-02-03 19:00:13 +00:00
}
2025-06-05 17:55:16 +00:00
if (event.kind === EVENT_TIME) {
return makeCalendarPath(url, getAddress(event))
2025-06-05 17:55:16 +00:00
}
if (event.kind === MESSAGE) {
2026-02-16 22:45:54 +00:00
return makeMessagePath(url, event)
2025-06-05 17:55:16 +00:00
}
const address = event.tags.find(nthEq(0, "A"))?.[1]
2025-06-05 17:55:16 +00:00
const kind = event.tags.find(nthEq(0, "K"))?.[1]
const id = event.tags.find(nthEq(0, "E"))?.[1]
if (id && kind) {
2025-07-07 22:28:36 +00:00
if (parseInt(kind) === ZAP_GOAL) {
return makeGoalPath(url, id)
}
2025-06-05 17:55:16 +00:00
if (parseInt(kind) === THREAD) {
2025-06-19 17:22:17 +00:00
return makeThreadPath(url, id)
2025-06-05 17:55:16 +00:00
}
if (parseInt(kind) === MESSAGE) {
2026-02-16 22:45:54 +00:00
return makeMessagePath(url, event)
}
}
if (address && kind) {
2026-02-03 19:00:13 +00:00
if (parseInt(kind) === CLASSIFIED) {
return makeClassifiedPath(url, address)
2026-02-03 19:00:13 +00:00
}
2025-06-05 17:55:16 +00:00
if (parseInt(kind) === EVENT_TIME) {
return makeCalendarPath(url, address)
2025-06-05 17:55:16 +00:00
}
}
}
2025-06-05 20:37:50 +00:00
2025-06-19 17:22:17 +00:00
return entityLink(nip19.neventEncode({id: event.id, relays: urls}))
2025-06-05 17:55:16 +00:00
}
2025-10-06 18:23:19 +00:00
2025-10-30 22:36:14 +00:00
export const getRoomItemPath = (url: string, event: TrustedEvent) => {
2025-10-06 18:23:19 +00:00
switch (event.kind) {
case THREAD:
return makeThreadPath(url, event.id)
2026-02-03 19:00:13 +00:00
case CLASSIFIED:
return makeClassifiedPath(url, getAddress(event))
2025-10-06 18:23:19 +00:00
case ZAP_GOAL:
return makeGoalPath(url, event.id)
case EVENT_TIME:
return makeCalendarPath(url, getAddress(event))
2025-10-06 18:23:19 +00:00
}
}