Skip to content

Commit 6fca52d

Browse files
committed
Instances and UI: show time ago
UI == - MultiInput: getTimeAgo reactive, add type time (WIP) - ObjectArray: getTimeAgo reactive, show time ago for type time - custom_utilities: add timeAgo Server ===== - Instances: add time with type time and set time
1 parent 536b181 commit 6fca52d

File tree

7 files changed

+14004
-13931
lines changed

7 files changed

+14004
-13931
lines changed

interface/src/lib/components/custom/MultiInput.svelte

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313

1414
<script lang="ts">
15+
import { onDestroy } from 'svelte';
1516
import FileEdit from '$lib/components/custom/FileEdit.svelte';
16-
import {initCap} from '$lib/stores/custom_utilities';
17+
import {initCap, getTimeAgo} from '$lib/stores/custom_utilities';
1718
1819
export let property: any;
1920
export let value: any;
@@ -23,14 +24,24 @@
2324
export let step = 1;
2425
export let changeOnInput:boolean = true;
2526
27+
//make getTimeAgo reactive
28+
export let currentTime = Date.now();
29+
// Update the dummy variable every second
30+
const interval = setInterval(() => {
31+
currentTime = Date.now();
32+
}, 1000);
33+
onDestroy(() => {
34+
clearInterval(interval);
35+
});
36+
2637
</script>
2738

2839
<label class="label cursor-pointer" for={property.name}>
2940
<!-- <span class="label-text text-md">{initCap(property.name)}</span> -->
3041
<span class="mr-4">{initCap(property.name)}</span>
3142
</label>
3243

33-
{#if property.ro}
44+
{#if property.ro && false}
3445
{#if property.type == "ip"}
3546
<a
3647
href="http://{value}"
@@ -123,6 +134,8 @@
123134
on:change={onChange}
124135
on:input={(event:any) => {if (changeOnInput) onChange(event)}}
125136
/>
137+
{:else if property.type == "time"}
138+
<span>{value} {getTimeAgo(value, currentTime)}</span>
126139
{:else if property.type == "ip"}
127140
<input
128141
type={property.type}

interface/src/lib/components/custom/ObjectArray.svelte

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414
<script lang="ts">
15+
import { onDestroy } from 'svelte';
1516
import DragDropList, { VerticalDropZone, reorder, type DropEvent } from 'svelte-dnd-list';
1617
import Add from '~icons/tabler/circle-plus';
1718
import { user } from '$lib/stores/user';
@@ -23,7 +24,7 @@
2324
import Delete from '~icons/tabler/trash';
2425
import MultiInput from '$lib/components/custom/MultiInput.svelte';
2526
import ObjectArray from '$lib/components/custom/ObjectArray.svelte';
26-
import {initCap} from '$lib/stores/custom_utilities';
27+
import {initCap, getTimeAgo} from '$lib/stores/custom_utilities';
2728
2829
let { property, data = $bindable(), definition, onChange, changeOnInput} = $props();
2930
@@ -51,6 +52,17 @@
5152
}
5253
}
5354
55+
//make getTimeAgo reactive
56+
let currentTime = $state(Date.now());
57+
// Update the dummy variable every second
58+
const interval = setInterval(() => {
59+
currentTime = Date.now();
60+
}, 1000);
61+
62+
onDestroy(() => {
63+
clearInterval(interval);
64+
});
65+
5466
function onDrop(propertyName: string, { detail: { from, to } }: CustomEvent<DropEvent>) {
5567
5668
if (!to || from === to) {
@@ -154,7 +166,11 @@
154166
<Router class="text-primary-content h-auto w-full scale-75" />
155167
</div>
156168
{#each property.n.slice(0, 3) as propertyN}
157-
{#if propertyN.type != "array" && propertyN.type != "password"}
169+
{#if propertyN.type == "time"}
170+
<div>
171+
<div class="font-bold">{getTimeAgo(data[property.name][index][propertyN.name], currentTime)}</div>
172+
</div>
173+
{:else if propertyN.type != "array" && propertyN.type != "password"}
158174
<div>
159175
<div class="font-bold">{data[property.name][index][propertyN.name]}</div>
160176
</div>

interface/src/lib/stores/custom_utilities.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,18 @@ export function initCap(s: string) {
1919
result+=s.charAt(i);
2020
}
2121
return result;
22-
}
22+
}
23+
24+
export function getTimeAgo(timestamp: number, currentTime: number = Date.now()): string {
25+
const diff = currentTime - timestamp * 1000; // Convert seconds to milliseconds
26+
27+
const seconds = Math.floor(diff / 1000);
28+
const minutes = Math.floor(seconds / 60);
29+
const hours = Math.floor(minutes / 60);
30+
const days = Math.floor(hours / 24);
31+
32+
if (days > 0) return `${days} day${days > 1 ? 's' : ''} ago`;
33+
if (hours > 0) return `${hours} hour${hours > 1 ? 's' : ''} ago`;
34+
if (minutes > 0) return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
35+
return `${seconds} second${seconds > 1 ? 's' : ''} ago`;
36+
}

0 commit comments

Comments
 (0)