import { useEffect, useRef } from 'react';
import { FileIcon } from '@renderer/components/team/editor/FileIcon';
import { getTeamColorSet } from '@renderer/constants/teamColors';
import { nameColorSet } from '@renderer/utils/projectColor';
import { Loader2, UsersRound } from 'lucide-react';
import type { MentionSuggestion } from '@renderer/types/mention';
interface MentionSuggestionListProps {
suggestions: MentionSuggestion[];
selectedIndex: number;
onSelect: (s: MentionSuggestion) => void;
query: string;
/** When true, adjusts empty state text to mention files */
hasFileSearch?: boolean;
/** When true, shows a loading spinner for file search */
filesLoading?: boolean;
}
const HighlightedName = ({ name, query }: { name: string; query: string }): React.JSX.Element => {
if (!query) return {name};
const lower = name.toLowerCase();
const qLower = query.toLowerCase();
const idx = lower.indexOf(qLower);
if (idx < 0) return {name};
const before = name.slice(0, idx);
const match = name.slice(idx, idx + query.length);
const after = name.slice(idx + query.length);
return (
{before}
{match}
{after}
);
};
/** Section header for grouped suggestion lists */
const SectionHeader = ({ label }: { label: string }): React.JSX.Element => (
{label}
);
export const MentionSuggestionList = ({
suggestions,
selectedIndex,
onSelect,
query,
hasFileSearch,
filesLoading,
}: MentionSuggestionListProps): React.JSX.Element => {
const listRef = useRef(null);
useEffect(() => {
const list = listRef.current;
if (!list) return;
// Query by role=option to skip section headers
const options = list.querySelectorAll('[role="option"]');
const selected = options[selectedIndex] as HTMLElement | undefined;
selected?.scrollIntoView({ block: 'nearest' });
}, [selectedIndex]);
if (suggestions.length === 0) {
return (
{hasFileSearch ? 'No matching members, teams, or files' : 'No matching members'}
);
}
// Categorize suggestions
type Section = 'member' | 'team' | 'file';
const getSuggestionSection = (s: MentionSuggestion): Section => {
if (s.type === 'file') return 'file';
if (s.type === 'team') return 'team';
return 'member';
};
const sectionLabel: Record = {
member: 'Members',
team: 'Teams',
file: 'Files',
};
// Determine which sections are present
const presentSections = new Set(suggestions.map(getSuggestionSection));
const showSections = presentSections.size > 1;
// Build items with section headers inserted
const items: React.JSX.Element[] = [];
let currentSection: Section | null = null;
let optionIndex = 0;
for (const s of suggestions) {
const section = getSuggestionSection(s);
const isFile = section === 'file';
const isTeam = section === 'team';
// Insert section header on transition
if (showSections && section !== currentSection) {
items.push();
currentSection = section;
}
const isSelected = optionIndex === selectedIndex;
const colorSet = isFile
? null
: s.color
? getTeamColorSet(s.color)
: isTeam
? nameColorSet(s.name)
: null;
const idx = optionIndex;
optionIndex++;
items.push(
{
e.preventDefault();
onSelect(s);
}}
>
{isFile ? (
) : isTeam ? (
) : (
)}
{isTeam && s.isOnline !== undefined ? (
) : null}
{s.subtitle ? (
{s.subtitle}
) : null}
);
}
return (
{items}
{filesLoading ? (
-
Searching files...
) : null}
);
};