Files
sexy/packages/frontend/src/lib/utils.ts
Valknar XXX 5bd2d9c215 fix: handle undefined user name in UI components
- Added null check in getUserInitials function to return "??" for undefined names
- Fixed logout button to handle undefined user.name when displaying first name
- Prevents 500 errors when rendering components for users without names

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 04:48:41 +01:00

46 lines
1.4 KiB
TypeScript

import { browser } from "$app/environment";
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type WithoutChild<T> = T extends { child?: any } ? Omit<T, "child"> : T;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type WithoutChildren<T> = T extends { children?: any }
? Omit<T, "children">
: T;
export type WithoutChildrenOrChild<T> = WithoutChildren<WithoutChild<T>>;
export type WithElementRef<T, U extends HTMLElement = HTMLElement> = T & {
ref?: U | null;
};
export const calcReadingTime = (text: string) => {
const wordsPerMinute = 200; // Average case.
const textLength = text.split(" ").length; // Split by words
if (textLength > 0) {
return Math.ceil(textLength / wordsPerMinute);
}
return 0;
};
export const getUserInitials = (name: string) => {
if (!name) return "??";
return name
.split(" ")
.map((word) => word.charAt(0))
.join("")
.toUpperCase()
.slice(0, 2);
};
export const formatVideoDuration = (duration: number) => {
const hours = Math.floor(duration / 3600);
const minutes = Math.floor((duration - hours * 3600) / 60);
const seconds = duration - hours * 3600 - minutes * 60;
return `${hours < 10 ? "0" + hours : hours}:${minutes < 10 ? "0" + minutes : minutes}:${seconds < 10 ? "0" + seconds : seconds}`;
};