Skip to content

Commit

Permalink
feat: use group selector when user has more than 1 group
Browse files Browse the repository at this point in the history
  • Loading branch information
jog1t committed Sep 25, 2024
1 parent 81120fb commit 45afcd3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
51 changes: 35 additions & 16 deletions apps/hub/src/components/breadcrumbs/group-breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { groupGamesQueryOptions } from "@/domains/game/queries";
import {
groupGamesQueryOptions,
groupsCountQueryOptions,
} from "@/domains/game/queries";
import { GroupAvatar } from "@/domains/group/components/group-avatar";
import { useSuspenseQuery } from "@tanstack/react-query";
import { Link } from "@tanstack/react-router";
import { GroupSelect } from "@/domains/group/components/group-select";
import { useSuspenseQueries } from "@tanstack/react-query";
import { Link, useNavigate } from "@tanstack/react-router";
import { Fragment, useContext } from "react";
import { NavItem } from "../header/nav-item";
import { MobileBreadcrumbsContext } from "./mobile-breadcrumbs";
Expand All @@ -11,26 +15,41 @@ interface GroupBreadcrumbProps {
}

export function GroupBreadcrumb({ groupId }: GroupBreadcrumbProps) {
const { data } = useSuspenseQuery(groupGamesQueryOptions(groupId));
const [{ data: groupsCount }, { data }] = useSuspenseQueries({
queries: [groupsCountQueryOptions(), groupGamesQueryOptions(groupId)],
});

const navigate = useNavigate();

const handleGroupChange = (groupId: string) => {
navigate({
to: "/teams/$groupId",
params: { groupId },
});
};

const isMobile = useContext(MobileBreadcrumbsContext);

const Element = isMobile ? NavItem : Fragment;

return (
<Element>
<Link
to="/teams/$groupId"
params={{ groupId }}
className="flex items-center gap-2"
>
<GroupAvatar
avatarUrl={data.avatarUrl}
displayName={data.displayName}
className={isMobile ? "size-4" : "size-5"}
/>
{data.displayName}
</Link>
{groupsCount > 1 ? (
<GroupSelect value={groupId} onValueChange={handleGroupChange} />
) : (
<Link
to="/teams/$groupId"
params={{ groupId }}
className="flex items-center gap-2"
>
<GroupAvatar
avatarUrl={data.avatarUrl}
displayName={data.displayName}
className={isMobile ? "size-4" : "size-5"}
/>
{data.displayName}
</Link>
)}
</Element>
);
}
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 @@ -26,6 +26,13 @@ export const gamesQueryOptions = () => {
});
};

export const groupsCountQueryOptions = () => {
return queryOptions({
...gamesQueryOptions(),
select: (data) => data.groups.length,
});
};

export const groupGamesQueryOptions = (groupId: string) => {
return queryOptions({
...gamesQueryOptions(),
Expand Down

0 comments on commit 45afcd3

Please sign in to comment.