flotilla/src/app/components/ContentLinkUrl.svelte

61 lines
1.7 KiB
Svelte
Raw Normal View History

<script lang="ts">
import {call, displayUrl} from "@welshman/lib"
import {displayRelayUrl, isRelayUrl, normalizeRelayUrl} from "@welshman/util"
2026-07-28 16:16:26 +00:00
import {splitRoomKey} from "@welshman/app"
import LinkRound from "@assets/icons/link-round.svg?dataurl"
import Icon from "@lib/components/Icon.svelte"
import Link from "@lib/components/Link.svelte"
2026-07-06 19:55:03 +00:00
import RoomName from "@app/components/RoomName.svelte"
import {PLATFORM_URL} from "@app/env"
2026-07-28 16:16:26 +00:00
import {isRoomId} from "@app/rooms"
2026-06-08 21:14:40 +00:00
import {makeRoomPath, makeSpacePath} from "@app/routes"
2026-07-28 16:16:26 +00:00
type Props = {
url: string
class?: string
2026-07-28 16:16:26 +00:00
}
const {url, class: className = ""}: Props = $props()
const roomReference = call(() => {
if (!isRoomId(url)) {
return undefined
}
2026-07-28 16:16:26 +00:00
const [roomUrl, h] = splitRoomKey(url)
if (!roomUrl || !h || !isRelayUrl(roomUrl)) {
return undefined
}
return {url: normalizeRelayUrl(roomUrl), h}
})
const relayReference = call(() => {
if (roomReference || !isRelayUrl(url)) {
return undefined
}
return normalizeRelayUrl(url)
})
const [href, external] = call(() => {
if (roomReference) return [makeRoomPath(roomReference.url, roomReference.h), false]
if (relayReference) return [makeSpacePath(relayReference), false]
if (url.startsWith(PLATFORM_URL)) return [url.replace(PLATFORM_URL, ""), false]
return [url, true]
})
</script>
<Link {external} {href} class={className}>
{#if roomReference}
2026-07-06 19:55:03 +00:00
#<RoomName {...roomReference} />
{:else if relayReference}
<span class="text-primary">{displayRelayUrl(relayReference)}</span>
{:else}
<Icon icon={LinkRound} size={3} class="inline-block" />
{displayUrl(url)}
{/if}
</Link>