mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-03-10 06:00:19 +00:00
fix: debounce profile update notifications to prevent toast spam
a new toast was being triggered for every character input.
This commit is contained in:
parent
294adfdd1b
commit
70b723d514
@ -1,4 +1,4 @@
|
|||||||
import { useState } from 'react';
|
import { useState, useCallback, useEffect } from 'react';
|
||||||
import { useStore } from '@nanostores/react';
|
import { useStore } from '@nanostores/react';
|
||||||
import { classNames } from '~/utils/classNames';
|
import { classNames } from '~/utils/classNames';
|
||||||
import { profileStore, updateProfile } from '~/lib/stores/profile';
|
import { profileStore, updateProfile } from '~/lib/stores/profile';
|
||||||
@ -7,6 +7,32 @@ import { toast } from 'react-toastify';
|
|||||||
export default function ProfileTab() {
|
export default function ProfileTab() {
|
||||||
const profile = useStore(profileStore);
|
const profile = useStore(profileStore);
|
||||||
const [isUploading, setIsUploading] = useState(false);
|
const [isUploading, setIsUploading] = useState(false);
|
||||||
|
const [toastTimeout, setToastTimeout] = useState<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
|
const handleProfileUpdate = useCallback(
|
||||||
|
(field: 'username' | 'bio', value: string) => {
|
||||||
|
updateProfile({ [field]: value });
|
||||||
|
|
||||||
|
if (toastTimeout) {
|
||||||
|
clearTimeout(toastTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
toast.success(`${field.charAt(0).toUpperCase() + field.slice(1)} updated`);
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
setToastTimeout(timeout);
|
||||||
|
},
|
||||||
|
[toastTimeout],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (toastTimeout) {
|
||||||
|
clearTimeout(toastTimeout);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [toastTimeout]);
|
||||||
|
|
||||||
const handleAvatarUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleAvatarUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const file = e.target.files?.[0];
|
const file = e.target.files?.[0];
|
||||||
@ -41,17 +67,6 @@ export default function ProfileTab() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleProfileUpdate = (field: 'username' | 'bio', value: string) => {
|
|
||||||
updateProfile({ [field]: value });
|
|
||||||
|
|
||||||
// Only show toast for completed typing (after 1 second of no typing)
|
|
||||||
const debounceToast = setTimeout(() => {
|
|
||||||
toast.success(`${field.charAt(0).toUpperCase() + field.slice(1)} updated`);
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
return () => clearTimeout(debounceToast);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="max-w-2xl mx-auto">
|
<div className="max-w-2xl mx-auto">
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
|
Loading…
Reference in New Issue
Block a user