{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "block-state",
  "title": "Block State",
  "description": "Primitives for loading, refreshing, empty and error states: a skeleton, a dimmed frame that keeps its layout while it refreshes, a message with an action, and a data-loading hook.",
  "files": [
    {
      "path": "registry/components/dashboardblocks/block-state.tsx",
      "content": "'use client'\n\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport {\n  type ComponentProps,\n  type ReactNode,\n  createContext,\n  startTransition,\n  useCallback,\n  useContext,\n  useEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\nfunction Skeleton({ className, ...props }: ComponentProps<'div'>) {\n  return (\n    <div\n      aria-hidden\n      className={cn(\n        'bg-muted animate-pulse rounded-md motion-reduce:animate-none',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\nfunction SkeletonText({ className, ...props }: ComponentProps<'span'>) {\n  return (\n    <span\n      aria-hidden\n      className={cn(\n        'bg-muted inline-block h-[0.8em] max-w-full animate-pulse rounded-sm align-middle motion-reduce:animate-none',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\ntype BlockStatus = 'loading' | 'error'\n\ntype BlockState = BlockStatus | 'empty'\n\ntype BlockData = readonly unknown[] | string | number | object | null | undefined\n\ninterface BlockStateProps {\n  /**\n   * Shown under \"Couldn’t load\" while `status` is `'error'`: one sentence for people on\n   * what went wrong and what to do, such as `The query took too long. Try a shorter range.`\n   * Never the raw error.\n   */\n  errorMessage?: string\n  status?: BlockStatus\n}\n\nconst BlockStateContext = createContext<{ errorMessage?: string; state?: BlockState }>({})\n\n/** `data` is empty when it's left out, `null`, `''` or `[]`. */\nfunction getBlockState(status: BlockStatus | undefined, data: BlockData) {\n  if (status) return status\n  const empty =\n    data === undefined ||\n    data === null ||\n    data === '' ||\n    (Array.isArray(data) && data.length === 0)\n  return empty ? ('empty' as const) : undefined\n}\n\ninterface BlockStateProviderProps {\n  children: ReactNode\n  errorMessage?: string\n  state?: BlockState\n}\n\nfunction BlockStateProvider({ children, errorMessage, state }: BlockStateProviderProps) {\n  const outer = useContext(BlockStateContext)\n  const message = errorMessage ?? outer.errorMessage\n  const value = useMemo(() => ({ errorMessage: message, state }), [message, state])\n  return <BlockStateContext.Provider value={value}>{children}</BlockStateContext.Provider>\n}\n\nfunction useBlockState() {\n  return useContext(BlockStateContext).state\n}\n\nconst blockStateMessages = {\n  empty: 'No data',\n  error: 'Couldn’t load',\n}\n\ninterface BlockStateTextProps {\n  className?: string\n  /** Says what belongs here when there's nothing yet, in place of \"No data\". */\n  emptyMessage?: ReactNode\n  /** Shows this state instead of the block's own. */\n  state?: BlockState\n  /** `short` sizes the loading placeholder to fit beside a title. @default 'sentence' */\n  variant?: 'sentence' | 'short'\n}\n\nfunction BlockStateText({\n  className,\n  emptyMessage,\n  state: shown,\n  variant = 'sentence',\n}: BlockStateTextProps) {\n  const { errorMessage, state: blockState } = useContext(BlockStateContext)\n  const state = shown ?? blockState\n  if (!state) return null\n\n  if (state === 'loading') {\n    return (\n      <span data-slot='block-state-text' className={cn('block', className)}>\n        <SkeletonText className={variant === 'short' ? 'w-16' : 'w-48'} />\n        <span className='sr-only'>Loading</span>\n      </span>\n    )\n  }\n\n  return (\n    <span\n      data-slot='block-state-text'\n      className={cn(\n        'block',\n        state === 'error' ? 'text-red-700 dark:text-red-400' : 'text-muted-foreground',\n        className,\n        'text-xl font-light tracking-normal',\n      )}\n    >\n      {state === 'error' && errorMessage ? (\n        <>\n          <span className='block'>{blockStateMessages.error}</span>\n          <span className='text-muted-foreground mt-1 block max-w-prose text-sm font-normal text-pretty whitespace-normal'>\n            {errorMessage}\n          </span>\n        </>\n      ) : state === 'empty' && emptyMessage ? (\n        emptyMessage\n      ) : (\n        blockStateMessages[state]\n      )}\n    </span>\n  )\n}\n\ninterface BlockReadyProps {\n  children: ReactNode\n  /** Sizes the placeholder line shown while loading, such as `text-xs`. */\n  className?: string\n}\n\nfunction BlockReady({ children, className }: BlockReadyProps) {\n  const state = useBlockState()\n  if (!state) return children\n  if (state !== 'loading') return null\n  return (\n    <span className={cn('block', className)}>\n      <SkeletonText className='w-2/3' />\n    </span>\n  )\n}\n\ninterface BlockStateRowsProps {\n  className?: string\n  /** Says what belongs here when there's nothing yet, in place of \"No data\". */\n  emptyMessage?: ReactNode\n  /** How many rows to sketch while loading. @default 4 */\n  rows?: number\n  /** Drawn while loading in place of the default rows: the block's own rows and headings in outline, which lay themselves out, so the list keeps its shape when they arrive. `className` then styles only the message. */\n  skeleton?: ReactNode\n}\n\nfunction BlockStateRows({\n  className,\n  emptyMessage,\n  rows = 4,\n  skeleton,\n}: BlockStateRowsProps) {\n  const state = useBlockState()\n  if (!state) return null\n\n  if (state !== 'loading') {\n    return (\n      <BlockStateText\n        className={cn('py-2.5 text-sm', className)}\n        emptyMessage={emptyMessage}\n      />\n    )\n  }\n\n  if (skeleton) {\n    return (\n      <div data-slot='block-state-rows'>\n        <span className='sr-only'>Loading</span>\n        <div aria-hidden>{skeleton}</div>\n      </div>\n    )\n  }\n\n  return (\n    <div data-slot='block-state-rows' className={className}>\n      <span className='sr-only'>Loading</span>\n      <ul aria-hidden className='flex flex-col'>\n        {Array.from({ length: rows }, (_, index) => (\n          <li\n            key={index}\n            className='flex items-center gap-4 border-b py-2.5 text-sm last:border-b-0'\n          >\n            <SkeletonText className='w-1/3' />\n            <SkeletonText className='ml-auto w-16' />\n          </li>\n        ))}\n      </ul>\n    </div>\n  )\n}\n\ninterface BlockBusyProps {\n  busy: boolean\n  children: ReactNode\n  className?: string\n  label: string\n}\n\nfunction BlockBusy({ busy, children, className, label }: BlockBusyProps) {\n  return (\n    <>\n      <div\n        aria-busy={busy}\n        inert={busy}\n        className={cn(\n          'transition-opacity duration-200 motion-reduce:transition-none',\n          busy && 'opacity-50',\n          className,\n        )}\n      >\n        {children}\n      </div>\n      {/* Outside the aria-busy element, whose changes screen readers hold back. */}\n      <span role='status' className='sr-only'>\n        {busy ? `${label}…` : ''}\n      </span>\n    </>\n  )\n}\n\nfunction BlockBusyIndicator({\n  className,\n  label = 'Updating',\n}: {\n  className?: string\n  label?: string\n}) {\n  return (\n    <span\n      aria-hidden\n      className={cn(\n        'text-muted-foreground inline-flex items-center gap-1.5 text-xs',\n        className,\n      )}\n    >\n      <IconPlaceholder\n        lucide='LoaderCircleIcon'\n        tabler='IconLoader2'\n        hugeicons='Loading03Icon'\n        phosphor='CircleNotchIcon'\n        remixicon='RiLoader4Line'\n        className='size-3.5 animate-spin motion-reduce:animate-none'\n      />\n      {label}\n    </span>\n  )\n}\n\ninterface BlockMessageProps {\n  action?: ReactNode\n  className?: string\n  description?: ReactNode\n  icon?: ReactNode\n  title: string\n  tone?: 'default' | 'error'\n}\n\nfunction BlockMessage({\n  action,\n  className,\n  description,\n  icon,\n  title,\n  tone = 'default',\n}: BlockMessageProps) {\n  return (\n    <div\n      className={cn(\n        'flex flex-col items-center justify-center gap-3 px-6 py-10 text-center',\n        className,\n      )}\n    >\n      {icon && (\n        <span\n          aria-hidden\n          className={cn(\n            'flex size-10 items-center justify-center rounded-full [&_svg]:size-5',\n            tone === 'error'\n              ? 'bg-destructive/10 text-destructive'\n              : 'bg-muted text-muted-foreground',\n          )}\n        >\n          {icon}\n        </span>\n      )}\n      <div className='flex max-w-xs flex-col gap-1'>\n        <p className='text-sm font-medium'>{title}</p>\n        {description && (\n          <p className='text-muted-foreground text-sm text-pretty'>{description}</p>\n        )}\n      </div>\n      {action}\n    </div>\n  )\n}\n\ntype LoadStatus = 'idle' | 'loading' | 'refreshing' | 'success' | 'error'\n\nfunction useBlockData<T>(load: () => Promise<T>, { keepData = true } = {}) {\n  const [data, setData] = useState<T | undefined>(undefined)\n  const [error, setError] = useState<unknown>(undefined)\n  const [status, setStatus] = useState<LoadStatus>('idle')\n  const hasData = useRef(false)\n  const loadRef = useRef(load)\n\n  useEffect(() => {\n    loadRef.current = load\n  }, [load])\n\n  const reload = useCallback(\n    async (options: { clear?: boolean } = {}) => {\n      const refresh = keepData && hasData.current && !options.clear\n      if (options.clear) {\n        hasData.current = false\n        setData(undefined)\n      }\n      setStatus(refresh ? 'refreshing' : 'loading')\n      setError(undefined)\n      try {\n        const next = await loadRef.current()\n        hasData.current = true\n        startTransition(() => {\n          setData(next)\n          setStatus('success')\n        })\n      } catch (caught) {\n        startTransition(() => {\n          setError(caught)\n          setStatus('error')\n        })\n      }\n    },\n    [keepData],\n  )\n\n  const started = useRef(false)\n  useEffect(() => {\n    if (started.current) return\n    started.current = true\n    void reload()\n  }, [reload])\n\n  return { data, error, reload, status }\n}\n\nexport {\n  BlockBusy,\n  BlockBusyIndicator,\n  BlockMessage,\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  BlockStateText,\n  Skeleton,\n  SkeletonText,\n  getBlockState,\n  useBlockData,\n  useBlockState,\n}\n\nexport type {\n  BlockBusyProps,\n  BlockData,\n  BlockMessageProps,\n  BlockReadyProps,\n  BlockState,\n  BlockStateProps,\n  BlockStateProviderProps,\n  BlockStateRowsProps,\n  BlockStateTextProps,\n  BlockStatus,\n  LoadStatus,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/block-state.tsx"
    }
  ],
  "type": "registry:component"
}
