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.

9.0 KB
Raw Blame
import type { SystemStyleObject } from '@chakra-ui/react';

import { Box, Flex, Icon, SimpleGrid, Skeleton, Text } from '@chakra-ui/react';
import { Button } from '@platform/ui/Button';
import { EmptyState } from '@platform/ui/EmptyState';
import { Scrollable } from '@platform/ui/Scrollable';
import { useProjectLibrarySelector } from '@workbench/projects/library';
import { useOpenProjectsSelector } from '@workbench/projects/openProjects';
import { FolderIcon, SearchXIcon } from 'lucide-react';
import { useCallback, useEffectEvent, useLayoutEffect, useMemo, useRef } from 'react';
import { useVirtualizer } from 'react-hook-tanstack-virtual';
import { useTranslation } from 'react-i18next';

import type { ProjectGroupId, ProjectRowModel, ProjectSortId, ProjectsViewId } from './projectLibraryView';

import { NewProjectButton } from './NewProjectButton';
import { ProjectCard } from './ProjectCard';
import { PROJECT_COVER_ASPECT_RATIO } from './ProjectCover';
import { buildProjectGroups, flattenProjectGroupsToRows } from './projectLibraryView';
import { ProjectRow } from './ProjectRow';
import { firstControlIn, ProjectsLoadError } from './ProjectsLoadError';
import {
  getProjectsGridRowHeight,
  PROJECT_GROUP_HEADER_HEIGHT_PX,
  PROJECT_ROW_HEIGHT_PX,
  PROJECTS_GRID_GAP_PX,
  useProjectsMetrics,
} from './projectsMetrics';

/** Virtualize derived-height rows in both layouts: grid rows hold multiple cards, list rows one. */

const MEASURE_SX: SystemStyleObject = {
  maxW: '6xl',
  mx: 'auto',
  px: { base: 4, md: 8 },
  w: 'full',
};

const GROUP_LABEL_KEY: Record<ProjectGroupId, string> = {
  all: 'projects.groups.all',
  month: 'projects.groups.month',
  older: 'projects.groups.older',
  open: 'projects.groups.open',
  pinned: 'projects.groups.pinned',
  today: 'projects.groups.today',
  week: 'projects.groups.week',
};

const SKELETON_GRID_COLUMNS = { base: 1, lg: 3, sm: 2 } as const;
const NO_MATCHES_ICON = <Icon as={SearchXIcon} />;
const NO_PROJECTS_ICON = <Icon as={FolderIcon} />;

export interface ProjectsBrowserProps {
  /** Captured once by the page: reading the clock during render would rebucket unstably. */
  now: number;
  pinnedIds: readonly string[];
  searchTerm: string;
  sort: ProjectSortId;
  view: ProjectsViewId;
  onClearSearch: () => void;
  onTogglePin: (projectId: string) => void;
}

export const ProjectsBrowser = ({
  now,
  onClearSearch,
  onTogglePin,
  pinnedIds,
  searchTerm,
  sort,
  view,
}: ProjectsBrowserProps) => {
  const { t } = useTranslation();
  const status = useProjectLibrarySelector((snapshot) => snapshot.status);
  const summaries = useProjectLibrarySelector((snapshot) => snapshot.summaries);
  const openProjectIds = useOpenProjectsSelector((snapshot) => snapshot.ids);

  const scrollRef = useRef<HTMLDivElement | null>(null);
  const { columnCount, measureRef, widthPx } = useProjectsMetrics(view);

  const rows = useMemo(
    () =>
      flattenProjectGroupsToRows(
        buildProjectGroups({
          now,
          openProjectIds: openProjectIds ?? [],
          pinnedIds,
          searchTerm,
          sort,
          summaries,
        }),
        columnCount
      ),
    [columnCount, now, openProjectIds, pinnedIds, searchTerm, sort, summaries]
  );

  const itemRowHeight =
    view === 'grid'
      ? getProjectsGridRowHeight({ aspectRatio: PROJECT_COVER_ASPECT_RATIO, columnCount, widthPx })
      : PROJECT_ROW_HEIGHT_PX;

  const estimateSize = useCallback(
    (index: number) => (rows[index]?.kind === 'header' ? PROJECT_GROUP_HEADER_HEIGHT_PX : itemRowHeight),
    [itemRowHeight, rows]
  );
  const getItemKey = useCallback(
    (index: number) => {
      const row = rows[index];

      if (!row) {
        return index;
      }

      return row.kind === 'header'
        ? `header:${row.group}`
        : `${row.group}:${row.projects.map((project) => project.id).join(',')}`;
    },
    [rows]
  );
  const getScrollElement = useCallback(() => scrollRef.current, []);
  const getRecoveryFocus = useCallback(() => firstControlIn(scrollRef.current), []);

  const virtualizer = useVirtualizer({
    count: rows.length,
    estimateSize,
    getItemKey,
    getScrollElement,
    overscan: 4,
    // useSyncExternalStore already commits scroll snapshots synchronously; flushSync would only add a React error for
    // any scroll event dispatched while React is rendering or flushing effects.
    useFlushSync: false,
  });

  // The virtualizer caches measurements and will not re-read `estimateSize` on its own, so switching layouts or
  // resizing the panel has to invalidate it — otherwise list rows keep the taller grid row's spacing.
  const measureVirtualizer = useEffectEvent(() => {
    virtualizer.measure();
  });

  useLayoutEffect(() => {
    measureVirtualizer();
  }, [itemRowHeight, view]);

  const isFirstLoad = summaries.length === 0 && (status === 'idle' || status === 'loading');
  const isSearching = searchTerm.trim().length > 0;

  return (
    <Scrollable flex="1" h="full" label={t('launchpad.sections.projects')} minH="0" viewportRef={scrollRef}>
      <Box css={MEASURE_SX} pb="8" ref={measureRef}>
        {status === 'error' ? (
          <ProjectsLoadError
            getRecoveryFocus={getRecoveryFocus}
            layout={summaries.length === 0 ? 'state' : 'inline'}
            mb="4"
          />
        ) : null}

        {/* A failed listing is not an empty library: the alert above is the whole state until a retry lands. */}
        {status === 'error' && summaries.length === 0 ? null : isFirstLoad ? (
          <SimpleGrid columns={SKELETON_GRID_COLUMNS} gap="4">
            <Skeleton minH="52" rounded="lg" />
            <Skeleton minH="52" rounded="lg" />
            <Skeleton minH="52" rounded="lg" />
          </SimpleGrid>
        ) : rows.length === 0 ? (
          <ProjectsEmptyState isSearching={isSearching} searchTerm={searchTerm} onClearSearch={onClearSearch} />
        ) : (
          <Box height={`${virtualizer.totalSize}px`} position="relative" w="full">
            {virtualizer.virtualItems.map((virtualRow) => {
              const row = rows[virtualRow.index];

              if (!row) {
                return null;
              }

              return (
                <VirtualProjectsRow
                  columnCount={columnCount}
                  key={virtualRow.key}
                  pinnedIds={pinnedIds}
                  row={row}
                  view={view}
                  virtualStart={virtualRow.start}
                  onTogglePin={onTogglePin}
                />
              );
            })}
          </Box>
        )}
      </Box>
    </Scrollable>
  );
};

