Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

2.0 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import type React from "react";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { useCellIds } from "@/core/cells/cells";
import type { CellId } from "@/core/cells/ids";
import { CellLink } from "./cell-link";

interface Props {
  maxCount: number;
  cellIds: CellId[];
  skipScroll?: boolean;
  onClick?: (cellId: CellId) => void;
}

export const CellLinkList: React.FC<Props> = ({
  maxCount,
  cellIds,
  onClick,
  skipScroll,
}) => {
  const cellIndex = useCellIds();
  const sortedCellIds = cellIds.toSorted((a, b) => {
    return cellIndex.indexOf(a) - cellIndex.indexOf(b);
  });

  if (cellIds.length === 0) {
    return <div className="text-muted-foreground">--</div>;
  }

  return (
    <>
      {sortedCellIds.slice(0, maxCount).map((cellId, idx) => (
        <span className="truncate" key={cellId}>
          <CellLink
            variant="focus"
            key={cellId}
            cellId={cellId}
            skipScroll={skipScroll}
            className="whitespace-nowrap"
            onClick={onClick ? () => onClick(cellId) : undefined}
          />
          {idx < cellIds.length - 1 && ", "}
        </span>
      ))}
      {cellIds.length > maxCount && (
        <Popover>
          <PopoverTrigger asChild={true}>
            <span className="whitespace-nowrap text-muted-foreground text-xs hover:underline cursor-pointer">
              +{cellIds.length - maxCount} more
            </span>
          </PopoverTrigger>
          <PopoverContent className="w-auto">
            <div className="flex flex-col gap-1 py-1">
              {sortedCellIds.slice(maxCount).map((cellId) => (
                <CellLink
                  variant="focus"
                  key={cellId}
                  cellId={cellId}
                  className="whitespace-nowrap"
                  onClick={onClick ? () => onClick(cellId) : undefined}
                />
              ))}
            </div>
          </PopoverContent>
        </Popover>
      )}
    </>
  );
};