-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use tanstack query on explorer
- Loading branch information
1 parent
21d1af9
commit d9c46d2
Showing
6 changed files
with
103 additions
and
80 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/app/(dashboard)/explorer/components/AssociatedGuilds.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use client"; | ||
|
||
import { useSuspenseQuery } from "@tanstack/react-query"; | ||
import { associatedGuildsOption } from "../options"; | ||
import { CreateGuildLink } from "./CreateGuildLink"; | ||
import { GuildCard, GuildCardSkeleton } from "./GuildCard"; | ||
|
||
export const AssociatedGuilds = () => { | ||
const { data: associatedGuilds } = useSuspenseQuery(associatedGuildsOption()); | ||
|
||
return associatedGuilds.length > 0 ? ( | ||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3"> | ||
{associatedGuilds.map((guild) => ( | ||
<GuildCard key={guild.id} guild={guild} /> | ||
))} | ||
</div> | ||
) : ( | ||
<div className="flex items-center gap-4 rounded-2xl bg-card px-5 py-6"> | ||
<img src="/images/robot.svg" alt="Guild Robot" className="size-8" /> | ||
|
||
<p className="font-semibold"> | ||
You're not a member of any guilds yet. Explore and join some below, | ||
or create your own! | ||
</p> | ||
|
||
<CreateGuildLink className="ml-auto" /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const AssociatedGuildsSkeleton = () => { | ||
return ( | ||
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3"> | ||
{Array.from({ length: 3 }, (_, i) => ( | ||
// biome-ignore lint/suspicious/noArrayIndexKey: <explanation> | ||
<GuildCardSkeleton key={i} /> | ||
))} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletion
10
src/app/(dashboard)/explorer/actions.ts → src/app/(dashboard)/explorer/fetchers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getQueryClient } from "@/lib/getQueryClient"; | ||
import { HydrationBoundary, dehydrate } from "@tanstack/react-query"; | ||
import type { PropsWithChildren } from "react"; | ||
import { associatedGuildsOption, guildSearchOptions } from "./options"; | ||
|
||
const ExplorerLayout = async ({ children }: PropsWithChildren) => { | ||
const queryClient = getQueryClient(); | ||
void queryClient.prefetchInfiniteQuery( | ||
guildSearchOptions({}), | ||
//queryFn: () => fetchGuildSearch({ search: "", pageParam: 1 }), | ||
); | ||
void queryClient.prefetchQuery(associatedGuildsOption()); | ||
|
||
return ( | ||
<HydrationBoundary state={dehydrate(queryClient)}> | ||
{children} | ||
</HydrationBoundary> | ||
); | ||
}; | ||
|
||
export default ExplorerLayout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { infiniteQueryOptions, queryOptions } from "@tanstack/react-query"; | ||
import { fetchAssociatedGuilds, fetchGuildSearch } from "./fetchers"; | ||
|
||
export const associatedGuildsOption = () => { | ||
return queryOptions({ | ||
queryKey: ["associatedGuilds"], | ||
queryFn: () => fetchAssociatedGuilds(), | ||
select: (data) => data.items, | ||
}); | ||
}; | ||
|
||
export const guildSearchOptions = ({ search = "" }: { search?: string }) => { | ||
return infiniteQueryOptions({ | ||
queryKey: ["guilds", search], | ||
queryFn: ({ pageParam }) => fetchGuildSearch({ search: search, pageParam }), | ||
initialPageParam: 1, | ||
enabled: search !== undefined, | ||
getNextPageParam: (lastPage) => | ||
lastPage.total / lastPage.pageSize <= lastPage.page | ||
? undefined | ||
: lastPage.page + 1, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters