Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expiration reset #125

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/chat-widget/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
import { createRoot } from "react-dom/client";
import { ThemeProvider } from "@emotion/react";
import { useChat, type ChatHook } from "@nlxai/chat-react";
import { type Response, type ConversationHandler } from "@nlxai/chat-core";
import {
type Response,
type ConversationHandler,
getCurrentExpirationTimestamp,
} from "@nlxai/chat-core";
import {
CloseIcon,
MinimizeIcon,
Expand Down Expand Up @@ -425,6 +429,32 @@ export const Widget = forwardRef<WidgetRef, Props>(function Widget(props, ref) {
props.storeIn,
]);

// Maintain a set of expioration timestamps for which resets have been performed, to ensure they are not handled multiple times
const resetTimestamps = useRef(new Set<number>());

useEffect(() => {
const expirationTimestamp = getCurrentExpirationTimestamp(chat.responses);
if (expirationTimestamp != null) {
const until = expirationTimestamp - new Date().getTime();
if (until <= 0) {
if (!resetTimestamps.current.has(expirationTimestamp)) {
resetTimestamps.current.add(expirationTimestamp);
chat.conversationHandler.reset({ clearResponses: false });
return;
}
}
const timeout = setTimeout(() => {
if (!resetTimestamps.current.has(expirationTimestamp)) {
resetTimestamps.current.add(expirationTimestamp);
chat.conversationHandler.reset({ clearResponses: false });
}
}, until);
return () => {
clearTimeout(timeout);
};
}
}, [chat.responses, chat.conversationHandler.reset]);

// Expanded state

const [expanded, setExpanded] = useState(false);
Expand Down