flotilla/src/app/components/ThunkStatus.svelte

53 lines
1.9 KiB
Svelte
Raw Normal View History

2024-10-16 22:57:18 +00:00
<script lang="ts">
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-17 23:03:50 +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-17 23:03:50 +00:00
import Button from "@lib/components/Button.svelte"
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
2024-10-17 23:03:50 +00:00
const {Pending, Failure, Timeout} = PublishStatus
2024-10-16 22:57:18 +00:00
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-25 00:25:28 +00:00
<div class="flex justify-end text-xs px-1">
2024-10-23 16:45:06 +00:00
{#if canCancel || isPending}
<span class="mt-2 flex items-center gap-1">
<span class="loading loading-spinner mx-1 h-3 w-3 translate-y-px" />
<span class="opacity-50">Sending...</span>
{#if canCancel}
<Button class="link" on:click={abort}>Cancel</Button>
{/if}
2024-10-17 18:56:13 +00:00
</span>
2024-10-23 16:45:06 +00:00
{:else if isFailure && failure}
{@const [url, {message, status}] = failure}
<Tippy
component={ThunkStatusDetail}
props={{url, message, status, retry}}
params={{interactive: true}}>
<span class="tooltip mt-2 flex cursor-pointer items-center gap-1">
<Icon icon="danger" size={3} />
<span class="opacity-50">Failed to send!</span>
</span>
</Tippy>
{/if}
</div>