refactor: extract shared ContentLinkUrl renderer
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<script lang="ts">
|
||||
import type {Snippet} from "svelte"
|
||||
import {call, displayUrl} from "@welshman/lib"
|
||||
import {displayRelayUrl, isRelayUrl, normalizeRelayUrl} from "@welshman/util"
|
||||
import LinkRound from "@assets/icons/link-round.svg?dataurl"
|
||||
import Icon from "@lib/components/Icon.svelte"
|
||||
import Link from "@lib/components/Link.svelte"
|
||||
import {PLATFORM_URL, displayRoom, isRoomId, splitRoomId} from "@app/core/state"
|
||||
import {makeRoomPath, makeSpacePath} from "@app/util/routes"
|
||||
|
||||
const {
|
||||
children,
|
||||
url,
|
||||
class: className = "",
|
||||
showIcon = false,
|
||||
labelClass = "",
|
||||
}: {
|
||||
children?: Snippet
|
||||
url: string
|
||||
class?: string
|
||||
showIcon?: boolean
|
||||
labelClass?: string
|
||||
} = $props()
|
||||
|
||||
const roomReference = call(() => {
|
||||
if (!isRoomId(url)) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const [roomUrl, h] = splitRoomId(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 label = call(() => {
|
||||
if (roomReference) {
|
||||
const spaceName = displayRelayUrl(roomReference.url)
|
||||
const roomName = displayRoom(roomReference.url, roomReference.h)
|
||||
|
||||
return `~${spaceName} / ${roomName}`
|
||||
}
|
||||
|
||||
if (relayReference) {
|
||||
return `~${displayRelayUrl(relayReference)}`
|
||||
}
|
||||
|
||||
return displayUrl(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 children}
|
||||
{@render children()}
|
||||
{:else if showIcon}
|
||||
<Icon icon={LinkRound} size={3} class="inline-block" />
|
||||
<span class={labelClass}>{label}</span>
|
||||
{:else}
|
||||
{label}
|
||||
{/if}
|
||||
</Link>
|
||||
Reference in New Issue
Block a user