Skip to content

Commit c15d028

Browse files
authored
feat: Reset buton functionality (#3764)
* Reset buton functionality * Remove loading from ref * Fixes * Lint fix * review changes
1 parent 5a520c8 commit c15d028

File tree

3 files changed

+87
-29
lines changed

3 files changed

+87
-29
lines changed

src/components/KymaCompanion/components/Chat/Chat.tsx

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,50 @@ import getChatResponse from 'components/KymaCompanion/api/getChatResponse';
1313
import { usePromptSuggestions } from 'components/KymaCompanion/hooks/usePromptSuggestions';
1414
import './Chat.scss';
1515

16-
interface MessageType {
16+
export interface MessageType {
1717
author: 'user' | 'ai';
1818
messageChunks: MessageChunk[];
1919
isLoading: boolean;
2020
suggestions?: string[];
2121
suggestionsLoading?: boolean;
2222
}
2323

24-
export default function Chat() {
24+
type ChatProps = {
25+
chatHistory: MessageType[];
26+
setChatHistory: React.Dispatch<React.SetStateAction<MessageType[]>>;
27+
loading: boolean;
28+
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
29+
isReset: boolean;
30+
setIsReset: React.Dispatch<React.SetStateAction<boolean>>;
31+
error: string | null;
32+
setError: React.Dispatch<React.SetStateAction<string | null>>;
33+
};
34+
35+
export const Chat = ({
36+
chatHistory,
37+
setChatHistory,
38+
error,
39+
setError,
40+
loading,
41+
setLoading,
42+
isReset,
43+
setIsReset,
44+
}: ChatProps) => {
2545
const { t } = useTranslation();
2646
const containerRef = useRef<HTMLDivElement | null>(null);
2747
const [inputValue, setInputValue] = useState<string>('');
28-
const [chatHistory, setChatHistory] = useState<MessageType[]>([
29-
{
30-
author: 'ai',
31-
messageChunks: [
32-
{
33-
data: {
34-
answer: {
35-
content: t('kyma-companion.introduction'),
36-
next: '__end__',
37-
},
38-
},
39-
},
40-
],
41-
isLoading: false,
42-
suggestionsLoading: true,
43-
},
44-
]);
45-
const [error, setError] = useState<string | null>(null);
48+
4649
const sessionID = useRecoilValue<string>(sessionIDState);
4750
const cluster = useRecoilValue<any>(clusterState);
4851
const authData = useRecoilValue<any>(authDataState);
49-
const [loading, setLoading] = useState<boolean>(false);
5052

5153
const {
5254
initialSuggestions,
5355
initialSuggestionsLoading,
5456
currentResource,
55-
} = usePromptSuggestions({ skip: chatHistory.length > 1 });
57+
} = usePromptSuggestions(isReset, setIsReset, {
58+
skip: chatHistory.length > 1,
59+
});
5660

5761
const addMessage = ({ author, messageChunks, isLoading }: MessageType) => {
5862
setChatHistory(prevItems =>
@@ -285,4 +289,4 @@ export default function Chat() {
285289
</div>
286290
</FlexBox>
287291
);
288-
}
292+
};

src/components/KymaCompanion/components/KymaCompanion.tsx

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1+
import { useState } from 'react';
12
import { useTranslation } from 'react-i18next';
23
import { Button, Card, Title } from '@ui5/webcomponents-react';
34
import { useRecoilState } from 'recoil';
45
import {
56
ShowKymaCompanion,
67
showKymaCompanionState,
78
} from 'state/companion/showKymaCompanionAtom';
8-
import Chat from './Chat/Chat';
9+
import { Chat, MessageType } from './Chat/Chat';
910
import './KymaCompanion.scss';
1011

1112
export default function KymaCompanion() {
1213
const { t } = useTranslation();
14+
15+
const initialChatHistory: MessageType[] = [
16+
{
17+
author: 'ai',
18+
messageChunks: [
19+
{
20+
data: {
21+
answer: {
22+
content: t('kyma-companion.introduction'),
23+
next: '__end__',
24+
},
25+
},
26+
},
27+
],
28+
isLoading: false,
29+
suggestionsLoading: true,
30+
},
31+
];
32+
1333
const [showCompanion, setShowCompanion] = useRecoilState<ShowKymaCompanion>(
1434
showKymaCompanionState,
1535
);
36+
const [loading, setLoading] = useState<boolean>(false);
37+
const [isReset, setIsReset] = useState<boolean>(false);
38+
const [chatHistory, setChatHistory] = useState<MessageType[]>(
39+
initialChatHistory,
40+
);
41+
const [error, setError] = useState<string | null>(null);
42+
43+
function handleRefresh() {
44+
setChatHistory(() => {
45+
return initialChatHistory;
46+
});
47+
setError(null);
48+
setIsReset(true);
49+
}
1650

1751
return (
1852
<div id="companion_wrapper" className="sap-margin-tiny">
@@ -27,9 +61,10 @@ export default function KymaCompanion() {
2761
<Button
2862
design="Transparent"
2963
icon="restart"
64+
disabled={loading}
3065
tooltip={t('common.buttons.reset')}
3166
className="action"
32-
onClick={() => {}}
67+
onClick={() => handleRefresh()}
3368
/>
3469
<Button
3570
design="Transparent"
@@ -57,7 +92,16 @@ export default function KymaCompanion() {
5792
</div>
5893
}
5994
>
60-
<Chat />
95+
<Chat
96+
loading={loading}
97+
setLoading={setLoading}
98+
chatHistory={chatHistory}
99+
setChatHistory={setChatHistory}
100+
isReset={isReset}
101+
setIsReset={setIsReset}
102+
error={error}
103+
setError={setError}
104+
/>
61105
</Card>
62106
</div>
63107
);

src/components/KymaCompanion/hooks/usePromptSuggestions.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ const getResourceFromColumnnLayout = (
3030
};
3131
};
3232

33-
export function usePromptSuggestions(options?: { skip?: boolean }) {
33+
export function usePromptSuggestions(
34+
isReset: boolean,
35+
setIsReset: React.Dispatch<React.SetStateAction<boolean>>,
36+
options?: {
37+
skip?: boolean;
38+
},
39+
) {
3440
const post = usePost();
3541
const [initialSuggestions, setInitialSuggestions] = useState<string[]>([]);
3642
const setSessionID = useSetRecoilState(sessionIDState);
@@ -93,11 +99,15 @@ export function usePromptSuggestions(options?: { skip?: boolean }) {
9399
}
94100
}
95101

96-
if (resourceType && fetchedResourceRef.current !== resourceKey) {
102+
if (
103+
resourceType &&
104+
(fetchedResourceRef.current !== resourceKey || isReset)
105+
) {
97106
fetchedResourceRef.current = resourceKey;
98107
fetchSuggestions();
108+
setIsReset(false);
99109
}
100-
}, [currentResource, options?.skip, post, setSessionID]);
110+
}, [currentResource, options?.skip, post, setSessionID, isReset, setIsReset]);
101111

102112
return {
103113
initialSuggestions,

0 commit comments

Comments
 (0)