diff --git a/components/Home.jsx b/components/Home.jsx
index 9162a8c..4a128ab 100644
--- a/components/Home.jsx
+++ b/components/Home.jsx
@@ -1,17 +1,89 @@
'use client';
-import { useState,useEffect } from "react";
+import { useState,useEffect,useRef } from "react";
import { useRouter } from "next/navigation";
import toast, { Toaster } from 'react-hot-toast';
import Typewriter from "typewriter-effect";
import Contributors from "./Contributors";
+import { MdHistory } from "react-icons/md";
function Home() {
- const [username, setUsername] = useState("");
+ const [username, setUserName] = useState("");
const [showContributors, setShowContributors] = useState(false);
const router = useRouter();
+ const [topSearches,setTopSearches] = useState([]);
+ const [showRecents,setShowRecents] = useState(false);
+ const inputRef = useRef(null);
+ const topRef = useRef(null);
+ useEffect(() => {
+ const names = localStorage.getItem("usernames");
+ if (names) {
+ const userObj = JSON.parse(names);
+ const sortedUsernames = Object.entries(userObj)
+ .sort(([, a], [, b]) => b.count - a.count)
+ .map(([key, value]) => ({ name: key, count: value.count }));
+
+ setTopSearches(sortedUsernames.slice(0, 5));
+ }
+}, []);
+
+useEffect(() => {
+ // Add event listener to detect clicks outside
+ document.addEventListener('mousedown', handleClickOutside);
+ return () => {
+ // Cleanup event listener on component unmount
+ document.removeEventListener('mousedown', handleClickOutside);
+ };
+}, []);
+
+ // Function to check if a click is outside the input or top element
+const handleClickOutside = (event) => {
+ if (
+ topRef.current &&
+ !topRef.current.contains(event.target) &&
+ !inputRef.current.contains(event.target)
+ ) {
+ setShowRecents(false);
+ }
+};
const handleSubmit = async (e) => {
e.preventDefault();
+
+ const val = username.trim();
+ if (!val) {
+ toast.error('Please enter a valid username.');
+ return;
+ }
+ const names = localStorage.getItem("usernames");
+
+ let userObj;
+ if (names) {
+
+ userObj = JSON.parse(names);
+
+ if (userObj[val]) {
+
+ userObj[val].count += 1;
+ } else {
+
+ userObj[val] = { count: 1 };
+ }
+ } else {
+
+ userObj = { [val]: { count: 1 } };
+ }
+
+ localStorage.setItem("usernames", JSON.stringify(userObj));
+
+
+ const sortedUsernames = Object.entries(userObj)
+ .sort(([, a], [, b]) => b.count - a.count) // Sort by count in descending order
+ .map(([key, value]) => ({ name: key, count: value.count })); // Convert back to array format
+
+
+ setTopSearches(sortedUsernames.slice(0, 5));
+
+
if (username.trim()) {
try {
const response = await fetch('/api/user', {
@@ -22,11 +94,8 @@ function Home() {
body: JSON.stringify({ username: username.trim() }),
});
- console.log(response)
-
if (response.ok) {
const data = await response.json();
- console.log(data);
if (data.exists) {
router.push(`/${username.trim()}`);
} else {
@@ -43,6 +112,15 @@ function Home() {
}
};
+ const handleTopSearchClick = (name) => {
+ setUserName(name);
+};
+
+ const handleUser = async (e) => {
+ const val = e.target.value;
+ setUserName(val);
+};
+
return (
<>