1 import { useState } from 'react';
3 import { getIsAssistantOpened } from '@proton/llm/lib/helpers';
4 import useAssistantSticky from '@proton/llm/lib/hooks/useAssistantSticky';
5 import type { OpenedAssistant } from '@proton/llm/lib/types';
6 import { OpenedAssistantStatus } from '@proton/llm/lib/types';
9 cleanSpecificErrors: (assistantID: string) => void;
12 const useOpenedAssistants = ({ cleanSpecificErrors }: Props) => {
13 const [openedAssistants, setOpenedAssistants] = useState<OpenedAssistant[]>([]);
14 const { setAssistantStickyOff, setAssistantStickyOn, getIsStickyAssistant } = useAssistantSticky({
18 // By default, open the assistant with the collapsed status
19 const openAssistant = (assistantID: string, manual = false) => {
20 const newAssistant: OpenedAssistant = {
22 status: OpenedAssistantStatus.COLLAPSED,
24 setOpenedAssistants((openedAssistants) => [...openedAssistants, newAssistant]);
26 setAssistantStickyOn();
30 const setAssistantStatus = (assistantID: string, status: OpenedAssistantStatus) => {
31 const assistant = openedAssistants.find((assistant) => assistant.id === assistantID);
33 const updatedAssistant = { ...assistant, status };
35 setOpenedAssistants((openedAssistants) => {
36 const filteredAssistants = openedAssistants.filter((assistant) => assistant.id !== assistantID);
37 return [...filteredAssistants, updatedAssistant];
42 const closeAssistant =
43 (cancelRunningAction: (assistantId: string) => void) =>
44 (assistantID: string, manual = false) => {
45 const isAssistantOpened = getIsAssistantOpened(openedAssistants, assistantID);
46 if (isAssistantOpened) {
47 setOpenedAssistants((openedAssistants) => {
48 return openedAssistants.filter((assistant) => assistant.id !== assistantID);
50 cancelRunningAction(assistantID);
51 cleanSpecificErrors(assistantID);
53 setAssistantStickyOff();
58 return { openedAssistants, openAssistant, setAssistantStatus, closeAssistant, getIsStickyAssistant };
61 export default useOpenedAssistants;