Home / Function/ ColumnHeader() — supabase Function Reference

ColumnHeader() — supabase Function Reference

Architecture documentation for the ColumnHeader() function in ColumnHeader.tsx from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  829fc631_c9b5_db3a_ad2d_70f659658090["ColumnHeader()"]
  46da82b4_598b_8aca_ed52_d820fc6b9581["getColumnFormat()"]
  829fc631_c9b5_db3a_ad2d_70f659658090 -->|calls| 46da82b4_598b_8aca_ed52_d820fc6b9581
  4c55560d_a21c_d069_5fc3_c55491f148ae["useColumnHasIndexSuggestion()"]
  829fc631_c9b5_db3a_ad2d_70f659658090 -->|calls| 4c55560d_a21c_d069_5fc3_c55491f148ae
  a6ca5428_7494_270c_e476_5f17438b8f0e["useTableIndexAdvisor()"]
  829fc631_c9b5_db3a_ad2d_70f659658090 -->|calls| a6ca5428_7494_270c_e476_5f17438b8f0e
  91aa6416_9033_4300_865c_58f97060580f["renderColumnIcon()"]
  829fc631_c9b5_db3a_ad2d_70f659658090 -->|calls| 91aa6416_9033_4300_865c_58f97060580f
  style 829fc631_c9b5_db3a_ad2d_70f659658090 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/grid/components/grid/ColumnHeader.tsx lines 17–183

export function ColumnHeader<R>({
  column,
  columnType,
  isPrimaryKey,
  isEncrypted,
  format,
  foreignKey,
  comment,
}: ColumnHeaderProps<R>) {
  const ref = useRef<HTMLDivElement>(null)
  const columnIdx = column.idx
  const columnKey = column.key
  const columnFormat = getColumnFormat(columnType, format)
  const snap = useTableEditorTableStateSnapshot()
  const hoverValue = column.name as string
  const hasIndexSuggestion = useColumnHasIndexSuggestion(column.name as string)
  const { openSheet } = useTableIndexAdvisor()

  // keep snap.gridColumns' order in sync with data grid component
  useEffect(() => {
    const snapGridColumnKey = snap.gridColumns[columnIdx]?.key

    if (snapGridColumnKey != columnKey) {
      snap.updateColumnIdx(columnKey, columnIdx)
    }
  }, [columnKey, columnIdx, snap.gridColumns])

  const [{ isDragging }, drag] = useDrag({
    type: 'column-header',
    item: () => {
      return { key: columnKey, index: columnIdx } as DragItem
    },
    canDrag: () => !column.frozen,
    collect: (monitor) => ({
      isDragging: monitor.isDragging(),
    }),
  })

  const [{ handlerId }, drop] = useDrop({
    accept: 'column-header',
    collect(monitor) {
      return {
        handlerId: monitor.getHandlerId(),
      }
    },
    hover(item, monitor) {
      if (!ref.current) {
        return
      }

      if (column.frozen) {
        return
      }

      const dragIndex = (item as DragItem).index
      const dragKey = (item as DragItem).key
      const hoverIndex = columnIdx
      const hoverKey = columnKey

      // Don't replace items with themselves
      if (dragIndex === hoverIndex) {
        return
      }

      // Determine rectangle on screen
      const hoverBoundingRect = ref.current?.getBoundingClientRect()

      // Get horizontal middle
      const hoverMiddleX = (hoverBoundingRect.right - hoverBoundingRect.left) / 2

      // Determine mouse position
      const clientOffset = monitor.getClientOffset()

      // Get pixels to the top
      const hoverClientX = (clientOffset as XYCoord).x - hoverBoundingRect.left

      // Only perform the move when the mouse has crossed half of the items width

      // Dragging left
      if (dragIndex < hoverIndex && hoverClientX < hoverMiddleX) {
        return
      }

      // Dragging right
      if (dragIndex > hoverIndex && hoverClientX > hoverMiddleX) {
        return
      }

      // Time to actually perform the action
      snap.moveColumn(dragKey, hoverKey)

      // Note: we're mutating the monitor item here!
      // Generally it's better to avoid mutations,
      // but it's good here for the sake of performance
      // to avoid expensive index searches.
      ;(item as DragItem).index = hoverIndex
    },
  })

  const opacity = isDragging ? 0 : 1
  const cursor = column.frozen ? 'sb-grid-column-header--cursor' : ''
  drag(drop(ref))

  return (
    <div ref={ref} data-handler-id={handlerId} style={{ opacity }} className="w-full">
      <div className={`sb-grid-column-header ${cursor}`}>
        <div className="sb-grid-column-header__inner">
          {renderColumnIcon(columnType, { name: column.name as string, foreignKey })}
          {isPrimaryKey && (
            <Tooltip>
              <TooltipTrigger>
                <div className="sb-grid-column-header__inner__primary-key">
                  <Key size={14} strokeWidth={2} />
                </div>
              </TooltipTrigger>
              <TooltipContent side="bottom" className="font-normal">
                Primary key
              </TooltipContent>
            </Tooltip>
          )}

          <Tooltip>
            <TooltipTrigger>
              <span className="sb-grid-column-header__inner__name">{column.name}</span>
            </TooltipTrigger>
            {!!comment && (
              <TooltipContent side="bottom" className="max-w-xs text-center">
                {comment}
              </TooltipContent>
            )}
          </Tooltip>

          <span className="sb-grid-column-header__inner__format">
            {columnFormat}
            {columnFormat === 'bytea' ? ` (hex)` : ''}
          </span>
          {isEncrypted && (
            <Tooltip>
              <TooltipTrigger>
                <Lock size={14} strokeWidth={2} />
              </TooltipTrigger>
              <TooltipContent side="bottom" className="font-normal">
                Encrypted column
              </TooltipContent>
            </Tooltip>
          )}
          {hasIndexSuggestion && (
            <Tooltip>
              <TooltipTrigger asChild>
                <button
                  className="flex items-center"
                  onClick={() => openSheet(column.name as string)}
                >
                  <Lightbulb size={14} strokeWidth={2} className="!text-warning" />
                </button>
              </TooltipTrigger>
              <TooltipContent side="bottom" className="font-normal">
                Index might improve performance. Click for details.
              </TooltipContent>
            </Tooltip>
          )}
        </div>
        <ColumnMenu column={column} isEncrypted={isEncrypted} />
      </div>
    </div>
  )
}

Subdomains

Frequently Asked Questions

What does ColumnHeader() do?
ColumnHeader() is a function in the supabase codebase.
What does ColumnHeader() call?
ColumnHeader() calls 4 function(s): getColumnFormat, renderColumnIcon, useColumnHasIndexSuggestion, useTableIndexAdvisor.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free