Files
nadir-webui/src/lib/utils.ts
T
2026-06-25 23:11:32 +02:00

34 lines
1.2 KiB
TypeScript

import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import type { AgentError } from './remotes/utils';
export type WithElementRef<T, U extends HTMLElement = HTMLElement> = { ref?: null | U } & T;
// 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 function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function extractErrorMessage(err: unknown): string | undefined {
if (!err || typeof err !== 'object') return undefined;
const a = err as AgentError;
return a.body?.message ?? a.message ?? a.detail ?? undefined;
}
export function hasPermission(
moduleId: string,
perm: 'read' | 'root' | 'write',
userPerms: null | Record<string, null | string[]> | undefined
): boolean {
if (!userPerms) return false;
if (userPerms['*']) return true;
const modPerms = userPerms[moduleId];
if (!modPerms) return false;
return modPerms.includes('*') || modPerms.includes(perm);
}