Skip to content

Commit

Permalink
Merge pull request seriousm4x#749 from studiojw/feat/ui-next-cron-time
Browse files Browse the repository at this point in the history
feat: add function to format next cron execution time as relative string
  • Loading branch information
seriousm4x authored Sep 3, 2024
2 parents 8555e08 + e3259b1 commit 0bd16b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"dependencies": {
"@fortawesome/free-solid-svg-icons": "^6.6.0",
"cron-parser": "^4.9.0",
"date-fns": "^3.6.0",
"npm-run-all": "^4.1.5",
"pocketbase": "^0.21.5",
Expand Down
17 changes: 17 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions frontend/src/lib/components/DeviceCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
faPen,
faRotateLeft
} from '@fortawesome/free-solid-svg-icons';
import cronParser from 'cron-parser';
import type { Locale } from 'date-fns';
import { formatDistance, parseISO } from 'date-fns';
import { formatDistance, formatRelative, parseISO } from 'date-fns';
import { de } from 'date-fns/locale/de';
import { enUS } from 'date-fns/locale/en-US';
import { es } from 'date-fns/locale/es';
Expand Down Expand Up @@ -128,6 +129,13 @@
toast.error(err.message);
});
}
function getNextCronRelativeTime(expression: string, now: number) {
const cron = cronParser.parseExpression(expression);
return formatRelative(cron.next().toISOString(), now, {
locale: dateFnsLocale
});
}
</script>

<div class="card rounded-3xl bg-base-300 shadow-md" transition:scale={{ delay: 0, duration: 200 }}>
Expand All @@ -144,18 +152,21 @@
<DeviceCardNic {device} />
</ul>
{#if device.wake_cron_enabled || device.shutdown_cron_enabled || device.password}
<div class="flex flex-row flex-wrap gap-2">
<div class="mt-1 flex flex-row flex-wrap gap-2">
{#if device.wake_cron_enabled}
<div class="tooltip" data-tip={$LL.device.card_tooltip_wake_cron()}>
<span class="badge badge-success gap-1 p-3"
><Fa icon={faCircleArrowUp} />{device.wake_cron}</span
><Fa icon={faCircleArrowUp} />{getNextCronRelativeTime(device.wake_cron, now)}</span
>
</div>
{/if}
{#if device.shutdown_cron_enabled}
<div class="tooltip" data-tip={$LL.device.card_tooltip_shutdown_cron()}>
<span class="badge badge-error gap-1 p-3"
><Fa icon={faCircleArrowDown} />{device.shutdown_cron}</span
><Fa icon={faCircleArrowDown} />{getNextCronRelativeTime(
device.shutdown_cron,
now
)}</span
>
</div>
{/if}
Expand Down

0 comments on commit 0bd16b8

Please sign in to comment.