-
Notifications
You must be signed in to change notification settings - Fork 438
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
b216960
commit b5bf336
Showing
8 changed files
with
112 additions
and
127 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import { env } from "@/lib/env"; | ||
import type { Guild } from "@/lib/schemas/guild"; | ||
import { fetchGuildApiData } from "@/lib/fetchGuildApi"; | ||
import { tryGetParsedToken } from "@/lib/token"; | ||
import type { PaginatedResponse } from "@/lib/types"; | ||
import { fetcher } from "../../../lib/fetcher"; | ||
import type { Schemas } from "@guildxyz/types"; | ||
import { PAGE_SIZE } from "./constants"; | ||
|
||
export const getGuildSearch = | ||
(search = "") => | ||
async ({ pageParam }: { pageParam: number }) => { | ||
return fetcher<PaginatedResponse<Guild>>( | ||
`${env.NEXT_PUBLIC_API}/guild/search?page=${pageParam}&pageSize=${PAGE_SIZE}&search=${search}`, | ||
); | ||
}; | ||
export const fetchAssociatedGuilds = async () => { | ||
const { userId } = await tryGetParsedToken(); | ||
return fetchGuildApiData<PaginatedResponse<Schemas["Guild"]>>( | ||
`guild/search?page=1&pageSize=${Number.MAX_SAFE_INTEGER}&sortBy=name&reverse=false&customQuery=@owner:{${userId}}`, | ||
); | ||
}; | ||
|
||
export const fetchGuildSearch = async ({ | ||
pageParam, | ||
search, | ||
}: { pageParam: number; search: string }) => { | ||
return fetchGuildApiData<PaginatedResponse<Schemas["Guild"]>>( | ||
`guild/search?page=${pageParam}&pageSize=${PAGE_SIZE}&search=${search}`, | ||
); | ||
}; |
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
This file was deleted.
Oops, something went wrong.