|
1 | | -import React from "react"; |
| 1 | +"use client"; |
| 2 | +import React, { useState, useEffect } from "react"; |
2 | 3 | import { THREADS_LIST_MOCK_DATA } from "./test-data"; |
3 | 4 | import { ScrollArea } from "@/components/ui/scroll-area"; |
4 | 5 | import ThreadsListItem from "./threads-list-item"; |
5 | 6 | import { Input } from "@/components/ui/input"; |
6 | 7 | import { Search } from "lucide-react"; |
| 8 | +import { Skeleton } from "@/components/ui/skeleton"; |
7 | 9 |
|
8 | 10 | const ThreadsList = () => { |
| 11 | + const [isLoading, setIsLoading] = useState(true); |
| 12 | + const [activeId, setActiveId] = useState(""); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + const fetchData = async () => { |
| 16 | + setTimeout(() => { |
| 17 | + setIsLoading(false); |
| 18 | + }, 2000); |
| 19 | + }; |
| 20 | + fetchData(); |
| 21 | + }, []); |
| 22 | + |
| 23 | + const handleSetActiveId = (id: string) => { |
| 24 | + setActiveId(id); |
| 25 | + }; |
| 26 | + |
| 27 | + const isActive = (id: string) => id === activeId; |
| 28 | + |
9 | 29 | return ( |
10 | 30 | <div className="w-64 rounded-md bg-white p-4 shadow-md"> |
11 | 31 | <div className="mb-4 flex flex-col gap-2"> |
12 | 32 | <Input placeholder="Filter your threads..." startIcon={Search} /> |
13 | 33 | </div> |
14 | 34 | <ScrollArea className="relative h-4/5 overflow-clip"> |
15 | | - {/* This is just for the overflow gradient */} |
| 35 | + {/* This is just for the overflow gradient */} |
16 | 36 | <div className="pointer-events-none absolute right-0 top-0 h-4/5 w-8 bg-gradient-to-r from-transparent to-white"></div> |
17 | | - {/* This is just for the overflow gradient */} |
| 37 | + {/* This is just for the overflow gradient */} |
18 | 38 | <div className="flex flex-col gap-4"> |
19 | | - {THREADS_LIST_MOCK_DATA.map((thread, key) => { |
20 | | - return <ThreadsListItem key={thread.title + key} {...thread} />; |
21 | | - })} |
| 39 | + {isLoading |
| 40 | + ? Array.from({ length: THREADS_LIST_MOCK_DATA.length }).map( |
| 41 | + (_, idx) => <Skeleton key={idx} className="h-8" />, |
| 42 | + ) |
| 43 | + : THREADS_LIST_MOCK_DATA.map((thread, key) => ( |
| 44 | + <div |
| 45 | + key={thread.title + key} |
| 46 | + onClick={() => handleSetActiveId(thread.title.toLowerCase())} |
| 47 | + > |
| 48 | + <ThreadsListItem |
| 49 | + isActive={isActive(thread.title.toLowerCase())} |
| 50 | + {...thread} |
| 51 | + /> |
| 52 | + </div> |
| 53 | + ))} |
22 | 54 | </div> |
23 | 55 | </ScrollArea> |
24 | 56 | </div> |
|
0 commit comments