flotilla/src/app/routes.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-08-16 17:50:38 +00:00
import type {Page} from "@sveltejs/kit"
2025-02-06 17:29:30 +00:00
import {identity} from "@welshman/lib"
2024-12-17 01:07:52 +00:00
import {makeChatId, decodeRelay, encodeRelay, userRoomsByUrl} from "@app/state"
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 makeChatPath = (pubkeys: string[]) => `/chat/${makeChatId(pubkeys)}`
2024-10-03 22:28:06 +00:00
2024-10-24 16:58:43 +00:00
export const makeRoomPath = (url: string, room: string) => `/spaces/${encodeRelay(url)}/${room}`
2024-10-23 23:41:11 +00:00
2025-02-06 17:29:30 +00:00
export const makeThreadPath = (url: string, eventId?: string) =>
makeSpacePath(url, "threads", eventId)
2024-12-16 18:30:42 +00:00
2025-02-06 17:29:30 +00:00
export const makeCalendarPath = (url: string, eventId?: string) =>
makeSpacePath(url, "calendar", eventId)
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) => {
2024-12-17 01:07:52 +00:00
const urls = Array.from(userRoomsByUrl.get().keys())
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": {
2024-10-21 17:30:12 +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
}
}