feat(new tools): Time Alarm and countdown

Fix #5 and #1489
This commit is contained in:
ShareVB committed 2025-04-20 23:15:37 +02:00
1 parent 33cffc5d73
commit bf7f986bfd
9 files changed
+369 -7

No files matched your search

+164
View File
@@ -0,0 +1,164 @@
<script setup lang="ts">
import { Countdown } from 'vue3-flip-countdown';
import moment from 'moment';
import { useQueryParam } from '@/composable/queryParams';
interface CountDown {
d: number
h: number
m: number
s: number
}
function getCountDownHref(c: CountDown) {
const parsedUrl = new URL(window.location.href);
parsedUrl.searchParams.set('d', c.d.toString());
parsedUrl.searchParams.set('h', c.h.toString());
parsedUrl.searchParams.set('m', c.m.toString());
parsedUrl.searchParams.set('s', c.s.toString());
return parsedUrl.toString();
}
const days = useQueryParam({ name: 'd', defaultValue: 0 });
const hours = useQueryParam({ name: 'h', defaultValue: 0 });
const minutes = useQueryParam({ name: 'm', defaultValue: 15 });
const seconds = useQueryParam({ name: 's', defaultValue: 0 });
const history = useStorage<CountDown[]>('countdown:hst', []);
const status = ref<'stopped' | 'ended' | 'running'>('stopped');
const audio = ref<HTMLAudioElement | null>(null);
watchEffect(() => {
if (status.value === 'ended') {
audio.value?.play();
}
});
const now = ref(moment());
const deadlineDate = computed(() => {
return now.value
.add(days.value, 'd')
.add(hours.value, 'h')
.add(minutes.value, 'm')
.add(seconds.value, 's')
.toDate();
});
const fmt = 'YYYY-MM-DD HH:mm:ss';
const deadlineFormatted = computed(() => {
return now.value
.add(days.value, 'd')
.add(hours.value, 'h')
.add(minutes.value, 'm')
.add(seconds.value, 's')
.format(fmt);
});
function start() {
now.value = moment();
status.value = 'running';
const histoEntry = { d: days.value, h: hours.value, m: minutes.value, s: seconds.value };
if (!history.value.find(h => h.d === histoEntry.d && h.h === histoEntry.h && h.m === histoEntry.m && h.s === histoEntry.s)) {
history.value = [histoEntry, ...history.value];
}
}
function stop() {
status.value = 'stopped';
audio.value?.pause();
}
function ended() {
if (status.value === 'running') {
status.value = 'ended';
}
}
const isEnded = computed(() => status.value === 'ended');
</script>
<template>
<div max-w-600px>
<c-card :disabled="status !== 'stopped'" title="Countdown time" mb-4>
<div mb-4 flex justify-center gap-2>
<n-input-number v-model:value="days" :min="0">
<template #suffix>
days
</template>
</n-input-number>
<n-input-number v-model:value="hours" :min="0">
<template #suffix>
hours
</template>
</n-input-number>
<n-input-number v-model:value="minutes" :min="0">
<template #suffix>
minutes
</template>
</n-input-number>
<n-input-number v-model:value="seconds" :min="0">
<template #suffix>
seconds
</template>
</n-input-number>
</div>
<div flex justify-center>
<c-button
@click="start"
>
Start
</c-button>
</div>
</c-card>
<Countdown :deadline="deadlineFormatted" :stop="status !== 'running'" mb-2 @time-elapsed="ended()" />
<n-p align="center">
Alarm at: {{ deadlineDate }}
</n-p>
<div mb-2 flex justify-center>
<c-button
:disabled="status === 'stopped'"
@click="stop"
>
Stop
</c-button>
</div>
<n-modal v-model:show="isEnded" mask-closable="false">
<n-card
style="width: 600px"
title="Timer finished"
:bordered="false"
size="huge"
role="dialog"
aria-modal="true"
>
<p>Timer ellapsed!</p>
<template #footer>
<n-button @click="stop()">
OK
</n-button>
</template>
</n-card>
</n-modal>
<c-card v-if="history" title="History">
<div flex justify-center gap-1>
<template v-for="(entry, index) in history" :key="index">
{{ index > 0 ? ' / ' : '' }}
<n-a :href="getCountDownHref(entry)">
{{ entry.d > 0 ? `${entry.d} days` : '' }}
{{ entry.h > 0 ? `${entry.h}h` : '' }}
{{ entry.m > 0 ? `${entry.m}m` : '' }}
{{ entry.s > 0 ? `${entry.s}s` : '' }}
</n-a>
</template>
</div>
</c-card>
<audio ref="audio" loop src="/Beep.mp3" />
</div>
</template>
+13
View File
@@ -0,0 +1,13 @@
import { Alarm } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Countdown',
path: '/countdown',
description: 'Countdown and alarm at ellapsed duration',
keywords: ['countdown'],
component: () => import('./countdown.vue'),
icon: Alarm,
createdAt: new Date('2025-03-09'),
category: 'Datetime',
});
+3
View File
@@ -0,0 +1,3 @@
declare module "vue3-flip-countdown" {
export function Countdown(): any;
}
+13
View File
@@ -0,0 +1,13 @@
import { Alarm } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Time Alarm',
path: '/time-alarm',
description: 'Alarm at a specific date and time',
keywords: ['time', 'alarm'],
component: () => import('./time-alarm.vue'),
icon: Alarm,
createdAt: new Date('2025-03-09'),
category: 'Datetime',
});
+133
View File
@@ -0,0 +1,133 @@
<script setup lang="ts">
import { Countdown } from 'vue3-flip-countdown';
import moment from 'moment';
import { useQueryParam } from '@/composable/queryParams';
const alarmAt = useQueryParam({ name: 'alarmAt', defaultValue: '17:30:00' });
const history = useStorage<string[]>('timealarm:hst', []);
function getTimeHref(at: string) {
const parsedUrl = new URL(window.location.href);
parsedUrl.searchParams.set('at', at);
return parsedUrl.toString();
}
const status = ref<'stopped' | 'ended' | 'running'>('stopped');
const audio = ref<HTMLAudioElement | null>(null);
watchEffect(() => {
if (status.value === 'ended') {
audio.value?.play();
}
});
const now = ref(moment());
const alarmAtDate = computed(() => {
const [h, m, s] = alarmAt.value.split(':');
return now.value
.set('h', Number(h))
.set('m', Number(m))
.set('s', Number(s))
.toDate();
});
const fmt = 'YYYY-MM-DD HH:mm:ss';
const alarmAtFormatted = computed(() => {
const [h, m, s] = alarmAt.value.split(':');
return now.value
.set('h', Number(h))
.set('m', Number(m))
.set('s', Number(s))
.format(fmt);
});
function start() {
now.value = moment();
status.value = 'running';
const histoEntry = alarmAt.value;
if (!history.value.find(h => h === histoEntry)) {
history.value = [histoEntry, ...history.value];
}
}
function stop() {
status.value = 'stopped';
audio.value?.pause();
}
function ended() {
if (status.value === 'running') {
status.value = 'ended';
}
}
const isEnded = computed(() => status.value === 'ended');
</script>
<template>
<div max-w-600px>
<c-card :disabled="status !== 'stopped'" title="Alarm" mb-4>
<div flex justify-center>
<n-form-item label="Alarm at:" label-placement="left">
<n-time-picker v-model:formatted-value="alarmAt" />
</n-form-item>
</div>
<div flex justify-center>
<c-button
@click="start"
>
Start
</c-button>
</div>
</c-card>
<Countdown :deadline="alarmAtFormatted" :stop="status !== 'running'" mb-2 @time-elapsed="ended()" />
<n-p align="center">
Alarm at: {{ alarmAtDate }}
</n-p>
<div mb-2 flex justify-center>
<c-button
:disabled="status === 'stopped'"
@click="stop"
>
Stop
</c-button>
</div>
<n-modal v-model:show="isEnded" mask-closable="false">
<n-card
style="width: 600px"
title="Timer finished"
:bordered="false"
size="huge"
role="dialog"
aria-modal="true"
>
<p>Timer ellapsed!</p>
<template #footer>
<n-button @click="stop()">
OK
</n-button>
</template>
</n-card>
</n-modal>
<c-card v-if="history" title="History">
<div flex justify-center gap-1>
<template v-for="(entry, index) in history" :key="index">
{{ index > 0 ? ' / ' : '' }}
<n-a :href="getTimeHref(entry)">
{{ entry }}
</n-a>
</template>
</div>
</c-card>
<audio ref="audio" loop src="/Beep.mp3" />
</div>
</template>
+3
View File
@@ -0,0 +1,3 @@
declare module "vue3-flip-countdown" {
export function Countdown(): any;
}