Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.1 KB
Raw Blame
'use client';

import {
  ActionIcon,
  createStaticStyles,
  Flexbox,
  Icon,
  Popover,
  Select,
  Switch,
  Tabs,
} from '@lobehub/ui';
import { Form, type FormFieldProps, useForm } from '@lobehub/ui/form';
import {
  ArrowDownWideNarrow,
  ArrowUpNarrowWide,
  LayoutGrid,
  LayoutList,
  Settings2Icon,
} from 'lucide-react';
import { memo, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useHeaderActionSize } from '@/styles/density';

import type { AgentGroupBy, AgentListViewOptions, AgentOrderBy } from './listViewOptions';

type ViewMode = 'card' | 'list';

interface ListConfigProps {
  options: AgentListViewOptions;
  setOptions: (updater: (prev: AgentListViewOptions) => AgentListViewOptions) => void;
  setViewMode: (mode: ViewMode) => void;
  /** Author-based grouping/ordering only makes sense inside a workspace. */
  showAuthor?: boolean;
  viewMode: ViewMode;
}

const styles = createStaticStyles(({ css, cssVar }) => {
  return {
    form: css`
      label,
      label * {
        font-size: 13px !important;
        color: ${cssVar.colorTextSecondary} !important;
      }
    `,
  };
});

const ListConfig = memo<ListConfigProps>(
  ({ options, setOptions, setViewMode, showAuthor, viewMode }) => {
    const [open, setOpen] = useState(false);
    const form = useForm();
    const { t } = useTranslation('common');
    const headerActionSmallSize = useHeaderActionSize(true);

    const groupingOptions = useMemo<Array<{ label: string; value: AgentGroupBy }>>(
      () => [
        { label: t('agentViewAll.groupBy.none'), value: 'none' },
        ...(showAuthor
          ? [{ label: t('agentViewAll.groupBy.author'), value: 'author' as const }]
          : []),
        { label: t('agentViewAll.groupBy.folder'), value: 'folder' as const },
        { label: t('agentViewAll.groupBy.label'), value: 'label' },
      ],
      [showAuthor, t],
    );
    const orderOptions = useMemo<Array<{ label: string; value: AgentOrderBy }>>(
      () => [
        { label: t('agentViewAll.orderBy.updatedAt'), value: 'updatedAt' },
        ...(showAuthor
          ? [{ label: t('agentViewAll.orderBy.author'), value: 'author' as const }]
          : []),
        { label: t('agentViewAll.orderBy.title'), value: 'title' },
      ],
      [showAuthor, t],
    );

    const formItems: FormFieldProps[] = [
      {
        children: (
          <Select
            options={groupingOptions}
            size={'small'}
            style={{ width: 150 }}
            value={options.groupBy}
            onChange={(value: AgentGroupBy) => {
              setOptions((prev) => ({ ...prev, groupBy: value }));
            }}
          />
        ),
        label: t('agentViewAll.form.grouping'),
      },
      {
        children: (
          <Flexbox horizontal align={'center'} gap={8}>
            <ActionIcon
              icon={options.orderDirection === 'asc' ? ArrowUpNarrowWide : ArrowDownWideNarrow}
              size={'small'}
              onClick={() => {
                setOptions((prev) => ({
                  ...prev,
                  orderDirection: prev.orderDirection === 'asc' ? 'desc' : 'asc',
                }));
              }}
            />
            <Select
              options={orderOptions}
              size={'small'}
              style={{ width: 112 }}
              value={options.orderBy}
              onChange={(value: AgentOrderBy) => {
                setOptions((prev) => ({ ...prev, orderBy: value }));
              }}
            />
          </Flexbox>
        ),
        label: t('agentViewAll.form.ordering'),
      },
      {
        children: (
          <Switch
            checked={options.showSidebarHidden}
            size={'small'}
            onChange={(checked) => {
              setOptions((prev) => ({ ...prev, showSidebarHidden: checked }));
            }}
          />
        ),
        minWidth: undefined,
        label: t('agentViewAll.form.showSidebarHidden'),
      },
    ];

    const panelContent = (
      <Flexbox gap={12} width={280}>
        <Tabs
          activeKey={viewMode}
          items={[
            { icon: <Icon icon={LayoutList} />, key: 'list', label: t('agentViewAll.view.list') },
            { icon: <Icon icon={LayoutGrid} />, key: 'card', label: t('agentViewAll.view.card') },
          ]}
          styles={{
            list: { display: 'flex', width: '100%' },
            tab: { flex: 1 },
          }}
          onChange={(key) => setViewMode(key as ViewMode)}
        />
        <Form
          className={styles.form}
          form={form}
          items={formItems}
          itemsType={'flat'}
          variant={'borderless'}
          styles={{
            item: { paddingBlock: 4, paddingInline: 0 },
          }}
        />
      </Flexbox>
    );

    return (
      <Popover
        arrow={false}
        content={panelContent}
        open={open}
        placement={'bottomRight'}
        trigger={['click']}
        onOpenChange={setOpen}
      >
        <ActionIcon icon={Settings2Icon} size={headerActionSmallSize} />
      </Popover>
    );
  },
);

ListConfig.displayName = 'AgentViewAllListConfig';

export default ListConfig;