Components
Loading preview...
A file upload component for React.
npx shadcn@latest add https://21st.dev/r/sean0205/file-upload'use client';
import { useState } from 'react';
import { useFileUpload } from '@/components/ui/file-upload';
import { Alert, AlertContent, AlertDescription, AlertIcon, AlertTitle } from '@/components/ui/alert-1';
import { Button } from '@/components/ui/button-1';
import { ImageIcon, TriangleAlert, Upload, XIcon, ZoomInIcon } from 'lucide-react';
import { cn } from '@/lib/utils';
/**
* Throttles a function to limit its execution to once every specified duration.
*
* @param func - The function to throttle.
* @param limit - The minimum delay in milliseconds between calls.
* @returns A throttled version of the provided function.
*/
export const throttle = (func: (...args: unknown[]) => void, limit: number): ((...args: unknown[]) => void) => {
let lastFunc: ReturnType<typeof setTimeout> | null = null;
let lastRan: number | null = null;
return function (this: unknown, ...args: unknown[]) {
if (lastRan === null) {
func.apply(this, args);
lastRan = Date.now();
} else {
if (lastFunc !== null) {
clearTimeout(lastFunc);
}
lastFunc = setTimeout(
() => {
if (Date.now() - (lastRan as number) >= limit) {
func.apply(this, args);
lastRan = Date.now();
}
},
limit - (Date.now() - (lastRan as number)),
);
}
};
};
/**
* Debounces a function to delay its execution until after a specified delay.
*
* @param func - The function to debounce.
* @param wait - The delay in milliseconds.
* @returns A debounced version of the provided function.
*/
export function debounce<T extends (...args: unknown[]) => unknown>(
func: T,
wait: number,
): (...args: Parameters<T>) => void {
let timeout: NodeJS.Timeout | null = null;
return function (...args: Parameters<T>): void {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
func(...args);
}, wait);
};
}
/**
* Generates a unique identifier using the current timestamp and a random number.
*
* @returns A string representing the unique ID.
*/
export function uid(): string {
return (Date.now() + Math.floor(Math.random() * 1000)).toString();
}
/**
* Extracts initials from a given name.
*
* @param name - The full name to extract initials from.
* @param count - The number of initials to return. Defaults to all initials.
* @returns A string of initials from the name.
*/
export const getInitials = (name: string | null | undefined, count?: number): string => {
if (!name || typeof name !== 'string') {
return '';
}
const initials = name
.split(' ')
.filter(Boolean)
.map((part) => part[0].toUpperCase());
return count && count > 0 ? initials.slice(0, count).join('') : initials.join('');
};
/**
* Formats a date as a readable string in "Month Day, Year" format.
*
* @param input - A date string or timestamp to format.
* @returns A string formatted as "Month Day, Year".
*/
export function formatDate(input: Date | string | number): string {
const date = new Date(input);
return date.toLocaleDateString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric',
});
}
/**
* Formats a date and time as a readable string in "Month Day, Year, Hour:Minute AM/PM" format.
*
* @param input - A date string or timestamp to format.
* @returns A string formatted as "Month Day, Year, Hour:Minute AM/PM".
*/
export function formatDateTime(input: Date | string | number): string {
const date = new Date(input);
return date.toLocaleString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: true,
});
}
/**
* Formats a number as a currency string.
*
* @param amount - The numeric value to format as currency.
* @param currency - The currency code (e.g., "USD", "EUR"). Defaults to "USD".
* @param locale - The locale for formatting (e.g., "en-US"). Defaults to "en-US".
* @returns A string formatted as currency.
*/
export function formatCurrency(amount: number, currency: string = 'USD', locale: string = 'en-US'): string {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency,
}).format(amount);
}
/**
* Constructs an absolute URL based on the base application URL.
*
* @param path - The relative path to append to the base URL.
* @returns A string representing the absolute URL.
*/
export function absoluteUrl(path: string): string {
return `${process.env.NEXT_PUBLIC_APP_URL}${path}`;
}
/**
* Constructs an absolute URL for media assets.
*
* @param path - The relative path to the media asset (e.g., "/media/avatars/1.png").
* @returns A string representing the absolute URL to the media asset.
*/
export function toAbsoluteUrl(path: string): string {
// Remove leading slash if present to avoid double slashes
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
return `/${cleanPath}`;
}
/**
Retrieves a list of supported time zones with their labels and values.
This function fetches the available time zones from the environment,
formats their offsets (e.g., "GMT+2"), and returns them in a sorted array.
*/
export const getTimeZones = (): { label: string; value: string }[] => {
// Fetch supported timezones
const timezones = Intl.supportedValuesOf('timeZone');
return timezones
.map((timezone) => {
const formatter = new Intl.DateTimeFormat('en', {
timeZone: timezone,
timeZoneName: 'shortOffset',
});
const parts = formatter.formatToParts(new Date());
const offset = parts.find((part) => part.type === 'timeZoneName')?.value || '';
const formattedOffset = offset === 'GMT' ? 'GMT+0' : offset;
return {
value: timezone,
label: `(${formattedOffset}) ${timezone.replace(/_/g, ' ')}`,
numericOffset: parseInt(formattedOffset.replace('GMT', '').replace('+', '') || '0'),
};
})
.sort((a, b) => a.numericOffset - b.numericOffset);
};
/**
* Generates a URL-friendly slug from a given title.
* @param title - The title to convert into a slug (e.g., "Write a Proposal")
* @returns A slug string (e.g., "write-a-proposal")
*/
export function getSlug(title: string): string {
// Return empty string for invalid input
if (!title || typeof title !== 'string') {
return '';
}
return title
.toLowerCase() // Convert to lowercase for consistency
.trim() // Remove leading/trailing whitespace
.normalize('NFD') // Normalize unicode (e.g., "é" -> "e")
.replace(/[\u0300-\u036f]/g, '') // Remove diacritics
.replace(/[^a-z0-9\s-]/g, '') // Remove special characters except spaces/hyphens
.replaceAll(/\s+/g, '-') // Replace spaces with single hyphen
.replace(/-+/g, '-') // Collapse multiple hyphens
.replace(/^-|-$/g, ''); // Remove leading/trailing hyphens
}
function formatBytes(bytes: number): string {
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
if (bytes === 0) return '0 Byte';
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${parseFloat((bytes / Math.pow(1024, i)).toFixed(2))} ${sizes[i]}`;
}
export function useCopyToClipboard() {
const [copied, setCopied] = useState(false);
const copy = async (text: string) => {
if (!navigator?.clipboard) return false;
try {
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
return true;
} catch (error) {
console.error('Failed to copy:', error);
setCopied(false);
return false;
}
};
return { copy, copied };
}
interface GalleryUploadProps {
maxFiles?: number;
maxSize?: number;
accept?: string;
multiple?: boolean;
className?: string;
onFilesChange?: (files: FileWithPreview[]) => void;
}
export default function GalleryUpload({
maxFiles = 10,
maxSize = 5 * 1024 * 1024, // 5MB
accept = 'image/*',
multiple = true,
className,
onFilesChange,
}: GalleryUploadProps) {
const [selectedImage, setSelectedImage] = useState<string | null>(null);
// Create default images using FileMetadata type
const defaultImages: FileMetadata[] = [
{
id: 'default-1',
name: 'avatar-1.png',
size: 44608,
type: 'image/png',
url: 'https://randomuser.me/api/portraits/women/21.jpg',
},
{
id: 'default-2',
name: 'avatar-2.png',
size: 42144,
type: 'image/png',
url: 'https://randomuser.me/api/portraits/men/21.jpg',
},
];
const [
{ files, isDragging, errors },
{
removeFile,
clearFiles,
handleDragEnter,
handleDragLeave,
handleDragOver,
handleDrop,
openFileDialog,
getInputProps,
},
] = useFileUpload({
maxFiles,
maxSize,
accept,
multiple,
initialFiles: defaultImages,
onFilesChange,
});
const isImage = (file: File | FileMetadata) => {
const type = file instanceof File ? file.type : file.type;
return type.startsWith('image/');
};
return (
<div className="flex flex-col gap-5 p-10 w-full mx-auto h-screen justify-center items-center">
<div className={cn('w-full max-w-4xl', className)}>
{/* Upload Area */}
<div
className={cn(
'relative rounded-lg border border-dashed p-8 text-center transition-colors',
isDragging ? 'border-primary bg-primary/5' : 'border-muted-foreground/25 hover:border-muted-foreground/50',
)}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
onDragOver={handleDragOver}
onDrop={handleDrop}
>
<input {...getInputProps()} className="sr-only" />
<div className="flex flex-col items-center gap-4">
<div
className={cn(
'flex h-16 w-16 items-center justify-center rounded-full',
isDragging ? 'bg-primary/10' : 'bg-muted',
)}
>
<ImageIcon className={cn('h-5 w-5', isDragging ? 'text-primary' : 'text-muted-foreground')} />
</div>
<div className="space-y-2">
<h3 className="text-lg font-semibold">Upload images to gallery</h3>
<p className="text-sm text-muted-foreground">Drag and drop images here or click to browse</p>
<p className="text-xs text-muted-foreground">
PNG, JPG, GIF up to {formatBytes(maxSize)} each (max {maxFiles} files)
</p>
</div>
<Button onClick={openFileDialog}>
<Upload className="h-4 w-4" />
Select images
</Button>
</div>
</div>
{/* Gallery Stats */}
{files.length > 0 && (
<div className="mt-6 flex items-center justify-between">
<div className="flex items-center gap-4">
<h4 className="text-sm font-medium">
Gallery ({files.length}/{maxFiles})
</h4>
<div className="text-xs text-muted-foreground">
Total: {formatBytes(files.reduce((acc, file) => acc + file.file.size, 0))}
</div>
</div>
<Button onClick={clearFiles} variant="outline" size="sm">
Clear all
</Button>
</div>
)}
{/* Image Grid */}
{files.length > 0 && (
<div className="mt-4 grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4">
{files.map((fileItem) => (
<div key={fileItem.id} className="group relative aspect-square">
{isImage(fileItem.file) && fileItem.preview ? (
<img
src={fileItem.preview}
alt={fileItem.file.name}
className="h-full w-full rounded-lg border object-cover transition-transform group-hover:scale-105"
/>
) : (
<div className="flex h-full w-full items-center justify-center rounded-lg border bg-muted">
<ImageIcon className="h-8 w-8 text-muted-foreground" />
</div>
)}
{/* Overlay */}
<div className="absolute inset-0 flex items-center justify-center gap-2 rounded-lg bg-black/50 opacity-0 transition-opacity group-hover:opacity-100">
{/* View Button */}
{fileItem.preview && (
<Button
onClick={() => setSelectedImage(fileItem.preview!)}
variant="secondary"
size="icon"
className="size-7"
>
<ZoomInIcon className="opacity-100/80" />
</Button>
)}
{/* Remove Button */}
<Button onClick={() => removeFile(fileItem.id)} variant="secondary" size="icon" className="size-7">
<XIcon className="opacity-100/80" />
</Button>
</div>
{/* File Info */}
<div className="absolute bottom-0 left-0 right-0 rounded-b-lg bg-black/70 p-2 text-white opacity-0 transition-opacity group-hover:opacity-100">
<p className="truncate text-xs font-medium">{fileItem.file.name}</p>
<p className="text-xs text-gray-300">{formatBytes(fileItem.file.size)}</p>
</div>
</div>
))}
</div>
)}
{/* Error Messages */}
{errors.length > 0 && (
<Alert variant="destructive" appearance="light" className="mt-5">
<AlertIcon>
<TriangleAlert />
</AlertIcon>
<AlertContent>
<AlertTitle>File upload error(s)</AlertTitle>
<AlertDescription>
{errors.map((error, index) => (
<p key={index} className="last:mb-0">
{error}
</p>
))}
</AlertDescription>
</AlertContent>
</Alert>
)}
{/* Image Preview Modal */}
{selectedImage && (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm transition-all duration-300 p-4"
onClick={() => setSelectedImage(null)}
>
<div className="relative max-h-full max-w-full">
<img
src={selectedImage}
alt="Preview"
className="max-h-full max-w-full rounded-lg object-contain"
onClick={(e) => e.stopPropagation()}
/>
<Button
onClick={() => setSelectedImage(null)}
variant="secondary"
size="icon"
className="absolute end-2 top-2 size-7 p-0"
>
<XIcon className="h-4 w-4" />
</Button>
</div>
</div>
)}
</div>
</div>
);
}