Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

2.4 KB
Raw Blame
import { type ReactNode } from 'react';
import { CircleDashed } from 'lucide-react';
import { useTranslations } from 'next-intl';
import type { MemberScope } from '@/lib/api/endpoints/customFields';
import type { Assignee } from '@/lib/api/endpoints/projects';
import Avatar from '@/components/common/Avatar';
import { Pill } from '@/components/common/fields/Pill';
import PopoverPick from '@/components/common/fields/PopoverPick';
import { memberCandidates } from '@/utils/memberFields';
import { useCandidatePickItem } from '../../hooks/useCandidatePickItem';

// The candidates the field's scope offers, listed with the people before the agents.
function candidates(assignees: Assignee[], scope: MemberScope): Assignee[] {
  const offered = memberCandidates(assignees, scope);
  return [
    ...offered.filter((a) => a.kind === 'member'),
    ...offered.filter((a) => a.kind === 'agent'),
  ];
}

// The person or agent a member custom field holds. Which of them the field offers is
// its scope, chosen when the field was created. Shaped like AssigneeSelect and
// DelegateSelect, which do the same for the built-in fields.
export default function MemberSelect({
  assignees,
  scope,
  value,
  onChange,
  placeholder,
  trigger,
}: {
  assignees: Assignee[];
  scope: MemberScope;
  value: string | null;
  onChange: (userId: string | null) => void;
  placeholder?: string;
  // Replaces the pill where a caller needs its own (a Timeline cell).
  trigger?: ReactNode;
}) {
  const t = useTranslations('issue.fieldSelects');
  const none = t('noMember');
  const options = candidates(assignees, scope);
  const selected = options.find((a) => a.userId === value);
  const toItem = useCandidatePickItem(assignees, value, onChange);

  return (
    <PopoverPick
      trigger={
        trigger ?? (
          <Pill active={!!selected}>
            {selected ? (
              <Avatar name={selected.name} image={selected.image} className="size-4 text-[8px]" />
            ) : (
              <CircleDashed />
            )}
            <span className="truncate">{selected?.name ?? placeholder ?? none}</span>
          </Pill>
        )
      }
      inputPlaceholder={t('pickMember')}
      emptyText={t('noCandidates')}
      items={[
        {
          key: 'none',
          search: none,
          icon: <CircleDashed />,
          label: none,
          selected: value == null,
          onSelect: () => onChange(null),
        },
        ...options.map(toItem),
      ]}
    />
  );
}