feat: improve connection handling

This commit is contained in:
2025-11-30 18:19:03 +01:00
parent eaef1f5302
commit 7d00d1b314
5 changed files with 199 additions and 132 deletions

View File

@@ -0,0 +1,13 @@
<script lang="ts">
import { browser } from "$app/environment";
import * as AlertDialog from "$lib/components/ui/alert-dialog/index.js";
import { onMount } from "svelte";
onMount(() => {
if (!browser) return;
setTimeout(() => {
location.reload();
}, 2000);
});
</script>

View File

@@ -1,49 +1,42 @@
<script>
import Spinner from '$lib/Spinner.svelte';
import { connectWS, wsState } from '$lib/ws.svelte';
import { onMount } from 'svelte';
import '../../app.css';
import { browser } from '$app/environment';
import ConnectionLostDialog from '$lib/ConnectionLostDialog.svelte';
let { children } = $props();
onMount(() => {
if(!browser) return;
import { connectWS, wsState } from "$lib/ws.svelte";
import { onMount } from "svelte";
import "../../app.css";
import { browser } from "$app/environment";
import ConnectionLostDialog2 from "$lib/ConnectionLostDialog2.svelte";
connectWS();
console.log("Connecting to WebSocket...");
})
let { children } = $props();
onMount(() => {
if (!browser) return;
connectWS();
console.log("Connecting to WebSocket...");
});
</script>
{#if wsState.closed}
<ConnectionLostDialog />
<ConnectionLostDialog2 />
{/if}
{#if !wsState.connected && !wsState.closed}
<div id="blocker">
<Spinner />
<h2 class="text-2xl">Verbindung herstellen...</h2>
</div>
{/if}
{#if !wsState.connected && !wsState.closed}{/if}
{@render children?.()}
<style>
#blocker {
position: fixed;
z-index: 9999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
backdrop-filter: blur(5px);
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 1rem;
}
#blocker {
position: fixed;
z-index: 9999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
backdrop-filter: blur(5px);
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 1rem;
}
</style>

View File

@@ -1,115 +1,164 @@
<script lang="ts">
import { eventTarget } from "$lib/ws.svelte";
import { onMount } from "svelte";
import { fade } from "svelte/transition";
import { eventTarget, wsState } from "$lib/ws.svelte";
import { PersistedState } from "runed";
import { onMount } from "svelte";
import { fade } from "svelte/transition";
interface CallEntry {
num: string;
ticket: {
status: "called" | "completed" | "no-show";
room?: string;
}
};
interface CallEntry {
num: string;
ticket: {
status: "called" | "completed" | "no-show";
room?: string;
};
}
let calls = $state<CallEntry[]>([]);
let newCalls = $state<CallEntry[]>([]);
let persisted: PersistedState<CallEntry[]> = new PersistedState("calls", []);
let calls = $state<CallEntry[]>($state.snapshot(persisted.current));
let newCalls = $state<CallEntry[]>([]);
let firstLoad = true;
let connected = $state(false);
let connected = $state(false);
onMount(() => {
eventTarget.addEventListener("display", (e) => {
const detail = (e as CustomEvent).detail;
const _newCalls = detail.tickets.filter((call: CallEntry) => !calls.find(c => c.num === call.num));
newCalls.push(..._newCalls);
calls = detail.tickets ?? [];
connected = true;
setTimeout(() => {
newCalls = newCalls.filter(c => _newCalls.includes(c));
}, 10000);
});
onMount(() => {
eventTarget.addEventListener("display", (e) => {
const detail = (e as CustomEvent).detail;
const _newCalls = detail.tickets.filter(
(call: CallEntry) => !calls.find((c) => c.num === call.num)
);
calls = detail.tickets ?? [];
persisted.current = calls;
connected = true;
if (!firstLoad) {
newCalls.push(..._newCalls);
setTimeout(() => {
newCalls = newCalls.filter((c) => _newCalls.includes(c));
}, 10000);
}
firstLoad = false;
});
eventTarget.dispatchEvent(new CustomEvent("send", { detail: { type: "display" } }));
})
const ENTRIES = {A: 1, B: 2, C: 1, D: 2, E: 1};
eventTarget.dispatchEvent(
new CustomEvent("send", { detail: { type: "display" } })
);
});
const ENTRIES_PER_TABLE = 10;
const ENTRIES: Record<string, number> = { A: 1, B: 2, C: 1, D: 2, E: 1 };
const ENTRIES_PER_TABLE = 10;
</script>
{#if newCalls.length > 0}
<div class="fixed top-0 left-0 w-full h-full backdrop-blur-md z-50" transition:fade>
<div class="fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-9xl font-bold bg-background text-foreground p-4 rounded-md flex flex-col gap-10">
{#each newCalls as call (call.num)}
<span class="mx-4">{call.num} &rarr; {call.ticket.room}</span>
{/each}
</div>
</div>
<div
class="fixed top-0 left-0 w-full h-full backdrop-blur-md z-50"
transition:fade
>
<div
class="fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-9xl font-bold bg-background text-foreground p-4 rounded-md flex flex-col gap-10"
>
{#each newCalls as call (call.num)}
<span class="mx-4">{call.num} &rarr; {call.ticket.room}</span>
{/each}
</div>
</div>
{/if}
<div class="p-4 flex justify-around gap-4 text-5xl">
<!-- <h1 class="text-6xl font-bold">Aufruf</h1> -->
{#if connected}
<table class="self-start">
<tbody>
<tr>
<th class="pr-4 font-bold">Wartenr.</th>
<th class="pr-4 font-bold">&rarr;</th>
<th class="pr-4 font-bold">Raum</th>
</tr>
{#each calls.slice(0, ENTRIES_PER_TABLE) as call (call.num)}
<tr>
<td class="call">{call.num}</td>
<td class="call">&rarr;</td>
<td class="call">{call.ticket.status === "no-show" ? "Empfang " + ENTRIES[call.num[0]] : call.ticket.room}</td>
</tr>
{/each}
</tbody>
</table>
<!-- <h1 class="text-6xl font-bold">Aufruf</h1> -->
<table class="self-start">
<tbody>
<tr>
<th class="pr-4 font-bold">Wartenr.</th>
<th class="pr-4 font-bold">&rarr;</th>
<th class="pr-4 font-bold">Raum</th>
</tr>
{#each calls.slice(0, ENTRIES_PER_TABLE) as call (call.num)}
<tr>
<td class="call">{call.num}</td>
<td class="call">&rarr;</td>
<td class="call"
>{call.ticket.status === "no-show"
? "Empfang " + ENTRIES[call.num[0]]
: call.ticket.room}</td
>
</tr>
{/each}
</tbody>
</table>
<div class="fixed h-full w-1 bg-black top-0 left-1/2 -translate-x-1/2"></div>
<div
class="fixed h-full w-1 top-0 left-1/2 -translate-x-1/2 {wsState.closed
? 'bg-red-800'
: !wsState.connected
? 'bg-amber-800'
: 'bg-black'}"
></div>
<table class="self-start">
<tbody>
<tr>
<th class="pr-4 font-bold">Wartenr.</th>
<th class="pr-4 font-bold">&rarr;</th>
<th class="pr-4 font-bold">Raum</th>
</tr>
{#each calls.slice(ENTRIES_PER_TABLE) as call (call.num)}
<tr>
<td class="call">{call.num}</td>
<td class="call">&rarr;</td>
<td class="call">{call.ticket.status === "no-show" ? "Empfang " + ENTRIES[call.num[0]] : call.ticket.room}</td>
</tr>
{/each}
</tbody>
</table>
{/if}
<table class="self-start">
<tbody>
<tr>
<th class="pr-4 font-bold">Wartenr.</th>
<th class="pr-4 font-bold">&rarr;</th>
<th class="pr-4 font-bold">Raum</th>
</tr>
{#each calls.slice(ENTRIES_PER_TABLE) as call (call.num)}
<tr>
<td class="call">{call.num}</td>
<td class="call">&rarr;</td>
<td class="call"
>{call.ticket.status === "no-show"
? "Empfang " + ENTRIES[call.num[0]]
: call.ticket.room}</td
>
</tr>
{/each}
</tbody>
</table>
</div>
<style>
.call {
animation: flash 5s ease-in-out;
animation-delay: 10s;
}
.call {
animation: flash 5s ease-in-out;
animation-delay: 10s;
}
@keyframes flash {
0% { opacity: 0; }
10% { opacity: 1; }
20% { opacity: 0; }
30% { opacity: 1; }
40% { opacity: 0; }
50% { opacity: 1; }
60% { opacity: 0; }
70% { opacity: 1; }
80% { opacity: 0; }
100% { opacity: 1; }
}
@keyframes flash {
0% {
opacity: 0;
}
10% {
opacity: 1;
}
20% {
opacity: 0;
}
30% {
opacity: 1;
}
40% {
opacity: 0;
}
50% {
opacity: 1;
}
60% {
opacity: 0;
}
70% {
opacity: 1;
}
80% {
opacity: 0;
}
100% {
opacity: 1;
}
}
th, td {
padding: 0.5rem 1rem;
text-align: center;
background-color: var(--accent);
border: 2px solid var(--background);
}
th,
td {
padding: 0.5rem 1rem;
text-align: center;
background-color: var(--accent);
border: 2px solid var(--background);
}
</style>