Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

4.4 KB
Raw Blame
import type { ProjectSummary } from '@workbench/projects/library';
import type { MouseEvent } from 'react';

import { Box, Flex, Icon, Text } from '@chakra-ui/react';
import { IconButton } from '@platform/ui/Button';
import { MiddleTruncate } from '@platform/ui/MiddleTruncate';
import { Row } from '@platform/ui/Row';
import { Tooltip } from '@platform/ui/Tooltip';
import { Link } from '@tanstack/react-router';
import { formatRelativeTime } from '@workbench/launchpad/formatRelativeTime';
import { isProjectSummaryCompatible } from '@workbench/projects/library';
import { EllipsisVerticalIcon, PinIcon } from 'lucide-react';
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useProjectActionsMenu, useProjectActionsMenuTrigger } from './ProjectActionsMenuHost';
import { ProjectCompatibilityBadge } from './ProjectCompatibilityBadge';
import { ProjectCover } from './ProjectCover';

const LINK_STYLE = { cursor: 'default', inset: 0, outline: 'none', position: 'absolute' } as const;
const REVEAL_ON_HOVER = { opacity: 1 } as const;
const THUMBNAIL_WIDTH = '14';

export const ProjectRow = ({
  isPinned,
  summary,
  onTogglePin,
}: {
  isPinned: boolean;
  summary: ProjectSummary;
  onTogglePin: (projectId: string) => void;
}) => {
  const { t } = useTranslation();
  const menu = useProjectActionsMenu();
  const isCompatible = isProjectSummaryCompatible(summary);

  const projectSearch = useMemo(() => ({ project: summary.id }), [summary.id]);
  const pinLabel = isPinned ? t('projects.unpin') : t('projects.pin');
  const menuTarget = useMemo(() => ({ isPinned, onTogglePin, summary }), [isPinned, onTogglePin, summary]);
  const handleContextMenu = useCallback(
    (event: MouseEvent<HTMLDivElement>) => menu.openAtPointer(event, menuTarget),
    [menu, menuTarget]
  );
  const menuTrigger = useProjectActionsMenuTrigger(menuTarget);
  const handleTogglePin = useCallback(() => onTogglePin(summary.id), [onTogglePin, summary.id]);

  return (
    <Row
      className="group"
      // A project this build cannot open does not light up under the pointer as if it would open.
      data-static={isCompatible ? undefined : ''}
      gap="3"
      h="full"
      position="relative"
      px="2.5"
      rounded="md"
      onContextMenu={handleContextMenu}
    >
      {/* A project this build cannot open has no link and no tab stop; its compatibility badge says why. */}
      {isCompatible ? (
        // The row's primary control: the row recipe draws its focus ring around the whole row.
        <Link
          aria-label={t('projects.openProjectLabel', { name: summary.name })}
          data-list-primary=""
          search={projectSearch}
          style={LINK_STYLE}
          to="/app"
        />
      ) : null}
      <Box flexShrink={0} overflow="hidden" pointerEvents="none" rounded="sm" w={THUMBNAIL_WIDTH}>
        <ProjectCover coverUrl={summary.coverUrl} />
      </Box>
      {/* Centred: the smaller timestamp would otherwise sit at the top of the name's taller line. */}
      <Flex align="center" flex="1" gap="3" minW="0" pointerEvents="none">
        <MiddleTruncate flex="1" fontSize="md" fontWeight="600" minW="0" text={summary.name} />
        <ProjectCompatibilityBadge summary={summary} />
        <Text color="fg.muted" flexShrink={0} fontSize="xs">
          {t('projects.editedRelative', { time: formatRelativeTime(summary.updatedAt) })}
        </Text>
      </Flex>
      <Flex flexShrink={0} gap="0.5" pointerEvents="auto" zIndex="1">
        <Tooltip content={pinLabel}>
          <IconButton
            aria-label={pinLabel}
            aria-pressed={isPinned}
            color={isPinned ? 'fg' : 'fg.muted'}
            opacity={isPinned ? 1 : 0}
            size="sm"
            variant="ghost"
            _focusVisible={REVEAL_ON_HOVER}
            _groupHover={REVEAL_ON_HOVER}
            onClick={handleTogglePin}
          >
            <Icon as={PinIcon} boxSize="3" fill={isPinned ? 'currentColor' : 'none'} />
          </IconButton>
        </Tooltip>
        <IconButton
          aria-expanded={menuTrigger.isExpanded}
          aria-haspopup="menu"
          aria-label={t('common.actions')}
          color="fg.muted"
          size="sm"
          variant="ghost"
          onClick={menuTrigger.onClick}
          onPointerDown={menuTrigger.onPointerDown}
        >
          <EllipsisVerticalIcon />
        </IconButton>
      </Flex>
    </Row>
  );
};