flotilla/src/app/components/SocketStatusToast.svelte

51 lines
1.1 KiB
Svelte
Raw Normal View History

<script lang="ts">
import {onDestroy} from "svelte"
import SocketStatusIndicator from "@app/components/SocketStatusIndicator.svelte"
import {deriveSocketStatus} from "@app/relays"
import {pushToast, popToast} from "@app/toast"
type Props = {
url: string
}
const {url}: Props = $props()
const status = deriveSocketStatus(url)
let toastId: string | undefined
let timeout: ReturnType<typeof setTimeout> | undefined
const hide = () => {
clearTimeout(timeout)
timeout = undefined
if (toastId) {
popToast(toastId)
toastId = undefined
}
}
$effect(() => {
if ($status.theme !== "warning") {
hide()
return
}
2026-09-21 16:48:35 +00:00
if (toastId || timeout) {
return
}
// deriveSocketStatus is throttled, so a socket that opens immediately still reports
// "Connecting" for the length of the throttle. Wait that out instead of flashing.
timeout = setTimeout(() => {
timeout = undefined
toastId = pushToast({
timeout: 60_000,
children: {component: SocketStatusIndicator, props: {url}},
})
}, 1000)
})
onDestroy(hide)
</script>