|
| 1 | +<script lang="ts"> |
| 2 | + import RotateCcw from '@lucide/svelte/icons/rotate-ccw'; |
| 3 | + import type { ColumnDef, SortingState } from '@tanstack/table-core'; |
| 4 | + import { page } from '$app/state'; |
| 5 | + import type { LogEntry } from '$lib/api/internal/v1'; |
| 6 | + import Container from '$lib/components/Container.svelte'; |
| 7 | + import { |
| 8 | + CreateColumnDef, |
| 9 | + CreateSortableColumnDef, |
| 10 | + LocaleDateTimeRenderer, |
| 11 | + NumberRenderer, |
| 12 | + RenderCell, |
| 13 | + } from '$lib/components/Table/ColumnUtils'; |
| 14 | + import DataTable from '$lib/components/Table/DataTableTemplate.svelte'; |
| 15 | + import Button from '$lib/components/ui/button/button.svelte'; |
| 16 | + import * as Card from '$lib/components/ui/card'; |
| 17 | + import { ShockerLogStore, fetchLogsForShocker } from '$lib/stores/ShockerLogStore'; |
| 18 | + import { onMount } from 'svelte'; |
| 19 | +
|
| 20 | + let data = $derived.by<LogEntry[]>(() => { |
| 21 | + const logMap = $ShockerLogStore; |
| 22 | + let allLogs: LogEntry[] = []; |
| 23 | + logMap.forEach((logs) => { |
| 24 | + allLogs = allLogs.concat(logs); |
| 25 | + }); |
| 26 | + return allLogs; |
| 27 | + }); |
| 28 | +
|
| 29 | + let sorting = $state<SortingState>([{ id: 'createdOn', desc: true }]); |
| 30 | +
|
| 31 | + const columns: ColumnDef<LogEntry>[] = [ |
| 32 | + CreateSortableColumnDef('createdOn', 'Time', LocaleDateTimeRenderer), |
| 33 | + CreateSortableColumnDef('type', 'Type', (t) => RenderCell(String(t))), |
| 34 | + CreateSortableColumnDef('controlledBy', 'By', (c) => RenderCell(c.customName ?? c.name)), |
| 35 | + CreateSortableColumnDef('intensity', 'Intensity', NumberRenderer), |
| 36 | + CreateSortableColumnDef('duration', 'Duration', NumberRenderer), |
| 37 | + ]; |
| 38 | +
|
| 39 | + let shockerId = page.params.shockerId ?? ''; |
| 40 | +
|
| 41 | + function refresh() { |
| 42 | + if (!shockerId) return; |
| 43 | + fetchLogsForShocker(shockerId); |
| 44 | + } |
| 45 | +
|
| 46 | + onMount(() => { |
| 47 | + if (shockerId) refresh(); |
| 48 | + }); |
| 49 | +</script> |
| 50 | + |
| 51 | +<Container> |
| 52 | + <Card.Header class="w-full"> |
| 53 | + <Card.Title class="flex items-center justify-between space-x-2 text-3xl"> |
| 54 | + <div class="flex items-center space-x-2"> |
| 55 | + <span>Shocker Logs</span> |
| 56 | + </div> |
| 57 | + <div class="flex items-center justify-end space-x-2"> |
| 58 | + <Button variant="ghost" aria-label="Refresh logs" onclick={refresh}> |
| 59 | + <RotateCcw /> |
| 60 | + </Button> |
| 61 | + </div> |
| 62 | + </Card.Title> |
| 63 | + <Card.Description>This is the logs for the specified shocker.</Card.Description> |
| 64 | + </Card.Header> |
| 65 | + <div class="w-full p-6 gap-6 grid"> |
| 66 | + <DataTable {data} {columns} bind:sorting /> |
| 67 | + </div> |
| 68 | +</Container> |
0 commit comments