The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { styled } from '@linaria/react';
import { type MouseEvent, useId } from 'react';
import { useLingui } from '@lingui/react/macro';
import { Key } from 'ts-key-enum';
import { isDefined } from 'twenty-shared/utils';
import { themeCssVariables } from 'twenty-ui/theme';
import { AiChatThreadActionsDropdown } from '@/ai/components/AiChatThreadActionsDropdown';
import { AiChatThreadActivityTime } from '@/ai/components/AiChatThreadActivityTime';
import { AiChatThreadAvatar } from '@/ai/components/AiChatThreadAvatar';
import { AiChatThreadChannelIcon } from '@/ai/components/AiChatThreadChannelIcon';
import { AiChatThreadRow } from '@/ai/components/AiChatThreadRow';
import { AiChatThreadSubtitle } from '@/ai/components/AiChatThreadSubtitle';
import { AiChatThreadTitle } from '@/ai/components/AiChatThreadTitle';
import { useAgentChatThreadSubtitleContent } from '@/ai/hooks/useAgentChatThreadSubtitleContent';
import { useAiChatThreadRename } from '@/ai/hooks/useAiChatThreadRename';
import { useAiChatThreadTriage } from '@/ai/hooks/useAiChatThreadTriage';
import { agentChatIsStreamingFamilyState } from '@/ai/states/agentChatIsStreamingFamilyState';
import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';
import { type AiChatThreadListVariant } from '@/ai/types/AiChatThreadListVariant';
import { getCommandMenuDropdownIdFromCommandMenuId } from '@/command-menu-item/utils/getCommandMenuDropdownIdFromCommandMenuId';
import { TextInput } from '@/ui/input/components/TextInput';
import { isDropdownOpenComponentState } from '@/ui/layout/dropdown/states/isDropdownOpenComponentState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { useAtomFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilyStateValue';
const StyledActivityTime = styled.div`
align-items: center;
display: flex;
gap: ${themeCssVariables.spacing[1]};
`;
type AiChatThreadListItemProps = {
thread: AgentChatThreadRecord;
variant?: AiChatThreadListVariant;
isSelected: boolean;
isChecked?: boolean;
onClick: (
thread: AgentChatThreadRecord,
event: MouseEvent<HTMLDivElement>,
) => void;
onCheckboxClick?: (
thread: AgentChatThreadRecord,
event: MouseEvent<HTMLDivElement>,
) => void;
onContextMenu?: (
thread: AgentChatThreadRecord,
event: MouseEvent<HTMLDivElement>,
) => void;
onDetach?: () => void;
};
export const AiChatThreadListItem = ({
thread,
variant = 'default',
isSelected,
isChecked = false,
onClick,
onCheckboxClick,
onContextMenu,
onDetach,
}: AiChatThreadListItemProps) => {
const { t } = useLingui();
const {
isRenaming,
draftTitle,
setDraftTitle,
startRename,
cancelRename,
commitRename,
} = useAiChatThreadRename(thread);
const actionsInstanceId = useId();
const itemMenuDropdownId =
getCommandMenuDropdownIdFromCommandMenuId(actionsInstanceId);
const isDropdownOpen = useAtomComponentStateValue(
isDropdownOpenComponentState,
itemMenuDropdownId,
);
const { status: triageStatus } = useAiChatThreadTriage({
threadId: thread.id,
});
const agentChatIsStreaming = useAtomFamilyStateValue(
agentChatIsStreamingFamilyState,
{ threadId: thread.id },
);
const { hasContent: hasSubtitleContent } =
useAgentChatThreadSubtitleContent(thread);
const isCompact = variant === 'compact';
const shouldShowMore =
isDefined(onDetach) ||
isDefined(thread.deletedAt) ||
!isDefined(triageStatus) ||
triageStatus === 'open';
return (
<AiChatThreadRow
variant={variant}
id={thread.id}
label={thread.title ?? t`Untitled`}
isSelected={isSelected}
isChecked={isChecked}
isEditing={isRenaming}
isActionMenuOpen={isDropdownOpen}
isStreaming={agentChatIsStreaming}
onClick={(event) => onClick(thread, event)}
onCheckboxClick={
isDefined(onCheckboxClick)
? (event) => onCheckboxClick(thread, event)
: undefined
}
onContextMenu={
isDefined(onContextMenu)
? (event) => onContextMenu(thread, event)
: undefined
}
avatar={
<AiChatThreadAvatar thread={thread} size={isCompact ? 'md' : 'lg'} />
}
title={
isRenaming ? (
<TextInput
value={draftTitle}
onChange={setDraftTitle}
onClick={(event) => event.stopPropagation()}
onFocus={(event) => event.target.select()}
onBlur={() => commitRename(draftTitle)}
onKeyDown={(event) => {
if (event.nativeEvent.isComposing || event.keyCode === 229) {
return;
}
if (event.key === Key.Enter) {
event.preventDefault();
void commitRename(draftTitle);
return;
}
if (event.key === Key.Escape) {
event.preventDefault();
cancelRename();
}
}}
sizeVariant="sm"
fullWidth
autoFocus
aria-label={t`Rename chat`}
/>
) : (
<AiChatThreadTitle thread={thread} />
)
}
activityTime={
!isRenaming && (
<StyledActivityTime>
<AiChatThreadChannelIcon thread={thread} />
<AiChatThreadActivityTime thread={thread} variant={variant} />
</StyledActivityTime>
)
}
subtitle={
!isCompact || hasSubtitleContent ? (
<AiChatThreadSubtitle
thread={thread}
size={isCompact ? 'md' : 'sm'}
/>
) : undefined
}
actions={
<AiChatThreadActionsDropdown
thread={thread}
instanceId={actionsInstanceId}
onRenameRequested={startRename}
onDetach={onDetach}
shouldShowTriageActions={!isCompact}
shouldShowMore={shouldShowMore}
/>
}
/>
);
};