Skip to content

Commit f54dbcf

Browse files
authored
Float notification auto close (#1669)
* Float notification auto close * Use on Deploy Contract page
1 parent 74fd017 commit f54dbcf

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

src/app/(sidebar)/smart-contracts/deploy-contract/page.tsx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,28 @@ export default function DeployContract() {
277277
setSourceAccountError("");
278278
};
279279

280+
const autoCloseFloatNotification = useCallback(
281+
({
282+
id,
283+
title,
284+
description,
285+
}: {
286+
id: string;
287+
title: string;
288+
description: string;
289+
}) => {
290+
addFloatNotification({
291+
id,
292+
title,
293+
description,
294+
type: "success",
295+
// Close after 3 seconds
296+
closeAt: Date.now() + 3000,
297+
});
298+
},
299+
[addFloatNotification],
300+
);
301+
280302
useEffect(() => {
281303
if (selectedFile && (!isRpcWasmBinarySuccess || !isUploadTxSuccess)) {
282304
const fn = async () => {
@@ -322,36 +344,33 @@ export default function DeployContract() {
322344
// Show float notification on upload tx created success
323345
useEffect(() => {
324346
if (isUploadTxSuccess) {
325-
addFloatNotification({
347+
autoCloseFloatNotification({
326348
id: "upload-build-tx-success",
327349
title: "Upload transaction created",
328350
description:
329351
"Upload transaction was created successfully. You can sign it next.",
330-
type: "success",
331352
});
332353
}
333-
}, [addFloatNotification, isUploadTxSuccess]);
354+
}, [autoCloseFloatNotification, isUploadTxSuccess]);
334355

335356
// Show float notification when upload tx is signed
336357
useEffect(() => {
337358
if (signUploadSuccess) {
338-
addFloatNotification({
359+
autoCloseFloatNotification({
339360
id: "upload-sign-tx-success",
340361
title: "Upload transaction signed",
341362
description: `${signUploadSuccess}. You can submit it to the network next.`,
342-
type: "success",
343363
});
344364
}
345-
}, [addFloatNotification, signUploadSuccess]);
365+
}, [autoCloseFloatNotification, signUploadSuccess]);
346366

347367
// Show float notification when upload tx is submitted
348368
useEffect(() => {
349369
if (isSubmitUploadTxSuccess) {
350-
addFloatNotification({
370+
autoCloseFloatNotification({
351371
id: "upload-submit-tx-success",
352372
title: "Contract uploaded to the network",
353373
description: `${selectedFile?.name} file has been uploaded successfully.`,
354-
type: "success",
355374
});
356375

357376
// Collapse the upload block on success
@@ -363,7 +382,7 @@ export default function DeployContract() {
363382
delay: 300,
364383
});
365384
}
366-
}, [addFloatNotification, isSubmitUploadTxSuccess, selectedFile?.name]);
385+
}, [autoCloseFloatNotification, isSubmitUploadTxSuccess, selectedFile?.name]);
367386

368387
// ===========================================================================
369388
// Deploy effects
@@ -372,39 +391,36 @@ export default function DeployContract() {
372391
// Show float notification on deploy tx created success
373392
useEffect(() => {
374393
if (isDeployTxSuccess) {
375-
addFloatNotification({
394+
autoCloseFloatNotification({
376395
id: "deploy-build-tx-success",
377396
title: "Deploy transaction created",
378397
description:
379398
"Deploy transaction was created successfully. You can sign it next.",
380-
type: "success",
381399
});
382400
}
383-
}, [addFloatNotification, isDeployTxSuccess]);
401+
}, [autoCloseFloatNotification, isDeployTxSuccess]);
384402

385403
// Show float notification when deploy tx is signed
386404
useEffect(() => {
387405
if (signDeploySuccess) {
388-
addFloatNotification({
406+
autoCloseFloatNotification({
389407
id: "deploy-sign-tx-success",
390408
title: "Deploy transaction signed",
391409
description: `${signDeploySuccess}. You can submit it to the network next.`,
392-
type: "success",
393410
});
394411
}
395-
}, [addFloatNotification, signDeploySuccess]);
412+
}, [autoCloseFloatNotification, signDeploySuccess]);
396413

397414
// Show float notification when deploy tx is submitted
398415
useEffect(() => {
399416
if (isSubmitDeployTxSuccess) {
400-
addFloatNotification({
417+
autoCloseFloatNotification({
401418
id: "deploy-submit-tx-success",
402419
title: "Contract deployed!",
403420
description: `${selectedFile?.name} file has been successfully deployed.`,
404-
type: "success",
405421
});
406422
}
407-
}, [addFloatNotification, isSubmitDeployTxSuccess, selectedFile?.name]);
423+
}, [autoCloseFloatNotification, isSubmitDeployTxSuccess, selectedFile?.name]);
408424

409425
useEffect(() => {
410426
// Collapse deploy block on success

src/components/FloatNotification/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type FloatNotificationItem = {
1313
label: string;
1414
onAction: () => void;
1515
}[];
16+
closeAt?: number;
1617
};
1718

1819
type FloatNotificationProps = {

src/components/layout/LayoutMain.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,34 @@ export const LayoutMain = ({ children }: { children: ReactNode }) => {
2121
initTracking();
2222
}, []);
2323

24+
// Handle auto closing float notifications
25+
useEffect(() => {
26+
if (floatNotifications.length === 0) {
27+
return;
28+
}
29+
30+
// No need to check if there are no auto closing notifications
31+
const hasAutoCloseNotifications = floatNotifications.some(
32+
(notification) => notification.closeAt,
33+
);
34+
35+
if (!hasAutoCloseNotifications) {
36+
return;
37+
}
38+
39+
// Check every 100ms for expired notifications
40+
const interval = setInterval(() => {
41+
const now = Date.now();
42+
floatNotifications.forEach((notification) => {
43+
if (notification.closeAt && now >= notification.closeAt) {
44+
removeFloatNotification(notification.id);
45+
}
46+
});
47+
}, 100);
48+
49+
return () => clearInterval(interval);
50+
}, [floatNotifications, removeFloatNotification]);
51+
2452
return (
2553
<div className="LabLayout">
2654
<div className="LabLayout__header">

0 commit comments

Comments
 (0)