const ABSOLUTE_ROW_SX: SystemStyleObject = { insetInline: 0, position: 'absolute', top: 0 };

const VirtualProjectsRow = ({
  columnCount,
  onTogglePin,
  pinnedIds,
  row,
  view,
  virtualStart,
}: {
  columnCount: number;
  pinnedIds: readonly string[];
  row: ProjectRowModel;
  view: ProjectsViewId;
  virtualStart: number;
  onTogglePin: (projectId: string) => void;
}) => {
  const { t } = useTranslation();
  const transform = `translateY(${virtualStart}px)`;

  if (row.kind === 'header') {
    return (
      <Flex align="baseline" css={ABSOLUTE_ROW_SX} gap="2" pb="2" pt="3" transform={transform}>
        <Text fontWeight="700">{t(GROUP_LABEL_KEY[row.group])}</Text>
        {/* `fg.subtle` only reaches 4.11:1 at this size — `fg.muted` clears 4.5:1. */}
        <Text color="fg.muted" fontSize="xs">
          {row.count}
        </Text>
      </Flex>
    );
  }

  return (
    <Box css={ABSOLUTE_ROW_SX} transform={transform}>
      {view === 'grid' ? (
        <SimpleGrid columns={columnCount} gap={`${PROJECTS_GRID_GAP_PX}px`}>
          {row.projects.map((project) => (
            <ProjectCard
              isPinned={pinnedIds.includes(project.id)}
              key={project.id}
              summary={project}
              onTogglePin={onTogglePin}
            />
          ))}
        </SimpleGrid>
      ) : (
        row.projects.map((project) => (
          // Let the wrapper own full row pitch; rounded hover fills separate items without intersecting dividers.
          <Box h={`${PROJECT_ROW_HEIGHT_PX}px`} key={project.id}>
            <ProjectRow isPinned={pinnedIds.includes(project.id)} summary={project} onTogglePin={onTogglePin} />
          </Box>
        ))
      )}
    </Box>
  );
};

const ProjectsEmptyState = ({
  isSearching,
  onClearSearch,
  searchTerm,
}: {
  isSearching: boolean;
  searchTerm: string;
  onClearSearch: () => void;
}) => {
  const { t } = useTranslation();

  if (isSearching) {
    return (
      <EmptyState
        align="start"
        description={t('projects.noSearchMatchesDescription')}
        icon={NO_MATCHES_ICON}
        title={t('projects.noSearchMatches', { search: searchTerm.trim() })}
      >
        <Button variant="outline" onClick={onClearSearch}>
          {t('common.clearSearch')}
        </Button>
      </EmptyState>
    );
  }

  return (
    <EmptyState
      align="start"
      description={t('projects.noSavedProjectsDescription')}
      icon={NO_PROJECTS_ICON}
      title={t('projects.noSavedProjects')}
    >
      <NewProjectButton variant="outline" />
    </EmptyState>
  );
};