forked from coracle/flotilla
Generalize goToMessage
This commit is contained in:
@@ -1,26 +1,14 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import * as nip19 from "nostr-tools/nip19"
|
import * as nip19 from "nostr-tools/nip19"
|
||||||
import {goto} from "$app/navigation"
|
|
||||||
import {nthEq} from "@welshman/lib"
|
|
||||||
import {Router} from "@welshman/router"
|
import {Router} from "@welshman/router"
|
||||||
import {tracker, repository} from "@welshman/app"
|
|
||||||
import type {TrustedEvent} from "@welshman/util"
|
import type {TrustedEvent} from "@welshman/util"
|
||||||
import {
|
import {Address, MESSAGE} from "@welshman/util"
|
||||||
Address,
|
|
||||||
getTagValue,
|
|
||||||
DIRECT_MESSAGE,
|
|
||||||
DIRECT_MESSAGE_FILE,
|
|
||||||
MESSAGE,
|
|
||||||
THREAD,
|
|
||||||
EVENT_TIME,
|
|
||||||
} from "@welshman/util"
|
|
||||||
import {scrollToEvent} from "@lib/html"
|
|
||||||
import Button from "@lib/components/Button.svelte"
|
import Button from "@lib/components/Button.svelte"
|
||||||
import Spinner from "@lib/components/Spinner.svelte"
|
import Spinner from "@lib/components/Spinner.svelte"
|
||||||
import NoteCard from "@app/components/NoteCard.svelte"
|
import NoteCard from "@app/components/NoteCard.svelte"
|
||||||
import NoteContent from "@app/components/NoteContent.svelte"
|
import NoteContent from "@app/components/NoteContent.svelte"
|
||||||
import {deriveEvent, entityLink, ROOM} from "@app/state"
|
import {deriveEvent, entityLink} from "@app/state"
|
||||||
import {makeThreadPath, makeSpacePath, makeCalendarPath, makeRoomPath} from "@app/routes"
|
import {goToEvent} from "@app/routes"
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
value: any
|
value: any
|
||||||
@@ -46,60 +34,12 @@
|
|||||||
? nip19.neventEncode({id, relays: mergedRelays})
|
? nip19.neventEncode({id, relays: mergedRelays})
|
||||||
: new Address(kind, pubkey, identifier, mergedRelays).toNaddr()
|
: new Address(kind, pubkey, identifier, mergedRelays).toNaddr()
|
||||||
|
|
||||||
const openMessage = (url: string, room: string | undefined, id: string) => {
|
|
||||||
const event = repository.getEvent(id)
|
|
||||||
const path = room ? makeRoomPath(url, room) : makeSpacePath(url, "chat")
|
|
||||||
|
|
||||||
if (event) {
|
|
||||||
goto(path)
|
|
||||||
scrollToEvent(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Boolean(event)
|
|
||||||
}
|
|
||||||
|
|
||||||
const onclick = () => {
|
const onclick = () => {
|
||||||
if ($quote) {
|
if ($quote) {
|
||||||
if ($quote.kind === DIRECT_MESSAGE || $quote.kind === DIRECT_MESSAGE_FILE) {
|
goToEvent($quote)
|
||||||
return scrollToEvent($quote.id)
|
} else {
|
||||||
}
|
window.open(entityLink(entity))
|
||||||
|
|
||||||
const [url] = tracker.getRelays($quote.id)
|
|
||||||
const room = getTagValue(ROOM, $quote.tags)
|
|
||||||
|
|
||||||
if (url) {
|
|
||||||
if ($quote.kind === THREAD) {
|
|
||||||
return goto(makeThreadPath(url, $quote.id))
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($quote.kind === EVENT_TIME) {
|
|
||||||
return goto(makeCalendarPath(url, $quote.id))
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($quote.kind === MESSAGE) {
|
|
||||||
return scrollToEvent($quote.id) || openMessage(url, room, $quote.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
const kind = $quote.tags.find(nthEq(0, "K"))?.[1]
|
|
||||||
const id = $quote.tags.find(nthEq(0, "E"))?.[1]
|
|
||||||
|
|
||||||
if (id && kind) {
|
|
||||||
if (parseInt(kind) === THREAD) {
|
|
||||||
return goto(makeThreadPath(url, id))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parseInt(kind) === EVENT_TIME) {
|
|
||||||
return goto(makeCalendarPath(url, id))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parseInt(kind) === MESSAGE) {
|
|
||||||
return scrollToEvent(id) || openMessage(url, room, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.open(entityLink(entity))
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
import ProfileCircle from "@app/components/ProfileCircle.svelte"
|
import ProfileCircle from "@app/components/ProfileCircle.svelte"
|
||||||
import ProfileCircles from "@app/components/ProfileCircles.svelte"
|
import ProfileCircles from "@app/components/ProfileCircles.svelte"
|
||||||
import {deriveEventsForUrl} from "@app/state"
|
import {deriveEventsForUrl} from "@app/state"
|
||||||
|
import {goToEvent} from "@app/routes"
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
url: string
|
url: string
|
||||||
@@ -80,7 +81,7 @@
|
|||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
{#each $conversations.slice(0, limit) as { room, events, latest, earliest, participants } (latest.id)}
|
{#each $conversations.slice(0, limit) as { room, events, latest, earliest, participants } (latest.id)}
|
||||||
<div class="card2 bg-alt">
|
<Button class="card2 bg-alt" onclick={() => goToEvent(earliest)}>
|
||||||
<div class="flex flex-col gap-3">
|
<div class="flex flex-col gap-3">
|
||||||
<div class="flex items-start gap-3">
|
<div class="flex items-start gap-3">
|
||||||
<ProfileCircle pubkey={earliest.pubkey} size={10} />
|
<ProfileCircle pubkey={earliest.pubkey} size={10} />
|
||||||
@@ -94,30 +95,35 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="ml-13 flex items-center justify-between">
|
<div class="ml-13 flex items-center justify-between">
|
||||||
<div class="flex items-center gap-4">
|
<div class="flex gap-1">
|
||||||
<span class="flex items-center gap-2 text-sm opacity-70">
|
<Icon icon="alt-arrow-left" />
|
||||||
<Icon icon="alt-arrow-left" size={4} />
|
<span class="text-sm opacity-70">
|
||||||
{events.length} messages
|
{events.length} messages
|
||||||
</span>
|
</span>
|
||||||
<div class="flex -space-x-2">
|
|
||||||
<ProfileCircles pubkeys={participants} size={6} />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<span class="text-sm opacity-50">
|
<div class="flex gap-2">
|
||||||
{formatTimestamp(latest.created_at)}
|
<ProfileCircles pubkeys={participants} size={6} />
|
||||||
</span>
|
<span class="text-sm opacity-70">
|
||||||
|
{participants.length} participants
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="card2 bg-alt">
|
<Button class="card2 bg-alt" onclick={() => goToEvent(latest)}>
|
||||||
<div class="flex flex-col gap-2">
|
<div class="flex flex-col gap-2">
|
||||||
<div class="flex items-center gap-2 text-sm opacity-70">
|
<div class="flex items-center justify-between">
|
||||||
<ProfileCircle pubkey={latest.pubkey} size={5} />
|
<div class="flex items-center gap-2 text-sm opacity-70">
|
||||||
<span class="font-medium">Latest reply:</span>
|
<ProfileCircle pubkey={latest.pubkey} size={5} />
|
||||||
|
<span class="font-medium">Latest reply:</span>
|
||||||
|
</div>
|
||||||
|
<span class="text-xs opacity-50">
|
||||||
|
{formatTimestamp(latest.created_at)}
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<Content minimalQuote minLength={100} maxLength={200} event={latest} />
|
<Content minimalQuote minLength={100} maxLength={200} event={latest} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</Button>
|
||||||
{/each}
|
{/each}
|
||||||
{#if $conversations.length > limit}
|
{#if $conversations.length > limit}
|
||||||
<Button class="btn btn-primary" onclick={viewMore}>
|
<Button class="btn btn-primary" onclick={viewMore}>
|
||||||
|
|||||||
+61
-1
@@ -1,6 +1,19 @@
|
|||||||
import type {Page} from "@sveltejs/kit"
|
import type {Page} from "@sveltejs/kit"
|
||||||
|
import {goto} from "$app/navigation"
|
||||||
|
import {nthEq, sleep} from "@welshman/lib"
|
||||||
|
import type {TrustedEvent} from "@welshman/util"
|
||||||
|
import {tracker} from "@welshman/app"
|
||||||
|
import {scrollToEvent} from "@lib/html"
|
||||||
import {identity} from "@welshman/lib"
|
import {identity} from "@welshman/lib"
|
||||||
import {makeChatId, decodeRelay, encodeRelay, userRoomsByUrl} from "@app/state"
|
import {
|
||||||
|
getTagValue,
|
||||||
|
DIRECT_MESSAGE,
|
||||||
|
DIRECT_MESSAGE_FILE,
|
||||||
|
MESSAGE,
|
||||||
|
THREAD,
|
||||||
|
EVENT_TIME,
|
||||||
|
} from "@welshman/util"
|
||||||
|
import {makeChatId, decodeRelay, encodeRelay, userRoomsByUrl, ROOM} from "@app/state"
|
||||||
|
|
||||||
export const makeSpacePath = (url: string, ...extra: (string | undefined)[]) => {
|
export const makeSpacePath = (url: string, ...extra: (string | undefined)[]) => {
|
||||||
let path = `/spaces/${encodeRelay(url)}`
|
let path = `/spaces/${encodeRelay(url)}`
|
||||||
@@ -46,3 +59,50 @@ export const getPrimaryNavItemIndex = ($page: Page) => {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const goToMessage = async (url: string, room: string | undefined, id: string) => {
|
||||||
|
await goto(room ? makeRoomPath(url, room) : makeSpacePath(url, "chat"))
|
||||||
|
await sleep(300)
|
||||||
|
|
||||||
|
return scrollToEvent(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const goToEvent = async (event: TrustedEvent) => {
|
||||||
|
if (event.kind === DIRECT_MESSAGE || event.kind === DIRECT_MESSAGE_FILE) {
|
||||||
|
return await scrollToEvent(event.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
const [url] = tracker.getRelays(event.id)
|
||||||
|
const room = getTagValue(ROOM, event.tags)
|
||||||
|
|
||||||
|
if (url) {
|
||||||
|
if (event.kind === THREAD) {
|
||||||
|
return goto(makeThreadPath(url, event.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.kind === EVENT_TIME) {
|
||||||
|
return goto(makeCalendarPath(url, event.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.kind === MESSAGE) {
|
||||||
|
return goToMessage(url, room, event.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
const kind = event.tags.find(nthEq(0, "K"))?.[1]
|
||||||
|
const id = event.tags.find(nthEq(0, "E"))?.[1]
|
||||||
|
|
||||||
|
if (id && kind) {
|
||||||
|
if (parseInt(kind) === THREAD) {
|
||||||
|
return goto(makeThreadPath(url, id))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parseInt(kind) === EVENT_TIME) {
|
||||||
|
return goto(makeCalendarPath(url, id))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parseInt(kind) === MESSAGE) {
|
||||||
|
return goToMessage(url, room, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+8
-2
@@ -100,7 +100,7 @@ export const isIntersecting = async (element: Element) =>
|
|||||||
observer.observe(element)
|
observer.observe(element)
|
||||||
})
|
})
|
||||||
|
|
||||||
export const scrollToEvent = async (id: string) => {
|
export const scrollToEvent = async (id: string, attempts = 3): Promise<boolean> => {
|
||||||
const element = document.querySelector(`[data-event="${id}"]`) as any
|
const element = document.querySelector(`[data-event="${id}"]`) as any
|
||||||
|
|
||||||
if (element) {
|
if (element) {
|
||||||
@@ -114,6 +114,8 @@ export const scrollToEvent = async (id: string) => {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
element.style = ""
|
element.style = ""
|
||||||
}, 800 + 400)
|
}, 800 + 400)
|
||||||
|
|
||||||
|
return true
|
||||||
} else {
|
} else {
|
||||||
const lastElement = last(Array.from(document.querySelectorAll("[data-event]")))
|
const lastElement = last(Array.from(document.querySelectorAll("[data-event]")))
|
||||||
|
|
||||||
@@ -123,6 +125,10 @@ export const scrollToEvent = async (id: string) => {
|
|||||||
|
|
||||||
await sleep(300)
|
await sleep(300)
|
||||||
|
|
||||||
scrollToEvent(id)
|
if (attempts > 0) {
|
||||||
|
return scrollToEvent(id, attempts - 1)
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user