|
| 1 | +<script> |
| 2 | + import { _ } from 'svelte-i18n'; |
| 3 | + let { historicalData } = $props(); |
| 4 | +
|
| 5 | + let dataArray = $derived(historicalData); // Assume data is an array of objects |
| 6 | +
|
| 7 | + // Filter data for current day |
| 8 | + function getCurrentDayData(dataArray) { |
| 9 | + const today = new Date(); |
| 10 | + today.setHours(0, 0, 0, 0); |
| 11 | +
|
| 12 | + return dataArray.filter((item) => { |
| 13 | + const itemDate = new Date(item.created_at); |
| 14 | + itemDate.setHours(0, 0, 0, 0); |
| 15 | + return itemDate.getTime() === today.getTime(); |
| 16 | + }); |
| 17 | + } |
| 18 | +
|
| 19 | + // Get visible columns |
| 20 | + function getVisibleColumns(dataArray) { |
| 21 | + const excludedColumns = ['is_simulated', 'dev_eui', 'smoke_detected', 'id', 'vape_detected']; |
| 22 | + const allKeys = dataArray.length > 0 ? Object.keys(dataArray[0]) : []; |
| 23 | +
|
| 24 | + return allKeys.filter((key) => { |
| 25 | + if (excludedColumns.includes(key)) return false; |
| 26 | + const hasNonNullValue = dataArray.some((item) => item[key] !== null); |
| 27 | + return hasNonNullValue; |
| 28 | + }); |
| 29 | + } |
| 30 | +
|
| 31 | + // Format column names |
| 32 | + function formatColumnName(key) { |
| 33 | + return key |
| 34 | + .split('_') |
| 35 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 36 | + .join(' '); |
| 37 | + } |
| 38 | +
|
| 39 | + // Format cell value |
| 40 | + function formatCellValue(key, value) { |
| 41 | + if (key === 'created_at') { |
| 42 | + return new Date(value).toLocaleString(); |
| 43 | + } |
| 44 | + if (typeof value === 'boolean') { |
| 45 | + return value ? 'Yes' : 'No'; |
| 46 | + } |
| 47 | + if (typeof value === 'number') { |
| 48 | + return value.toFixed(2); |
| 49 | + } |
| 50 | + return value; |
| 51 | + } |
| 52 | +
|
| 53 | + let filteredData = $derived(getCurrentDayData(dataArray)); |
| 54 | + let visibleColumns = $derived(getVisibleColumns(filteredData)); |
| 55 | +</script> |
| 56 | + |
| 57 | +<div class="min-h-screen w-full text-gray-900"> |
| 58 | + <div class="mx-auto w-full p-4"> |
| 59 | + <div |
| 60 | + class="overflow-hidden rounded-lg border border-gray-200/70 bg-white shadow-lg dark:border-white/10 dark:bg-neutral-900" |
| 61 | + > |
| 62 | + <!-- Header --> |
| 63 | + <div |
| 64 | + class="bg-gradient-to-r from-blue-600 to-blue-700 px-6 py-4 dark:from-blue-700 dark:to-indigo-800" |
| 65 | + > |
| 66 | + <h1 class="text-2xl font-bold text-white">{$_('Sensor Data - Today')}</h1> |
| 67 | + </div> |
| 68 | + |
| 69 | + <!-- Table wrapper --> |
| 70 | + <div class="overflow-x-auto"> |
| 71 | + {#if filteredData.length === 0} |
| 72 | + <div class="p-12 text-center text-gray-500 dark:text-gray-400"> |
| 73 | + <p class="text-lg">{$_('No data available for today')}</p> |
| 74 | + </div> |
| 75 | + {:else} |
| 76 | + <table class="w-full text-sm"> |
| 77 | + <thead |
| 78 | + class="border-b border-gray-200 bg-gray-50 dark:border-white/10 dark:bg-neutral-800" |
| 79 | + > |
| 80 | + <tr> |
| 81 | + {#each visibleColumns as column} |
| 82 | + <th |
| 83 | + class="text-md px-6 py-3 text-left font-semibold tracking-wide text-gray-700 uppercase dark:text-gray-200" |
| 84 | + > |
| 85 | + {$_(column)} |
| 86 | + </th> |
| 87 | + {/each} |
| 88 | + </tr> |
| 89 | + </thead> |
| 90 | + |
| 91 | + <tbody class="divide-y divide-gray-200 dark:divide-white/10"> |
| 92 | + {#each filteredData as row, rowIndex} |
| 93 | + <tr |
| 94 | + class="transition-colors duration-150 even:bg-transparent hover:bg-gray-50 dark:even:bg-white/5 dark:hover:bg-white/10" |
| 95 | + > |
| 96 | + {#each visibleColumns as column} |
| 97 | + <td |
| 98 | + class="px-6 py-3 text-lg whitespace-nowrap text-gray-900 dark:text-gray-100" |
| 99 | + > |
| 100 | + {formatCellValue(column, row[column])} |
| 101 | + </td> |
| 102 | + {/each} |
| 103 | + </tr> |
| 104 | + {/each} |
| 105 | + </tbody> |
| 106 | + </table> |
| 107 | + {/if} |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | +</div> |
0 commit comments