report.pdf242.5 KB
"use client"

import type { FC } from "react"
import { cva, type VariantProps } from "class-variance-authority"
import {
  BracesIcon,
  DownloadIcon,
  FileIcon,
  FileTextIcon,
  ImageIcon,
  MusicIcon,
  VideoIcon,
} from "lucide-react"

import { cn } from "@/lib/utils"

const fileVariants = cva(
  "inline-flex items-center gap-3 rounded-lg transition-colors",
  {
    variants: {
      variant: {
        outline: "border-border hover:bg-muted/50 border",
        ghost: "hover:bg-muted/50",
        muted: "bg-muted/50 hover:bg-muted/70",
      },
      size: {
        sm: "px-2.5 py-1.5 text-xs",
        default: "px-3 py-2 text-sm",
        lg: "px-4 py-3 text-base",
      },
    },
    defaultVariants: {
      variant: "outline",
      size: "default",
    },
  }
)

function getMimeTypeIcon(mimeType: string): FC<{ className?: string }> {
  const type = mimeType.toLowerCase()
  if (type.startsWith("image/")) return ImageIcon
  if (type === "application/pdf") return FileTextIcon
  if (type === "application/json") return BracesIcon
  if (type.startsWith("text/")) return FileTextIcon
  if (type.startsWith("audio/")) return MusicIcon
  if (type.startsWith("video/")) return VideoIcon
  return FileIcon
}

export function formatFileSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
}

export type FileRootProps = React.ComponentProps<"div"> &
  VariantProps<typeof fileVariants>

function FileRoot({
  className,
  variant,
  size,
  children,
  ...props
}: FileRootProps) {
  return (
    <div
      data-slot="file-root"
      className={cn(fileVariants({ variant, size, className }))}
      {...props}
    >
      {children}
    </div>
  )
}

function FileIconDisplay({
  mimeType,
  className,
  children,
  ...props
}: React.ComponentProps<"span"> & { mimeType?: string }) {
  const IconComponent = mimeType ? getMimeTypeIcon(mimeType) : FileIcon
  return (
    <span
      data-slot="file-icon"
      className={cn("text-muted-foreground shrink-0", className)}
      {...props}
    >
      {children ?? <IconComponent className="size-5" />}
    </span>
  )
}

function FileName({
  className,
  children,
  ...props
}: React.ComponentProps<"span">) {
  return (
    <span
      data-slot="file-name"
      className={cn("min-w-0 flex-1 truncate font-medium", className)}
      {...props}
    >
      {children}
    </span>
  )
}

function FileSize({
  bytes,
  className,
  ...props
}: React.ComponentProps<"span"> & { bytes?: number }) {
  if (bytes === undefined) return null
  return (
    <span
      data-slot="file-size"
      className={cn("text-muted-foreground shrink-0 tabular-nums", className)}
      {...props}
    >
      {formatFileSize(bytes)}
    </span>
  )
}

function FileDownload({
  href,
  filename,
  className,
  ...props
}: React.ComponentProps<"a"> & { filename?: string }) {
  if (!href) return null
  return (
    <a
      data-slot="file-download"
      href={href}
      download={filename}
      className={cn(
        "text-muted-foreground hover:text-foreground shrink-0",
        className
      )}
      aria-label={filename ? `Download ${filename}` : "Download file"}
      {...props}
    >
      <DownloadIcon className="size-4" />
    </a>
  )
}

export function FilePart({
  name,
  mimeType,
  bytes,
  href,
  variant,
  size,
  className,
}: {
  name: string
  mimeType?: string
  bytes?: number
  href?: string
  variant?: FileRootProps["variant"]
  size?: FileRootProps["size"]
  className?: string
}) {
  return (
    <FileRoot variant={variant} size={size} className={className}>
      <FileIconDisplay mimeType={mimeType} />
      <div className="flex min-w-0 flex-1 flex-col">
        <FileName>{name}</FileName>
        <FileSize bytes={bytes} className="text-xs" />
      </div>
      <FileDownload href={href} filename={name} />
    </FileRoot>
  )
}

export {
  FileRoot,
  FileIconDisplay as FileIcon,
  FileName,
  FileSize,
  FileDownload,
  fileVariants,
}