Skip to content

Commit c036849

Browse files
committed
refactor(renderer): add throttling and timeout control for share providers
1 parent 1abb49a commit c036849

File tree

1 file changed

+33
-1
lines changed
  • src/renderer/src/components/ChatInput

1 file changed

+33
-1
lines changed

src/renderer/src/components/ChatInput/index.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,33 @@ const ChatInput = forwardRef((_props, _ref) => {
7878
.find((m) => m?.from === 'human' && m?.value !== IMAGE_PLACEHOLDER)
7979
?.value || '';
8080

81+
const [isSharing, setIsSharing] = React.useState(false);
82+
const isSharePending = React.useRef(false);
83+
const shareTimeoutRef = React.useRef<NodeJS.Timeout>();
84+
const SHARE_TIMEOUT = 100000;
85+
8186
const handleShare = async () => {
87+
if (isSharePending.current) {
88+
return;
89+
}
90+
8291
try {
92+
setIsSharing(true);
93+
isSharePending.current = true;
94+
95+
shareTimeoutRef.current = setTimeout(() => {
96+
setIsSharing(false);
97+
isSharePending.current = false;
98+
toast({
99+
title: 'Share timeout',
100+
description: 'Please try again later',
101+
status: 'error',
102+
position: 'top',
103+
duration: 3000,
104+
isClosable: true,
105+
});
106+
}, SHARE_TIMEOUT);
107+
83108
const response = await fetch(reportHTMLUrl);
84109
const html = await response.text();
85110

@@ -143,6 +168,12 @@ const ChatInput = forwardRef((_props, _ref) => {
143168
duration: 3000,
144169
isClosable: true,
145170
});
171+
} finally {
172+
if (shareTimeoutRef.current) {
173+
clearTimeout(shareTimeoutRef.current);
174+
}
175+
setIsSharing(false);
176+
isSharePending.current = false;
146177
}
147178
};
148179

@@ -215,8 +246,9 @@ const ChatInput = forwardRef((_props, _ref) => {
215246
variant="tars-ghost"
216247
aria-label="Share"
217248
onClick={handleShare}
249+
isDisabled={isSharing}
218250
>
219-
<LuScreenShare />
251+
{isSharing ? <Spinner size="sm" /> : <LuScreenShare />}
220252
</Button>
221253
)}
222254
<div />

0 commit comments

Comments
 (0)