Skip to content

Commit

Permalink
feat: use game selector when chosen group has more than 1 game
Browse files Browse the repository at this point in the history
  • Loading branch information
jog1t committed Sep 25, 2024
1 parent dc06deb commit a95f90e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
48 changes: 35 additions & 13 deletions apps/hub/src/components/breadcrumbs/game-breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { GameAvatar } from "@/domains/game/components/game-avatar";
import { gameQueryOptions } from "@/domains/game/queries";
import { GroupGameSelect } from "@/domains/game/components/group-game-select";
import {
gameQueryOptions,
gamesCountQueryOptions,
} from "@/domains/game/queries";
import { useSuspenseQuery } from "@tanstack/react-query";
import { Link } from "@tanstack/react-router";
import { Link, useNavigate } from "@tanstack/react-router";
import { Fragment, useContext } from "react";
import { NavItem } from "../header/nav-item";
import { GroupBreadcrumb } from "./group-breadcrumb";
Expand All @@ -14,6 +18,16 @@ interface GameBreadcrumbProps {

export function GameBreadcrumb({ gameId }: GameBreadcrumbProps) {
const { data } = useSuspenseQuery(gameQueryOptions(gameId));
const { data: gamesCount } = useSuspenseQuery(
gamesCountQueryOptions(data.developerGroupId),
);

const navigate = useNavigate();
const handleGameChange = (gameId: string) => {
navigate({
params: { gameId },
});
};

const isMobile = useContext(MobileBreadcrumbsContext);

Expand All @@ -24,18 +38,26 @@ export function GameBreadcrumb({ gameId }: GameBreadcrumbProps) {
<GroupBreadcrumb groupId={data.developerGroupId} />
<Separator />
<Element>
<Link
to="/games/$gameId"
params={{ gameId }}
className="flex items-center gap-2"
>
<GameAvatar
displayName={data.displayName}
logoUrl={data.logoUrl}
className={isMobile ? "size-4" : "size-5"}
{gamesCount > 1 ? (
<GroupGameSelect
groupId={data.developerGroupId}
value={gameId}
onValueChange={handleGameChange}
/>
{data.displayName}
</Link>
) : (
<Link
to="/games/$gameId"
params={{ gameId }}
className="flex items-center gap-2"
>
<GameAvatar
displayName={data.displayName}
logoUrl={data.logoUrl}
className={isMobile ? "size-4" : "size-5"}
/>
{data.displayName}
</Link>
)}
</Element>
</>
);
Expand Down
42 changes: 42 additions & 0 deletions apps/hub/src/domains/game/components/group-game-select.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { groupGamesQueryOptions } from "@/domains/game/queries";
import {
AssetImage,
Flex,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@rivet-gg/components";
import { useSuspenseQuery } from "@tanstack/react-query";
import type { ComponentProps } from "react";

interface GroupGameSelectProps extends ComponentProps<typeof Select> {
groupId: string;
}

export function GroupGameSelect({ groupId, ...props }: GroupGameSelectProps) {
const { data } = useSuspenseQuery(groupGamesQueryOptions(groupId));

return (
<Select {...props}>
<SelectTrigger>
<SelectValue placeholder="Select game..." />
</SelectTrigger>
<SelectContent>
{data.games.map((game, index, groupList) => (
<SelectItem key={game.gameId} value={game.gameId}>
<Flex gap="2" items="center">
<AssetImage
src={game.logoUrl || "/games/blank/blankgame.svg"}
className="mx-auto size-5 object-contain"
alt="Game logo"
/>
{game.displayName}
</Flex>
</SelectItem>
))}
</SelectContent>
</Select>
);
}
7 changes: 7 additions & 0 deletions apps/hub/src/domains/game/queries/query-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export const groupOnwerQueryOptions = (groupId: string) => {
});
};

export const gamesCountQueryOptions = (groupId: string) => {
return queryOptions({
...groupGamesQueryOptions(groupId),
select: (data) => data.games.length,
});
};

export const gameQueryOptions = (gameId: string) => {
return queryOptions({
queryKey: ["game", gameId],
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/ui/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const SelectTrigger = React.forwardRef<
<SelectPrimitive.Trigger
ref={ref}
className={cn(
"flex h-10 w-full items-center justify-between gap-2 rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
"flex whitespace-nowrap h-10 w-full items-center justify-between gap-2 rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
className,
)}
{...props}
Expand Down

0 comments on commit a95f90e

Please sign in to comment.