flotilla/src/app/components/ThunkStatus.svelte

76 lines
2.5 KiB
Svelte
Raw Normal View History

2024-10-16 22:57:18 +00:00
<script lang="ts">
2024-11-12 22:32:36 +00:00
import {get} from "svelte/store"
2024-10-16 22:57:18 +00:00
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-31 19:24:40 +00:00
import {userSettingValues} from "@app/state"
2024-10-16 22:57:18 +00:00
2025-02-04 00:37:14 +00:00
interface Props {
thunk: Thunk | MergedThunk
}
let {thunk} = $props()
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-11-12 22:32:36 +00:00
let isPending = Object.values(get(thunk.status)).some(s => s.status == Pending)
2024-10-31 17:37:20 +00:00
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-31 17:37:20 +00:00
$: canCancel = ps.length === 0 && $userSettingValues.send_delay > 0
2024-10-17 18:27:17 +00:00
$: isFailure = !canCancel && ps.every(s => [Failure, Timeout].includes(s.status))
2024-10-17 15:53:03 +00:00
$: failure = Object.entries($status).find(([url, s]) => [Failure, Timeout].includes(s.status))
2024-10-31 17:37:20 +00:00
// Delay updating isPending so users can see that the message is sent
2024-10-31 17:37:20 +00:00
$: {
2024-12-12 22:49:30 +00:00
isPending = isPending || ps.some(s => s.status === Pending)
2024-12-12 22:49:30 +00:00
if (!ps.some(s => s.status === Pending)) {
2024-10-31 17:37:20 +00:00
setTimeout(() => {
isPending = false
}, 2000)
}
}
2024-10-16 22:57:18 +00:00
</script>
2025-01-15 01:23:27 +00:00
{#if isFailure && failure}
{@const [url, {message, status}] = failure}
<div class="flex justify-end px-1 text-xs {$$props.class}">
2024-10-23 16:45:06 +00:00
<Tippy
class="flex items-center {$$props.class}"
2024-10-23 16:45:06 +00:00
component={ThunkStatusDetail}
props={{url, message, status, retry}}
params={{interactive: true}}>
2025-02-03 23:40:00 +00:00
{#snippet children()}
<span class="flex cursor-pointer items-center gap-1 text-error">
<Icon icon="danger" size={3} />
<span>Failed to send!</span>
</span>
{/snippet}
2024-10-23 16:45:06 +00:00
</Tippy>
2025-01-15 01:23:27 +00:00
</div>
{:else if canCancel || isPending}
<div class="flex justify-end px-1 text-xs {$$props.class}">
2024-11-15 22:03:54 +00:00
<span class="flex items-center gap-1 {$$props.class}">
2025-02-03 23:01:42 +00:00
<span class="loading loading-spinner mx-1 h-3 w-3 translate-y-px"></span>
2024-11-12 22:32:36 +00:00
<span class="opacity-50">Sending...</span>
{#if canCancel}
<Button class="link" on:click={abort}>Cancel</Button>
{/if}
</span>
2025-01-15 01:23:27 +00:00
</div>
{/if}