diff --git a/src/components/social/FollowingSystem.tsx b/src/components/social/FollowingSystem.tsx index 37ca0b29..7bb4a746 100644 --- a/src/components/social/FollowingSystem.tsx +++ b/src/components/social/FollowingSystem.tsx @@ -1,6 +1,6 @@ 'use client'; import Image from 'next/image'; -import { useState, useEffect } from 'react'; +import { useState, useEffect, useMemo } from 'react'; import { Search, UserCircle } from 'lucide-react'; import { useFollowUser } from '@/hooks/useSocialFeatures'; import { apiClient } from '@/lib/api'; @@ -40,11 +40,10 @@ function UserRow({ user }: { user: SocialUser }) { @@ -56,8 +55,18 @@ export default function FollowingSystem({ userId }: FollowingSystemProps) { const [tab, setTab] = useState('followers'); const [users, setUsers] = useState([]); const [query, setQuery] = useState(''); + const [debouncedQuery, setDebouncedQuery] = useState(''); const [loading, setLoading] = useState(false); + // Debounce the search input query by 300ms + useEffect(() => { + const timer = setTimeout(() => { + setDebouncedQuery(query); + }, 300); + + return () => clearTimeout(timer); + }, [query]); + useEffect(() => { setLoading(true); apiClient @@ -67,7 +76,11 @@ export default function FollowingSystem({ userId }: FollowingSystemProps) { .finally(() => setLoading(false)); }, [tab, userId]); - const filtered = users.filter((u) => u.name.toLowerCase().includes(query.toLowerCase())); + // Memoize the filtered user results based on the debounced query + const filtered = useMemo(() => { + const lowercaseQuery = debouncedQuery.toLowerCase(); + return users.filter((u) => u.name.toLowerCase().includes(lowercaseQuery)); + }, [users, debouncedQuery]); return (
@@ -77,11 +90,10 @@ export default function FollowingSystem({ userId }: FollowingSystemProps) { @@ -118,4 +130,4 @@ export default function FollowingSystem({ userId }: FollowingSystemProps) {
); -} +} \ No newline at end of file