import { type ReactNode } from "react" import dayjs from "dayjs"; export const wait = (ms: number) => new Promise((rs) => setTimeout(rs, ms)) export const toastError = (toast: any, message: string | ReactNode) => { toast({ variant: "destructive", title: "Oops! Something went wrong.", description: message, }) } export const toastInfo = (toast: any, message: string | ReactNode) => { toast({ title: "Success", description: message, }) } export const revalidateTag = async (tag: string) => { return fetch(`/api/revalidate/${tag}`).then(res => res.text()) } export function deleteAllCookies() { const cookies = document.cookie.split(";"); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i]; const eqPos = cookie.indexOf("="); const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; } } export function randomIntFromInterval(min: number, max: number) { // min and max included return Math.floor(Math.random() * (max - min + 1) + min) } export function getRandomNumber(min: number, max: number) { return Math.random() * (max - min) + min; } export function getArrayRandomNumber(n: number, min: number, max: number) { const randomNumbers = []; for (let i = 0; i < n; i++) { const randomNumber = getRandomNumber(min, max); randomNumbers.push(randomNumber); } return randomNumbers } export function getLastNDays(n: any) { const today = new Date(); // Get the current date const last30Days = []; for (let i = 0; i < n; i++) { const date = new Date(today); date.setDate(today.getDate() - i); // Subtract 'i' days from the current date last30Days.push(dayjs(date).format('YYYY-MM-DD')); } return last30Days; } export const getVariantApproved = (code: 'R' | 'NA' | 'AP' | 'A') => { switch (code) { case 'NA': return 'default' case 'A': return 'default' case 'R': return 'destructive' default: return 'secondary' } } export const getVariantWorkingStatus = (code: string) => { const res = code?.toUpperCase() switch (res) { case 'WFO': return 'outline' case 'WFS': return 'default' case 'SKT': return 'warn' case 'SAKIT': return 'warn' case 'I': return 'info' case 'IZIN': return 'info' case 'WFH': return 'secondary' default: return 'violet' } } export const getIconWorkingStatus = (code: string) => { switch (code) { case 'WFH': return 'ti ti-home' case 'WFS': return 'ti ti-building-community' case 'SKT': return 'ti ti-stethoscope' case 'SAKIT': return 'ti ti-stethoscope' case 'CT01': return 'ti ti-speedboat' default: return 'ti ti-speedboat' } } export const generateHourMinutes = (totalTime: any) => { try { const hours = Math.floor(totalTime / 3600); const remainingSeconds = totalTime % 3600; const minutes = Math.floor(remainingSeconds / 60); // Create the formatted string const formattedTime = `${hours} hour${hours !== 1 ? 's' : ''} ${minutes} minute${minutes !== 1 ? 's' : ''}`; return formattedTime } catch (error) { return '0 hour' } } export function titleCase(str: string) { var splitStr = str.toLowerCase().split(' '); for (var i = 0; i < splitStr.length; i++) { splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); } return splitStr.join(' '); } export function readFile(file: any) { return new Promise((resolve) => { const reader = new FileReader() reader.addEventListener('load', () => resolve(reader.result), false) reader.readAsDataURL(file) }) } export const toDataURL = (url: any) => fetch(url) .then(response => response.blob()) .then(blob => new Promise((resolve, reject) => { const reader = new FileReader() reader.onloadend = () => resolve(reader.result) reader.onerror = reject reader.readAsDataURL(blob) } )) export const blobToBase64 = (blob: any) => { const reader = new FileReader(); reader.readAsDataURL(blob); return new Promise(resolve => { reader.onloadend = () => { resolve(reader.result); }; }); }; export function generateCalendar(month: any = dayjs().month()) { month = Math.floor(month) const year = dayjs().year() const firstDayOfTheMonth = dayjs(new Date(year, month, 1)).day() let currentMonthCount = 0 - firstDayOfTheMonth const daysMatrix = new Array(5).fill([]).map(() => { return new Array(7).fill(null).map(() => { currentMonthCount++ return dayjs(new Date(year, month, currentMonthCount)) }) }) return daysMatrix } function getDeviceType() { if (navigator?.userAgent.includes('Mobile')) { return 'Mobile'; } else if (navigator.userAgent.includes('Tablet')) { return 'Tablet'; } else { return 'Computer'; } } function getOperatingSystemInfo() { const platform = navigator ? navigator.platform : ''; let osInfo = 'Unknown'; if (platform.startsWith('Win')) { osInfo = 'Windows'; } else if (platform.startsWith('Mac')) { osInfo = 'macOS'; } else if (platform === 'Linux' || platform.startsWith('Linux')) { osInfo = 'Linux'; } else if (platform === 'iPhone' || platform === 'iPad' || platform === 'iPod') { osInfo = 'iOS'; } else if (platform.startsWith('Android')) { osInfo = 'Android'; } return osInfo; } function getBrowserInfo() { const userAgent = navigator ? navigator.userAgent : ''; const match = userAgent.match(/(Chrome|Firefox|Edge|IE|Opera|Safari)\/(\S+)/); if (match) { return `${match[1]} ${match[2]}`; } else { return 'Unknown'; } } export const getUserAgent = () => { try { const deviceType = getDeviceType(); const deviceModel = navigator.userAgent.match(/\(([^)]+)\)/)![1] || 'Unknown'; const osInfo = getOperatingSystemInfo(); const agentType = 'Browser'; const browserInfo = getBrowserInfo(); const renderingEngine = 'Webkit'; return { deviceType, deviceModel, osInfo, agentType, browserInfo, renderingEngine } } catch (error) { return { deviceType: '', deviceModel: '', osInfo: '', agentType: '', browserInfo: '', renderingEngine: '' } } } export const getDeviceFCM = () => { const { deviceType, osInfo } = getUserAgent() const device = deviceType.toUpperCase() const os = osInfo.toUpperCase() if (device === 'MOBILE') { if (os === 'IOS') { return 'ios' } else { return 'browser mobile' } } else { return 'browser' } }