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.

5.1 KB
Raw Blame
import { Flex, Icon, Input, InputGroup, Menu, Portal, SegmentGroup } from '@chakra-ui/react';
import { Button, IconButton } from '@platform/ui/Button';
import { MenuContent } from '@platform/ui/Menu';
import { Tooltip } from '@platform/ui/Tooltip';
import { ArrowUpDownIcon, CheckIcon, LayoutGridIcon, ListIcon, SearchIcon, XIcon } from 'lucide-react';
import { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

/* eslint-disable react-perf/jsx-no-jsx-as-prop */
import type { ProjectSortId, ProjectsViewId } from './projectLibraryView';

import { isProjectsViewId, PROJECT_SORT_IDS, PROJECTS_VIEW_IDS } from './projectLibraryView';

const MENU_POSITIONING = { placement: 'bottom-end' } as const;
const SEARCH_ICON = <Icon as={SearchIcon} boxSize="3.5" color="fg.subtle" />;

const SORT_LABEL_KEY: Record<ProjectSortId, string> = {
  created: 'projects.sort.created',
  edited: 'projects.sort.edited',
  name: 'projects.sort.name',
};

const VIEW_LABEL_KEY: Record<ProjectsViewId, string> = {
  grid: 'projects.gridView',
  list: 'projects.listView',
};

const VIEW_ICON: Record<ProjectsViewId, typeof LayoutGridIcon> = {
  grid: LayoutGridIcon,
  list: ListIcon,
};

/** Tooltip on the icon so the item keeps its checked data-state; hidden-radio focus drives it. */
const ViewSegment = ({ id }: { id: ProjectsViewId }) => {
  const { t } = useTranslation();
  const [isTooltipOpen, setIsTooltipOpen] = useState(false);
  const handleTooltipOpenChange = useCallback((details: { open: boolean }) => setIsTooltipOpen(details.open), []);
  const showTooltip = useCallback(() => setIsTooltipOpen(true), []);
  const hideTooltip = useCallback(() => setIsTooltipOpen(false), []);
  const label = t(VIEW_LABEL_KEY[id]);

  return (
    <SegmentGroup.Item px="2" value={id}>
      <SegmentGroup.ItemHiddenInput aria-label={label} onBlur={hideTooltip} onFocus={showTooltip} />
      <SegmentGroup.ItemText display="flex">
        <Tooltip content={label} open={isTooltipOpen} onOpenChange={handleTooltipOpenChange}>
          <Icon as={VIEW_ICON[id]} boxSize="3.5" />
        </Tooltip>
      </SegmentGroup.ItemText>
    </SegmentGroup.Item>
  );
};

export const ProjectsToolbar = ({
  searchTerm,
  sort,
  view,
  onSearchTermChange,
  onSortChange,
  onViewChange,
}: {
  searchTerm: string;
  sort: ProjectSortId;
  view: ProjectsViewId;
  onSearchTermChange: (value: string) => void;
  onSortChange: (sort: ProjectSortId) => void;
  onViewChange: (view: ProjectsViewId) => void;
}) => {
  const { t } = useTranslation();

  const handleSearchInput = useCallback(
    (event: React.ChangeEvent<HTMLInputElement>) => onSearchTermChange(event.target.value),
    [onSearchTermChange]
  );
  const handleClearSearch = useCallback(() => onSearchTermChange(''), [onSearchTermChange]);
  const handleSortSelect = useCallback(
    (details: { value: string }) => {
      if (PROJECT_SORT_IDS.includes(details.value as ProjectSortId)) {
        onSortChange(details.value as ProjectSortId);
      }
    },
    [onSortChange]
  );
  const handleViewChange = useCallback(
    (details: { value: string | null }) => {
      if (isProjectsViewId(details.value)) {
        onViewChange(details.value);
      }
    },
    [onViewChange]
  );

  return (
    <Flex align="center" gap="2" wrap="wrap">
      <InputGroup
        endElement={
          searchTerm ? (
            <IconButton aria-label={t('common.clearSearch')} size="sm" variant="ghost" onClick={handleClearSearch}>
              <Icon as={XIcon} boxSize="3" />
            </IconButton>
          ) : undefined
        }
        flex="1"
        maxW="xs"
        minW="3xs"
        startElement={SEARCH_ICON}
      >
        <Input
          aria-label={t('projects.searchLabel')}
          placeholder={t('projects.searchPlaceholder')}
          type="search"
          value={searchTerm}
          onChange={handleSearchInput}
        />
      </InputGroup>

      <Menu.Root positioning={MENU_POSITIONING} onSelect={handleSortSelect}>
        <Menu.Trigger asChild>
          <Button ms="auto" variant="outline">
            <Icon as={ArrowUpDownIcon} boxSize="3.5" />
            {t(SORT_LABEL_KEY[sort])}
          </Button>
        </Menu.Trigger>
        <Portal>
          <Menu.Positioner>
            <MenuContent minW="44">
              <Menu.ItemGroup>
                <Menu.ItemGroupLabel color="fg.subtle" fontSize="xs" textTransform="uppercase">
                  {t('projects.sort.label')}
                </Menu.ItemGroupLabel>
                {PROJECT_SORT_IDS.map((id) => (
                  <Menu.Item key={id} value={id}>
                    <Icon as={CheckIcon} boxSize="3.5" color="fg" opacity={id === sort ? 1 : 0} />
                    <Menu.ItemText>{t(SORT_LABEL_KEY[id])}</Menu.ItemText>
                  </Menu.Item>
                ))}
              </Menu.ItemGroup>
            </MenuContent>
          </Menu.Positioner>
        </Portal>
      </Menu.Root>

      <SegmentGroup.Root aria-label={t('projects.viewLabel')} value={view} onValueChange={handleViewChange}>
        <SegmentGroup.Indicator />
        {PROJECTS_VIEW_IDS.map((id) => (
          <ViewSegment key={id} id={id} />
        ))}
      </SegmentGroup.Root>
    </Flex>
  );
};