Add Watch Buddies feature

- Created WatchBuddy model
- M:N junctions to EntrySeen/EpisodeSeen
  and Media relation for profile pictures
- Full CRUD + search + profile picture upload
- WatchBuddySelect multi-select with search and inline create, replaced
  all seen_with text inputs
- WatchBuddiesPage (grid) and WatchBuddyProfilePage (edit form,
  icon-based contacts, poster-grid watch history, monthly timeline)
This commit is contained in:
X4Lo committed 2026-06-25 03:11:14 +01:00
1 parent 4313a794ce
commit e82b905886
28 files changed
+6813 -374

No files matched your search

@@ -0,0 +1,178 @@
import { useState, useEffect, useRef } from "react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { watchBuddyApi } from "@/api/watchBuddy";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import { Loader2, Plus, X, Users } from "lucide-react";
import { useDebounce } from "@/hooks/useDebounce";
import type { WatchBuddy } from "@/types";
interface WatchBuddySelectProps {
selectedIds: number[];
onChange: (ids: number[]) => void;
initialBuddies?: WatchBuddy[];
}
export function WatchBuddySelect({
selectedIds,
onChange,
initialBuddies = [],
}: WatchBuddySelectProps) {
const queryClient = useQueryClient();
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");
const [creating, setCreating] = useState(false);
const [localBuddies, setLocalBuddies] = useState<WatchBuddy[]>([]);
const debouncedSearch = useDebounce(search, 200);
const ref = useRef<HTMLDivElement>(null);
const { data: options = [], isLoading } = useQuery({
queryKey: ["watch-buddies", debouncedSearch],
queryFn: () => watchBuddyApi.search(debouncedSearch || undefined),
});
// Display: initialBuddies + locally created/selected ones
const displayed = [
...initialBuddies.filter((b) => selectedIds.includes(b.id)),
...localBuddies.filter((b) => selectedIds.includes(b.id)),
];
// Check if typed name exactly matches an existing buddy
const hasExactMatch =
search.trim().length > 0 &&
options.some((b) => b.name.toLowerCase() === search.trim().toLowerCase());
useEffect(() => {
const handler = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node))
setOpen(false);
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, []);
const toggle = (id: number) => {
const isSelected = selectedIds.includes(id);
if (!isSelected) {
const buddy = options.find((b) => b.id === id);
if (buddy && !localBuddies.some((b) => b.id === id)) {
setLocalBuddies((prev) => [...prev, buddy]);
}
}
onChange(
isSelected ? selectedIds.filter((i) => i !== id) : [...selectedIds, id],
);
setSearch("");
};
const handleCreate = async () => {
if (!search.trim()) return;
setCreating(true);
try {
const buddy = await watchBuddyApi.create({ name: search.trim() });
queryClient.invalidateQueries({ queryKey: ["watch-buddies"] });
setLocalBuddies((prev) => [...prev, buddy]);
onChange([...selectedIds, buddy.id]);
setSearch("");
} finally {
setCreating(false);
}
};
return (
<div ref={ref} className="relative">
<button
type="button"
onClick={() => setOpen(!open)}
className="flex min-h-9 w-full flex-wrap items-center gap-1 rounded-md border border-input bg-background px-3 py-2 text-sm"
>
{displayed.length > 0 ? (
displayed.map((b) => (
<Badge key={b.id} variant="secondary" className="gap-1 text-xs">
{b.name}
<button
type="button"
onClick={(e) => {
e.stopPropagation();
onChange(selectedIds.filter((i) => i !== b.id));
}}
>
<X className="h-3 w-3" />
</button>
</Badge>
))
) : (
<span className="flex items-center gap-1.5 text-muted-foreground">
<Users className="h-3.5 w-3.5" />
Select buddies
</span>
)}
</button>
{open && (
<div className="absolute z-50 mt-1 w-full rounded-md border border-border bg-popover p-2 shadow-lg">
<Input
placeholder="Search or type name to create..."
value={search}
onChange={(e) => setSearch(e.target.value)}
className="h-8 text-xs mb-2"
autoFocus
/>
<div className="max-h-40 space-y-0.5 overflow-y-auto">
{isLoading && (
<div className="flex justify-center py-3">
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
</div>
)}
{!isLoading && options.length === 0 && search.trim() && (
<button
type="button"
onClick={handleCreate}
disabled={creating}
className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-xs hover:bg-accent"
>
<Plus className="h-3.5 w-3.5" />
{creating ? "Creating..." : `Create "${search.trim()}"`}
</button>
)}
{!isLoading &&
options.map((b) => (
<label
key={b.id}
className={`flex cursor-pointer items-center gap-2 rounded px-2 py-1.5 text-xs hover:bg-accent ${
selectedIds.includes(b.id) ? "bg-accent" : ""
}`}
>
<input
type="checkbox"
checked={selectedIds.includes(b.id)}
onChange={() => toggle(b.id)}
className="h-3.5 w-3.5 rounded"
/>
{b.name}
</label>
))}
{!isLoading &&
search.trim() &&
!hasExactMatch &&
options.length > 0 && (
<button
type="button"
onClick={handleCreate}
disabled={creating}
className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-xs hover:bg-accent"
>
<Plus className="h-3.5 w-3.5" />
{creating ? "Creating..." : `Create "${search.trim()}"`}
</button>
)}
</div>
</div>
)}
</div>
);
}