fix: debounce profile update notifications to prevent toast spam
Some checks are pending
Docker Publish / docker-build-publish (push) Waiting to run
Update Stable Branch / prepare-release (push) Waiting to run

a new toast was being triggered for every character input.
This commit is contained in:
KevIsDev 2025-02-17 01:49:33 +00:00
parent 294adfdd1b
commit 70b723d514

View File

@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, useCallback, useEffect } from 'react';
import { useStore } from '@nanostores/react';
import { classNames } from '~/utils/classNames';
import { profileStore, updateProfile } from '~/lib/stores/profile';
@ -7,6 +7,32 @@ import { toast } from 'react-toastify';
export default function ProfileTab() {
const profile = useStore(profileStore);
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 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 (
<div className="max-w-2xl mx-auto">
<div className="space-y-6">