Use locale-aware parseTimeInput for duration inputs

This commit is contained in:
Gregor Vostrak
2026-03-11 15:36:00 +01:00
parent a1d5563fc4
commit 97df779d1e
6 changed files with 567 additions and 50 deletions

View File

@@ -1,7 +1,10 @@
<script setup lang="ts">
import parse from 'parse-duration';
import { onMounted, ref, watch, inject } from 'vue';
import { formatHumanReadableDuration, getDayJsInstance } from '@/packages/ui/src/utils/time';
import {
formatHumanReadableDuration,
getDayJsInstance,
parseTimeInput,
} from '@/packages/ui/src/utils/time';
import dayjs from 'dayjs';
import { twMerge } from 'tailwind-merge';
import { TextInput } from '@/packages/ui/src';
@@ -20,51 +23,23 @@ const end = defineModel('end', {
const organization = inject<ComputedRef<Organization>>('organization');
function isHHMM(value: string): boolean {
return HHMMtimeRegex.test(value);
}
function parseHHMM(value: string): string[] | null {
return value.match(HHMMtimeRegex);
}
function updateDuration() {
const time = parse(temporaryCustomTimerEntry.value, 's');
if (isNumeric(temporaryCustomTimerEntry.value)) {
const newStartDate = getDayJsInstance()(end.value).subtract(
parseInt(temporaryCustomTimerEntry.value),
'm'
);
start.value = newStartDate.utc().format();
} else if (isHHMM(temporaryCustomTimerEntry.value)) {
const results = parseHHMM(temporaryCustomTimerEntry.value);
if (results) {
const newStartDate = getDayJsInstance()(end.value)
.subtract(parseInt(results[1]!), 'h')
.subtract(parseInt(results[2]!), 'm');
start.value = newStartDate.utc().format();
}
}
// try to parse natural language like "1h 30m"
else if (time && time > 1) {
const newStartDate = getDayJsInstance()(end.value).subtract(time, 's');
const seconds = parseTimeInput(
temporaryCustomTimerEntry.value,
organization?.value?.number_format,
organization?.value?.interval_format === 'decimal' ? 'hours' : 'minutes'
);
if (seconds && seconds > 0) {
const newStartDate = getDayJsInstance()(end.value).subtract(seconds, 's');
start.value = newStartDate.utc().format();
}
// fallback to minutes if just a number is given
updateTimeEntryInputValue();
}
function isNumeric(value: string) {
return /^-?\d+$/.test(value);
}
const props = defineProps<{
class?: string;
}>();
const HHMMtimeRegex = /^([0-9]{1,2}):([0-5]?[0-9])$/;
watch([start, end], updateTimeEntryInputValue);
onMounted(() => updateTimeEntryInputValue());

View File

@@ -26,8 +26,11 @@ function updateDuration() {
return;
}
// Use parseTimeInput with 'hours' as default unit for estimated time
const seconds = parseTimeInput(input, 'hours');
const seconds = parseTimeInput(
input,
organization?.value?.number_format,
'hours'
);
if (seconds !== null && seconds > 0) {
model.value = seconds;
}