2025-10-29 15:42:43 +01:00
|
|
|
import { InertiaLinkProps } from '@inertiajs/vue3'
|
|
|
|
|
import { clsx, type ClassValue } from 'clsx'
|
|
|
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
|
import { toast } from 'vue-sonner'
|
2025-10-20 08:57:51 +02:00
|
|
|
|
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
|
|
|
return twMerge(clsx(inputs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function urlIsActive(urlToCheck: NonNullable<InertiaLinkProps['href']>, currentUrl: string) {
|
2025-10-29 10:24:39 +01:00
|
|
|
return currentUrl.indexOf(toUrl(urlToCheck)) >= 0
|
2025-10-20 08:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toUrl(href: NonNullable<InertiaLinkProps['href']>) {
|
|
|
|
|
return typeof href === 'string' ? href : href?.url;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 10:24:39 +01:00
|
|
|
|
2025-10-20 08:57:51 +02:00
|
|
|
const currencyFormatter = new Intl.NumberFormat('de-DE', {
|
|
|
|
|
style: 'currency',
|
|
|
|
|
currency: 'EUR',
|
|
|
|
|
minimumFractionDigits: 2,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export function toCurrency(value: number | undefined) {
|
|
|
|
|
if (!value) return currencyFormatter.format(0);
|
|
|
|
|
return currencyFormatter.format(value);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 10:24:39 +01:00
|
|
|
export function toFixedRounded(num: number, precision: number): number {
|
|
|
|
|
return Number((Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision)).toFixed(precision))
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 08:57:51 +02:00
|
|
|
export function toLocalDate(value: string) {
|
|
|
|
|
const date = new Date(value);
|
|
|
|
|
return date.toLocaleDateString('de-DE', { day: '2-digit', month: 'short', year: 'numeric' });
|
|
|
|
|
// return date.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' });
|
|
|
|
|
// return date.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: '2-digit' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toShortISOString(date: Date | string) {
|
|
|
|
|
let tempDate = (typeof date === 'string') ? new Date(date) : date
|
|
|
|
|
return tempDate.toISOString().split('T')[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function calcDueDate(date: Date, daysAfterInvoice: number): Date {
|
|
|
|
|
let dueDate = new Date(date);
|
|
|
|
|
dueDate.setDate(date.getDate() + daysAfterInvoice);
|
|
|
|
|
return dueDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function randomInt(max: number) {
|
|
|
|
|
return Math.floor(Math.random() * max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const randomColors = [
|
|
|
|
|
'bg-rose-200 dark:bg-rose-700',
|
|
|
|
|
'bg-lime-200 dark:bg-lime-700',
|
|
|
|
|
'bg-orange-200 dark:bg-orange-700',
|
|
|
|
|
'bg-sky-200 dark:bg-sky-700',
|
|
|
|
|
'bg-amber-200 dark:bg-amber-700',
|
|
|
|
|
'bg-blue-200 dark:bg-blue-700',
|
|
|
|
|
'bg-purple-200 dark:bg-purple-700'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
export function bgColorForString(input: string): string {
|
|
|
|
|
let charCodeSum = 0;
|
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
|
charCodeSum += input.charCodeAt(i)
|
|
|
|
|
}
|
|
|
|
|
return randomColors[charCodeSum % randomColors.length];
|
2025-10-29 15:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const promise = () => new Promise((resolve) => setTimeout(resolve, 2000))
|
|
|
|
|
export function testToast() {
|
|
|
|
|
let delay = 150
|
|
|
|
|
toast('Toast', {
|
|
|
|
|
description: "Description bjksad fkjlhsd fkjhsdf jkshdf jkashdf adskljhf ", action: {
|
|
|
|
|
label: 'Undo',
|
|
|
|
|
onClick: () => console.log('Undo')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
toast.info('Info', {
|
|
|
|
|
action: {
|
|
|
|
|
label: 'Undo',
|
|
|
|
|
onClick: () => console.log('Undo')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}, delay * 1)
|
|
|
|
|
setTimeout(() => { toast.success('Success', { description: "description" }) }, delay * 2)
|
|
|
|
|
setTimeout(() => { toast.warning('Warning', { description: "description" }) }, delay * 3)
|
|
|
|
|
setTimeout(() => { toast.error('Error', { description: "description" }) }, delay * 4)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
toast.promise(promise, {
|
|
|
|
|
loading: 'Loading...',
|
|
|
|
|
success: (data) => {
|
|
|
|
|
return `${data.name} toast has been added`;
|
|
|
|
|
},
|
|
|
|
|
error: (data: any) => 'Error',
|
|
|
|
|
})
|
|
|
|
|
}, delay * 5)
|
|
|
|
|
|
2025-10-20 08:57:51 +02:00
|
|
|
}
|