Skip to content
Snippets Groups Projects
utils.ts 1.88 KiB
Newer Older
Yusuf Akgül's avatar
Yusuf Akgül committed
import { env } from "@/env.mjs"
import { ClassValue, clsx } from "clsx"
import dayjs from "dayjs"
import { twMerge } from "tailwind-merge"

// tailwindcss classnames generator from shadcn
export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

// changes the default size of the image to be fetched
export function getImageURL(hashId: string, size: string): string {
Yusuf Akgül's avatar
Yusuf Akgül committed
  const IGDB_IMG_BASE_URL = env.IGDB_IMG_BASE_URL ?? ''
  return `${IGDB_IMG_BASE_URL}/t_${size}/${hashId}.jpg`
}

// calculates the offset for the query
export function calculateOffset(page: number, limit: number): number {
Yusuf Akgül's avatar
Yusuf Akgül committed
}

export function formatDate(data: number) {
  const date = new Date(data)
  return date.toLocaleDateString('en-US', {
    month: 'short',
    day: 'numeric',
    year: 'numeric'
  })
Yusuf Akgül's avatar
Yusuf Akgül committed
}

Yusuf Akgül's avatar
Yusuf Akgül committed
export function formatTimeElapsed(createdAt: Date) {
  const now = dayjs();
  const timeDiff = Math.abs(now.diff(dayjs(createdAt))); // Difference in milliseconds
Yusuf Akgül's avatar
Yusuf Akgül committed
  const seconds = Math.floor(timeDiff / 1000); // Convert to seconds
  const minutes = Math.floor(seconds / 60); // Convert to minutes
  const hours = Math.floor(minutes / 60); // Convert to hours
  const days = Math.floor(hours / 24); // Convert to days

  if (days > 0) {
    return dayjs(createdAt).format('L'); // Show the date if days have passed
Yusuf Akgül's avatar
Yusuf Akgül committed
  } else if (hours > 0) {
    return hours + 'h'; // Show hours if hours have passed
  } else if (minutes > 0) {
    return minutes + 'm'; // Show minutes if minutes have passed
  } else {
    return seconds + 's'; // Show seconds if seconds have passed
  }
}

// gets the current url for server or client
const IS_SERVER = typeof window === "undefined";
export default function getURL(path: string) {
  const baseURL = IS_SERVER
    ? env.NEXT_PUBLIC_APP_URL!
    : window.location.origin;
  return new URL(path, baseURL).toString();
}