2025-10-14 12:20:36 -07:00
|
|
|
import { useEffect, useState } from "react";
|
2025-10-11 01:03:31 -07:00
|
|
|
import { Link, useSearchParams } from "react-router";
|
|
|
|
|
import { toast } from "sonner";
|
2025-10-15 22:02:21 -07:00
|
|
|
import { format } from "date-fns";
|
2025-10-11 00:07:39 -07:00
|
|
|
import type { Route } from "./+types/$leagueId";
|
2025-10-11 01:03:31 -07:00
|
|
|
import { Button } from "~/components/ui/button";
|
2025-10-11 00:07:39 -07:00
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardDescription,
|
|
|
|
|
CardHeader,
|
|
|
|
|
CardTitle,
|
|
|
|
|
} from "~/components/ui/card";
|
2025-11-14 20:39:51 -08:00
|
|
|
import { SportSeasonCard } from "~/components/sports/SportSeasonCard";
|
2025-10-11 00:07:39 -07:00
|
|
|
|
2025-11-14 21:18:34 -08:00
|
|
|
export { loader } from "./$leagueId.server";
|
2025-10-11 00:07:39 -07:00
|
|
|
|
|
|
|
|
export default function LeagueHome({ loaderData }: Route.ComponentProps) {
|
2025-10-14 21:24:58 -07:00
|
|
|
const {
|
|
|
|
|
league,
|
|
|
|
|
season,
|
|
|
|
|
teams,
|
|
|
|
|
commissioners,
|
|
|
|
|
currentUserId,
|
2025-10-14 21:20:58 -07:00
|
|
|
isUserCommissioner,
|
|
|
|
|
ownerMap,
|
|
|
|
|
commissionerMap,
|
|
|
|
|
availableTeamCount,
|
2025-10-15 08:58:35 -07:00
|
|
|
draftSlots,
|
2025-10-15 21:50:02 -07:00
|
|
|
sportsCount,
|
2025-10-21 21:36:42 -07:00
|
|
|
teamsWithOwners,
|
|
|
|
|
isDraftOrderSet,
|
2025-11-14 20:39:51 -08:00
|
|
|
sportsSeasons,
|
2025-10-14 21:20:58 -07:00
|
|
|
} = loaderData;
|
2025-10-11 01:03:31 -07:00
|
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
2025-10-14 12:20:36 -07:00
|
|
|
const [copied, setCopied] = useState(false);
|
2025-10-11 01:03:31 -07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (searchParams.get("updated") === "true") {
|
2025-10-14 22:04:37 -07:00
|
|
|
toast.success("Team updated successfully");
|
2025-10-14 12:20:36 -07:00
|
|
|
setSearchParams({});
|
|
|
|
|
}
|
|
|
|
|
if (searchParams.get("joined") === "true") {
|
|
|
|
|
toast.success("Welcome to the league! You've been assigned a team.");
|
2025-10-11 01:03:31 -07:00
|
|
|
setSearchParams({});
|
|
|
|
|
}
|
2025-10-14 22:04:37 -07:00
|
|
|
if (searchParams.get("left") === "true") {
|
|
|
|
|
toast.success("You have left the league. Your team is now available.");
|
|
|
|
|
setSearchParams({});
|
|
|
|
|
}
|
2025-10-11 01:03:31 -07:00
|
|
|
}, [searchParams, setSearchParams]);
|
2025-10-11 00:07:39 -07:00
|
|
|
|
2025-10-14 12:20:36 -07:00
|
|
|
const handleCopyInviteLink = async () => {
|
|
|
|
|
if (!season?.inviteCode) return;
|
2025-10-14 21:24:58 -07:00
|
|
|
|
2025-10-14 12:20:36 -07:00
|
|
|
const inviteUrl = `${window.location.origin}/i/${season.inviteCode}`;
|
|
|
|
|
try {
|
|
|
|
|
await navigator.clipboard.writeText(inviteUrl);
|
|
|
|
|
setCopied(true);
|
|
|
|
|
toast.success("Invite link copied to clipboard!");
|
|
|
|
|
setTimeout(() => setCopied(false), 2000);
|
2025-10-14 21:24:58 -07:00
|
|
|
} catch {
|
2025-10-14 12:20:36 -07:00
|
|
|
toast.error("Failed to copy invite link");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-11 00:07:39 -07:00
|
|
|
return (
|
|
|
|
|
<div className="container mx-auto py-8 px-4">
|
|
|
|
|
<div className="mb-8">
|
|
|
|
|
<div className="flex items-center justify-between mb-2">
|
|
|
|
|
<h1 className="text-4xl font-bold">{league.name}</h1>
|
2025-10-11 00:29:04 -07:00
|
|
|
<div className="flex gap-2">
|
2025-10-14 22:04:37 -07:00
|
|
|
{teams.find((t) => t.ownerId === currentUserId) && (
|
|
|
|
|
<Button variant="outline" asChild>
|
|
|
|
|
<Link
|
|
|
|
|
to={`/teams/${teams.find((t) => t.ownerId === currentUserId)?.id}/settings`}
|
|
|
|
|
>
|
|
|
|
|
Team Settings
|
|
|
|
|
</Link>
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2025-10-11 00:29:04 -07:00
|
|
|
{isUserCommissioner && (
|
|
|
|
|
<Button variant="outline" asChild>
|
2025-10-14 22:04:37 -07:00
|
|
|
<Link to={`/leagues/${league.id}/settings`}>
|
|
|
|
|
League Settings
|
|
|
|
|
</Link>
|
2025-10-11 00:29:04 -07:00
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2025-10-11 00:07:39 -07:00
|
|
|
</div>
|
|
|
|
|
{season && (
|
|
|
|
|
<p className="text-muted-foreground">
|
|
|
|
|
{season.year} Season • Status:{" "}
|
|
|
|
|
<span className="capitalize">
|
|
|
|
|
{season.status.replace("_", " ")}
|
|
|
|
|
</span>
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-10-14 21:54:46 -07:00
|
|
|
<div className="grid gap-6 md:grid-cols-3">
|
|
|
|
|
{/* Left Column - 2/3 width on desktop */}
|
|
|
|
|
<div className="md:col-span-2 space-y-6">
|
2025-11-14 20:39:51 -08:00
|
|
|
{/* Sports Seasons Section */}
|
|
|
|
|
{season && sportsSeasons.length > 0 && (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>Sports Seasons</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
{sportsSeasons.length} sport{sportsSeasons.length !== 1 ? "s" : ""} in this season
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
<div className="grid gap-4 sm:grid-cols-2">
|
|
|
|
|
{sportsSeasons.map((sportSeason) => (
|
|
|
|
|
<SportSeasonCard
|
|
|
|
|
key={sportSeason.id}
|
|
|
|
|
leagueId={league.id}
|
|
|
|
|
sportSeasonId={sportSeason.id}
|
|
|
|
|
sportName={sportSeason.sport.name}
|
|
|
|
|
seasonName={sportSeason.name}
|
|
|
|
|
status={sportSeason.status as "upcoming" | "active" | "completed"}
|
|
|
|
|
scoringPattern={sportSeason.scoringPattern}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-10-14 21:54:46 -07:00
|
|
|
{isUserCommissioner && season && availableTeamCount > 0 && (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>Invite Link</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
Share this link to invite people to join your league (
|
|
|
|
|
{availableTeamCount} spot{availableTeamCount !== 1 ? "s" : ""}{" "}
|
|
|
|
|
available)
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent className="space-y-3">
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
readOnly
|
|
|
|
|
value={`${typeof window !== "undefined" ? window.location.origin : ""}/i/${season.inviteCode}`}
|
|
|
|
|
className="flex-1 px-3 py-2 text-sm border rounded-md bg-muted"
|
|
|
|
|
onClick={(e) => e.currentTarget.select()}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleCopyInviteLink}
|
|
|
|
|
variant={copied ? "default" : "outline"}
|
|
|
|
|
size="sm"
|
|
|
|
|
>
|
|
|
|
|
{copied ? "Copied!" : "Copy"}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-xs text-muted-foreground">
|
|
|
|
|
Anyone with this link can join your league
|
|
|
|
|
</p>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Card>
|
2025-10-14 21:20:58 -07:00
|
|
|
<CardHeader>
|
2025-10-14 21:54:46 -07:00
|
|
|
<CardTitle>Teams</CardTitle>
|
2025-10-14 21:20:58 -07:00
|
|
|
<CardDescription>
|
2025-10-14 22:04:37 -07:00
|
|
|
{teams.length} team{teams.length !== 1 ? "s" : ""} in this
|
|
|
|
|
league
|
2025-10-14 21:20:58 -07:00
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
2025-10-14 21:54:46 -07:00
|
|
|
<CardContent>
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
{teams.map((team) => {
|
|
|
|
|
const isOwned = team.ownerId === currentUserId;
|
2025-10-14 22:04:37 -07:00
|
|
|
const ownerName = team.ownerId
|
|
|
|
|
? ownerMap[team.ownerId]
|
|
|
|
|
: null;
|
2025-10-14 21:54:46 -07:00
|
|
|
return (
|
|
|
|
|
<Card
|
|
|
|
|
key={team.id}
|
|
|
|
|
className={isOwned ? "border-primary" : ""}
|
|
|
|
|
>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="text-lg">{team.name}</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
{team.ownerId ? (
|
|
|
|
|
isOwned ? (
|
|
|
|
|
<span className="text-primary font-medium">
|
|
|
|
|
Your Team
|
|
|
|
|
</span>
|
|
|
|
|
) : (
|
|
|
|
|
<span>{ownerName || "Unknown Owner"}</span>
|
|
|
|
|
)
|
|
|
|
|
) : (
|
|
|
|
|
<span className="text-muted-foreground">
|
|
|
|
|
Available
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2025-10-14 21:20:58 -07:00
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
2025-10-14 21:54:46 -07:00
|
|
|
</div>
|
2025-10-11 00:07:39 -07:00
|
|
|
|
2025-10-14 21:54:46 -07:00
|
|
|
{/* Right Column - 1/3 width on desktop */}
|
|
|
|
|
<div className="space-y-6">
|
2025-10-15 08:58:35 -07:00
|
|
|
{/* Draft Order Display - Only show during pre_draft and draft */}
|
2025-10-20 15:53:38 -07:00
|
|
|
{season &&
|
|
|
|
|
(season.status === "pre_draft" || season.status === "draft") &&
|
|
|
|
|
draftSlots.length > 0 && (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div>
|
|
|
|
|
<CardTitle>Draft Order</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
{season.status === "pre_draft"
|
|
|
|
|
? "Upcoming draft order"
|
|
|
|
|
: "Current draft order"}
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</div>
|
|
|
|
|
<Button asChild size="sm">
|
|
|
|
|
<Link to={`/leagues/${league.id}/draft/${season.id}`}>
|
|
|
|
|
Enter Draft Room
|
|
|
|
|
</Link>
|
|
|
|
|
</Button>
|
2025-10-17 12:30:58 -07:00
|
|
|
</div>
|
2025-10-20 15:53:38 -07:00
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
{draftSlots.map((slot: any, index: number) => {
|
|
|
|
|
const ownerName = slot.team.ownerId
|
|
|
|
|
? ownerMap[slot.team.ownerId]
|
|
|
|
|
: null;
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={slot.id}
|
|
|
|
|
className="flex items-center gap-3 py-2 border-b last:border-0"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-center justify-center w-7 h-7 rounded-full bg-primary text-primary-foreground font-bold text-xs">
|
|
|
|
|
{index + 1}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex-1 min-w-0">
|
|
|
|
|
<p className="font-medium text-sm truncate">
|
|
|
|
|
{slot.team.name}
|
|
|
|
|
</p>
|
|
|
|
|
{ownerName && (
|
|
|
|
|
<p className="text-xs text-muted-foreground truncate">
|
|
|
|
|
{ownerName}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2025-10-15 08:58:35 -07:00
|
|
|
</div>
|
2025-10-20 15:53:38 -07:00
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
)}
|
2025-10-15 08:58:35 -07:00
|
|
|
|
2025-10-14 21:54:46 -07:00
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>League Info</CardTitle>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent className="space-y-2">
|
2025-10-11 00:07:39 -07:00
|
|
|
<div>
|
2025-10-14 21:54:46 -07:00
|
|
|
<p className="text-sm text-muted-foreground">Created</p>
|
|
|
|
|
<p className="font-medium">
|
|
|
|
|
{new Date(league.createdAt).toLocaleDateString()}
|
2025-10-11 00:07:39 -07:00
|
|
|
</p>
|
|
|
|
|
</div>
|
2025-10-14 21:54:46 -07:00
|
|
|
{season && (
|
2025-10-15 21:50:02 -07:00
|
|
|
<>
|
|
|
|
|
<div>
|
2025-10-20 15:53:38 -07:00
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Season Status
|
|
|
|
|
</p>
|
2025-10-15 21:50:02 -07:00
|
|
|
<p className="font-medium capitalize">
|
|
|
|
|
{season.status.replace("_", " ")}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2025-10-21 21:36:42 -07:00
|
|
|
<div>
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Teams Filled
|
|
|
|
|
</p>
|
|
|
|
|
<p className="font-medium">
|
|
|
|
|
{teamsWithOwners} of {teams.length}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Draft Order Set
|
|
|
|
|
</p>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<p className="font-medium">
|
|
|
|
|
{isDraftOrderSet ? "Yes" : "No"}
|
|
|
|
|
</p>
|
|
|
|
|
{!isDraftOrderSet && isUserCommissioner && (
|
|
|
|
|
<Button size="sm" variant="outline" asChild>
|
|
|
|
|
<Link to={`/leagues/${league.id}/settings#draft-order`}>
|
|
|
|
|
Set Order
|
|
|
|
|
</Link>
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-10-15 21:50:02 -07:00
|
|
|
<div>
|
2025-10-20 15:53:38 -07:00
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Draft Rounds
|
|
|
|
|
</p>
|
2025-10-15 21:50:02 -07:00
|
|
|
<p className="font-medium">{season.draftRounds}</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
2025-10-20 15:53:38 -07:00
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Sports Selected
|
|
|
|
|
</p>
|
2025-10-15 21:50:02 -07:00
|
|
|
<p className="font-medium">{sportsCount}</p>
|
|
|
|
|
</div>
|
2025-10-20 15:53:38 -07:00
|
|
|
<div>
|
|
|
|
|
<p className="text-sm text-muted-foreground">Draft Board</p>
|
|
|
|
|
<Link
|
|
|
|
|
to={`/leagues/${league.id}/draft-board/${season.id}`}
|
2026-02-20 19:26:11 -08:00
|
|
|
className="text-electric hover:underline font-medium"
|
2025-10-20 15:53:38 -07:00
|
|
|
>
|
|
|
|
|
View Draft Board
|
|
|
|
|
</Link>
|
2025-11-13 13:24:03 -08:00
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-sm text-muted-foreground">Standings</p>
|
|
|
|
|
<Link
|
|
|
|
|
to={`/leagues/${league.id}/standings/${season.id}`}
|
2026-02-20 19:26:11 -08:00
|
|
|
className="text-electric hover:underline font-medium"
|
2025-11-13 13:24:03 -08:00
|
|
|
>
|
|
|
|
|
View Standings
|
|
|
|
|
</Link>
|
2025-10-20 15:53:38 -07:00
|
|
|
</div>
|
2025-10-15 21:50:02 -07:00
|
|
|
<div>
|
|
|
|
|
<p className="text-sm text-muted-foreground">Flex Spots</p>
|
|
|
|
|
<p className="font-medium text-primary">
|
|
|
|
|
{Math.max(0, season.draftRounds - sportsCount)}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2025-10-15 22:02:21 -07:00
|
|
|
{season.draftDateTime && (
|
|
|
|
|
<div>
|
2025-10-20 15:53:38 -07:00
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Draft Date
|
|
|
|
|
</p>
|
2025-10-15 22:02:21 -07:00
|
|
|
<p className="font-medium">
|
|
|
|
|
{format(new Date(season.draftDateTime), "PPP 'at' p")}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-10-15 21:50:02 -07:00
|
|
|
</>
|
2025-10-14 21:54:46 -07:00
|
|
|
)}
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
2025-10-11 00:29:04 -07:00
|
|
|
|
2025-10-14 21:54:46 -07:00
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>Commissioners</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
{commissioners.length} commissioner
|
|
|
|
|
{commissioners.length !== 1 ? "s" : ""}
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
{commissioners.map((commissioner) => {
|
|
|
|
|
const commissionerName = commissionerMap[commissioner.userId];
|
2026-02-20 08:45:09 -08:00
|
|
|
const hasTeam = teams.some(
|
|
|
|
|
(t) => t.ownerId === commissioner.userId
|
|
|
|
|
);
|
2025-10-14 21:54:46 -07:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={commissioner.id}
|
|
|
|
|
className="py-2 border-b last:border-0"
|
|
|
|
|
>
|
|
|
|
|
<p className="font-medium">
|
|
|
|
|
{commissionerName || "Unknown User"}
|
|
|
|
|
{commissioner.userId === currentUserId && (
|
2025-10-14 22:04:37 -07:00
|
|
|
<span className="ml-2 text-xs text-primary">
|
|
|
|
|
(You)
|
|
|
|
|
</span>
|
2025-10-14 21:54:46 -07:00
|
|
|
)}
|
|
|
|
|
</p>
|
2026-02-20 08:45:09 -08:00
|
|
|
{!hasTeam && (
|
|
|
|
|
<p className="text-xs text-muted-foreground">
|
|
|
|
|
No team
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2025-10-14 21:54:46 -07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
</div>
|
2025-10-11 00:07:39 -07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|