|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + Phone, |
| 5 | + Video, |
| 6 | + PhoneIncoming, |
| 7 | + PhoneMissed, |
| 8 | + Clock, |
| 9 | + Users, |
| 10 | +} from "lucide-react"; |
| 11 | +import { useQuery } from "@tanstack/react-query"; |
| 12 | +import { formatDate } from "date-fns"; |
| 13 | +import { ScrollArea } from "@repo/ui/components/scroll-area"; |
| 14 | +import api, { ApiOutputs } from "@repo/web/api"; |
| 15 | +import { |
| 16 | + Avatar, |
| 17 | + AvatarFallback, |
| 18 | + AvatarImage, |
| 19 | +} from "@repo/ui/components/avatar"; |
| 20 | +import { cn } from "@repo/ui/lib/utils"; |
| 21 | +import { Button } from "@repo/ui/components/button"; |
| 22 | +import { useSearch } from "../_providers/search-provider"; |
| 23 | + |
| 24 | +type Call = ApiOutputs["/call"]["/"]["get"]["calls"][number]; |
| 25 | + |
| 26 | +const CallList = () => { |
| 27 | + const { searchQuery } = useSearch(); |
| 28 | + const { isPending, isError, data } = useQuery({ |
| 29 | + queryKey: ["calls"], |
| 30 | + queryFn: async () => { |
| 31 | + const { data } = await api.call[""].get({ |
| 32 | + options: { |
| 33 | + throwOnErrorStatus: true, |
| 34 | + }, |
| 35 | + }); |
| 36 | + return data; |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + if (isPending) { |
| 41 | + return <p>Loading...</p>; |
| 42 | + } |
| 43 | + |
| 44 | + if (isError) { |
| 45 | + return <p>Error</p>; |
| 46 | + } |
| 47 | + |
| 48 | + const filteredCalls = searchQuery.trim() |
| 49 | + ? data.calls.filter((call) => |
| 50 | + call.participants.some((p) => |
| 51 | + p.user.name.toLowerCase().includes(searchQuery.toLowerCase()) |
| 52 | + ) |
| 53 | + ) |
| 54 | + : data.calls; |
| 55 | + |
| 56 | + return ( |
| 57 | + <ScrollArea className="h-[calc(100vh-190px)]"> |
| 58 | + {filteredCalls.map((call, index) => { |
| 59 | + return ( |
| 60 | + <div key={call.call.id} className="p-4"> |
| 61 | + <DateDivider call={call} prevCall={data.calls[index - 1]} /> |
| 62 | + <Call call={call} /> |
| 63 | + </div> |
| 64 | + ); |
| 65 | + })} |
| 66 | + </ScrollArea> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +const Call = ({ call }: { call: Call }) => { |
| 71 | + return ( |
| 72 | + <div className="mt-2"> |
| 73 | + <div className="flex items-center gap-2 justify-between"> |
| 74 | + <CallParticipants call={call} /> |
| 75 | + </div> |
| 76 | + <div className="flex items-center justify-between"> |
| 77 | + <CallStatus call={call} /> |
| 78 | + <CallButtons call={call} /> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + ); |
| 82 | +}; |
| 83 | + |
| 84 | +const DateDivider = ({ |
| 85 | + call, |
| 86 | + prevCall, |
| 87 | +}: { |
| 88 | + call: Call; |
| 89 | + prevCall: Call | undefined; |
| 90 | +}) => { |
| 91 | + const showDate = |
| 92 | + prevCall?.self.joinedAt?.toDateString() !== |
| 93 | + call.self.joinedAt!.toDateString(); |
| 94 | + |
| 95 | + if (!showDate) return null; |
| 96 | + |
| 97 | + return ( |
| 98 | + <span className="font-medium text-muted-foreground text-sm"> |
| 99 | + {formatDate(call.self.joinedAt!, "PPP")} |
| 100 | + </span> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +const CallParticipants = ({ call }: { call: Call }) => { |
| 105 | + const displayParticipants = call.participants.slice(0, 3); |
| 106 | + const remainingCount = call.participants.length - 3; |
| 107 | + |
| 108 | + return call.participants && call.participants.length > 1 ? ( |
| 109 | + <div className="flex items-center gap-2"> |
| 110 | + <div className="flex -space-x-2"> |
| 111 | + {displayParticipants.map((participant) => { |
| 112 | + return ( |
| 113 | + <Avatar className="size-8" key={participant.user.id}> |
| 114 | + <AvatarImage src={participant.user.picture ?? undefined} /> |
| 115 | + <AvatarFallback>{participant.user.name}</AvatarFallback> |
| 116 | + </Avatar> |
| 117 | + ); |
| 118 | + })} |
| 119 | + {remainingCount > 0 && ( |
| 120 | + <div className="h-8 w-8 rounded-full bg-muted border-2 border-card flex items-center justify-center"> |
| 121 | + <span className="text-xs text-muted-foreground font-medium"> |
| 122 | + +{remainingCount} |
| 123 | + </span> |
| 124 | + </div> |
| 125 | + )} |
| 126 | + </div> |
| 127 | + <div className="flex items-center gap-1"> |
| 128 | + <Users className="h-3 w-3 text-muted-foreground" /> |
| 129 | + <span className="font-medium"> |
| 130 | + {call.participants.length === 2 |
| 131 | + ? call.participants.map((p) => p.user.name).join(", ") |
| 132 | + : `${call.participants[0].user.name} and ${call.participants.length - 1} others`} |
| 133 | + </span> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ) : ( |
| 137 | + <div className="flex items-center gap-2"> |
| 138 | + <Avatar className="size-8"> |
| 139 | + <AvatarImage src={call.participants[0].user.picture ?? undefined} /> |
| 140 | + <AvatarFallback>{call.participants[0].user.name}</AvatarFallback> |
| 141 | + </Avatar> |
| 142 | + <span className="font-medium">{call.participants[0].user.name}</span> |
| 143 | + </div> |
| 144 | + ); |
| 145 | +}; |
| 146 | + |
| 147 | +const CallStatus = ({ call }: { call: Call }) => { |
| 148 | + return ( |
| 149 | + <div className="flex items-center gap-2"> |
| 150 | + <CallIcon call={call} /> |
| 151 | + <span |
| 152 | + className={cn( |
| 153 | + "text-sm capitalize", |
| 154 | + call.call.status === "missed" |
| 155 | + ? "text-red-600" |
| 156 | + : "text-muted-foreground" |
| 157 | + )} |
| 158 | + > |
| 159 | + {call.call.status} |
| 160 | + </span> |
| 161 | + {call.call.duration && ( |
| 162 | + <> |
| 163 | + <span className="text-muted-foreground">•</span> |
| 164 | + <div className="flex items-center gap-1"> |
| 165 | + <Clock className="h-3 w-3 text-muted-foreground" /> |
| 166 | + <span className="text-sm text-muted-foreground"> |
| 167 | + {call.call.duration} |
| 168 | + </span> |
| 169 | + </div> |
| 170 | + </> |
| 171 | + )} |
| 172 | + </div> |
| 173 | + ); |
| 174 | +}; |
| 175 | + |
| 176 | +const CallIcon = ({ call }: { call: Call }) => { |
| 177 | + if (call.call.status === "ringing") |
| 178 | + return <PhoneIncoming className="h-4 w-4 text-green-500" />; |
| 179 | + |
| 180 | + if (call.call.status === "missed" || call.call.status === "declined") |
| 181 | + return <PhoneMissed className="h-4 w-4 text-red-500" />; |
| 182 | + |
| 183 | + return <Phone className="h-4 w-4 text-gray-500" />; |
| 184 | +}; |
| 185 | + |
| 186 | +const CallButtons = ({ call }: { call: Call }) => { |
| 187 | + return ( |
| 188 | + <div className="flex gap-1"> |
| 189 | + <Button variant="ghost" size="icon" className="h-7 w-7"> |
| 190 | + <Phone className="h-3.5 w-3.5" /> |
| 191 | + </Button> |
| 192 | + <Button variant="ghost" size="icon" className="h-7 w-7"> |
| 193 | + <Video className="h-3.5 w-3.5" /> |
| 194 | + </Button> |
| 195 | + </div> |
| 196 | + ); |
| 197 | +}; |
| 198 | + |
| 199 | +export default CallList; |
0 commit comments