flotilla/src/app/components/ThunkStatus.svelte

52 lines
1.9 KiB
Svelte
Raw Normal View History

2024-10-16 22:57:18 +00:00
<script lang="ts">
import {displayRelayUrl} from "@welshman/util"
import {PublishStatus} from "@welshman/net"
2024-10-17 18:56:13 +00:00
import {mergeThunks, publishThunk} from "@welshman/app"
2024-10-17 18:27:17 +00:00
import type {Thunk, MergedThunk} from "@welshman/app"
2024-10-17 19:29:28 +00:00
import {throttled} from "@welshman/store"
2024-10-16 22:57:18 +00:00
import Icon from '@lib/components/Icon.svelte'
2024-10-17 18:56:13 +00:00
import Tippy from "@lib/components/Tippy.svelte"
2024-10-16 22:57:18 +00:00
import Button from '@lib/components/Button.svelte'
2024-10-17 18:56:13 +00:00
import ThunkStatusDetail from '@app/components/ThunkStatusDetail.svelte'
2024-10-16 22:57:18 +00:00
2024-10-17 18:27:17 +00:00
export let thunk: Thunk | MergedThunk
2024-10-16 22:57:18 +00:00
const {Pending, Success, Failure, Timeout} = PublishStatus
2024-10-17 18:27:17 +00:00
const abort = () => thunk.controller.abort()
2024-10-16 22:57:18 +00:00
2024-10-17 18:56:13 +00:00
const retry = () => {
thunk = (thunk as any).thunks
? mergeThunks((thunk as MergedThunk).thunks.map(t => publishThunk(t.request)))
: publishThunk((thunk as Thunk).request)
}
2024-10-17 19:29:28 +00:00
$: status = throttled(300, thunk.status)
2024-10-16 22:57:18 +00:00
$: ps = Object.values($status)
2024-10-17 18:27:17 +00:00
$: canCancel = ps.length === 0
$: isFailure = !canCancel && ps.every(s => [Failure, Timeout].includes(s.status))
2024-10-17 19:29:28 +00:00
$: isPending = !isFailure && ps.some(s => s.status === Pending)
2024-10-17 15:53:03 +00:00
$: failure = Object.entries($status).find(([url, s]) => [Failure, Timeout].includes(s.status))
2024-10-16 22:57:18 +00:00
</script>
2024-10-17 18:27:17 +00:00
{#if canCancel || isPending}
<span class="flex gap-1 mt-2 items-center">
2024-10-16 22:57:18 +00:00
<span class="loading loading-spinner mx-1 h-3 w-3 translate-y-px" />
<span class="opacity-50">Sending...</span>
2024-10-17 18:27:17 +00:00
{#if canCancel}
<Button class="link" on:click={abort}>Cancel</Button>
2024-10-16 22:57:18 +00:00
{/if}
</span>
{:else if isFailure && failure}
2024-10-17 18:56:13 +00:00
{@const [url, {message, status}] = failure}
<Tippy
component={ThunkStatusDetail}
props={{url, message, status, retry}}
params={{interactive: true}}>
<span class="flex tooltip cursor-pointer gap-1 mt-2 items-center">
<Icon icon="danger" size={3} />
<span class="opacity-50">Failed to send!</span>
</span>
</Tippy>
2024-10-16 22:57:18 +00:00
{/if}