feat: add TopFriendsCombobox component for selecting top friends, update edit profile form to use it, and implement getFriends API

This commit is contained in:
2025-09-06 22:37:06 +02:00
parent 8ddbf45a09
commit c520690f1e
5 changed files with 138 additions and 24 deletions

View File

@@ -70,7 +70,7 @@ export default async function ThoughtPage({ params }: ThoughtPageProps) {
return ( return (
<div className="container mx-auto max-w-2xl p-4 sm:p-6"> <div className="container mx-auto max-w-2xl p-4 sm:p-6">
<header className="my-6"> <header className="my-6">
<h1 className="text-3xl font-bold">Conversation</h1> <h1 className="text-3xl font-bold">Thoughts</h1>
</header> </header>
<main> <main>
<ThoughtThread <ThoughtThread

View File

@@ -20,6 +20,7 @@ import {
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { TopFriendsCombobox } from "@/components/top-friends-combobox";
interface EditProfileFormProps { interface EditProfileFormProps {
currentUser: Me; currentUser: Me;
@@ -47,9 +48,8 @@ export function EditProfileForm({ currentUser }: EditProfileFormProps) {
try { try {
await updateProfile(values, token); await updateProfile(values, token);
toast.success("Profile updated successfully!"); toast.success("Profile updated successfully!");
// Redirect to the profile page to see the changes
router.push(`/users/${currentUser.username}`); router.push(`/users/${currentUser.username}`);
router.refresh(); // Ensure fresh data is loaded router.refresh();
} catch (err) { } catch (err) {
toast.error(`Failed to update profile. ${err}`); toast.error(`Failed to update profile. ${err}`);
} }
@@ -139,21 +139,16 @@ export function EditProfileForm({ currentUser }: EditProfileFormProps) {
name="topFriends" name="topFriends"
control={form.control} control={form.control}
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem className="flex flex-col">
<FormLabel>Top Friends</FormLabel> <FormLabel>Top Friends</FormLabel>
<FormControl> <FormControl>
<Input <TopFriendsCombobox
placeholder="username1, username2, ..." value={field.value || []}
{...field} onChange={field.onChange}
onChange={(e) =>
field.onChange(
e.target.value.split(",").map((s) => s.trim())
)
}
/> />
</FormControl> </FormControl>
<FormDescription> <FormDescription>
A comma-separated list of usernames. Select up to 8 of your friends to display on your profile.
</FormDescription> </FormDescription>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>

View File

@@ -112,14 +112,14 @@ export function ThoughtCard({
<span className="text-sm text-muted-foreground">{timeAgo}</span> <span className="text-sm text-muted-foreground">{timeAgo}</span>
</div> </div>
</Link> </Link>
{isAuthor && ( <DropdownMenu>
<DropdownMenu> <DropdownMenuTrigger asChild>
<DropdownMenuTrigger asChild> <button className="p-2 rounded-full hover:bg-accent">
<button className="p-2 rounded-full hover:bg-accent"> <MoreHorizontal className="h-4 w-4" />
<MoreHorizontal className="h-4 w-4" /> </button>
</button> </DropdownMenuTrigger>
</DropdownMenuTrigger> <DropdownMenuContent>
<DropdownMenuContent> {isAuthor && (
<DropdownMenuItem <DropdownMenuItem
className="text-destructive" className="text-destructive"
onSelect={() => setIsAlertOpen(true)} onSelect={() => setIsAlertOpen(true)}
@@ -127,9 +127,15 @@ export function ThoughtCard({
<Trash2 className="mr-2 h-4 w-4" /> <Trash2 className="mr-2 h-4 w-4" />
Delete Delete
</DropdownMenuItem> </DropdownMenuItem>
</DropdownMenuContent> )}
</DropdownMenu> <DropdownMenuItem>
)} <Link href={`/thoughts/${thought.id}`} className="flex gap-2">
<MessageSquare className="mr-2 h-4 w-4" />
View
</Link>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<p className="whitespace-pre-wrap break-words">{thought.content}</p> <p className="whitespace-pre-wrap break-words">{thought.content}</p>

View File

@@ -0,0 +1,105 @@
"use client";
import * as React from "react";
import { Check, ChevronsUpDown } from "lucide-react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { getFriends, User } from "@/lib/api";
import { useAuth } from "@/hooks/use-auth";
import { Skeleton } from "./ui/skeleton";
interface TopFriendsComboboxProps {
value: string[];
onChange: (value: string[]) => void;
}
export function TopFriendsCombobox({
value,
onChange,
}: TopFriendsComboboxProps) {
const [open, setOpen] = React.useState(false);
const [friends, setFriends] = React.useState<User[]>([]);
const [isLoading, setIsLoading] = React.useState(true);
const { token } = useAuth();
React.useEffect(() => {
if (token) {
getFriends(token)
.then((data) => setFriends(data.users))
.catch(() => console.error("Failed to fetch friends"))
.finally(() => setIsLoading(false));
} else {
setIsLoading(false);
}
}, [token]);
if (isLoading) {
return <Skeleton className="h-10 w-full" />;
}
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-full justify-between"
>
{value.length > 0
? `${value.length} friend(s) selected`
: "Select up to 8 friends..."}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-full p-0">
<Command>
<CommandInput placeholder="Search friends..." />
<CommandList>
<CommandEmpty>No friends found.</CommandEmpty>
<CommandGroup>
{friends.map((friend) => (
<CommandItem
key={friend.id}
value={friend.username}
onSelect={(currentValue) => {
const newValue = value.includes(currentValue)
? value.filter((v) => v !== currentValue)
: [...value, currentValue];
if (newValue.length <= 8) {
onChange(newValue);
}
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
value.includes(friend.username)
? "opacity-100"
: "opacity-0"
)}
/>
{friend.username}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}

View File

@@ -225,3 +225,11 @@ export const getFollowersList = (username: string, token: string | null) =>
z.object({ users: z.array(UserSchema) }), z.object({ users: z.array(UserSchema) }),
token token
); );
export const getFriends = (token: string) =>
apiFetch(
"/friends",
{},
z.object({ users: z.array(UserSchema) }),
token
);