From e6aac61b8635ba66c74aba321b41b36a4d3d1dfd Mon Sep 17 00:00:00 2001 From: Dominik Stumpf Date: Mon, 2 Dec 2024 11:40:05 +0100 Subject: [PATCH] chore: migrate from role-group to page, rename route params --- src/app/(dashboard)/[guildId]/fetchers.ts | 28 -------------- src/app/(dashboard)/[guildId]/page.tsx | 13 ------- .../[roleGroupUrlName]}/page.tsx | 37 +++++-------------- .../{[guildId] => [guildUrlName]}/actions.ts | 2 +- .../components/CreateRoleGroup.tsx | 2 +- .../components/GuildTabs.tsx | 0 .../components/JoinButton.tsx | 0 .../components/RoleGroupNavLink.tsx | 0 .../(dashboard)/[guildUrlName]/fetchers.ts | 29 +++++++++++++++ .../{[guildId] => [guildUrlName]}/layout.tsx | 19 +++++----- src/app/(dashboard)/[guildUrlName]/page.tsx | 10 +++++ src/lib/fetcher.ts | 7 ++++ 12 files changed, 66 insertions(+), 81 deletions(-) delete mode 100644 src/app/(dashboard)/[guildId]/fetchers.ts delete mode 100644 src/app/(dashboard)/[guildId]/page.tsx rename src/app/(dashboard)/{[guildId]/[roleGroupId] => [guildUrlName]/[roleGroupUrlName]}/page.tsx (74%) rename src/app/(dashboard)/{[guildId] => [guildUrlName]}/actions.ts (92%) rename src/app/(dashboard)/{[guildId] => [guildUrlName]}/components/CreateRoleGroup.tsx (98%) rename src/app/(dashboard)/{[guildId] => [guildUrlName]}/components/GuildTabs.tsx (100%) rename src/app/(dashboard)/{[guildId] => [guildUrlName]}/components/JoinButton.tsx (100%) rename src/app/(dashboard)/{[guildId] => [guildUrlName]}/components/RoleGroupNavLink.tsx (100%) create mode 100644 src/app/(dashboard)/[guildUrlName]/fetchers.ts rename src/app/(dashboard)/{[guildId] => [guildUrlName]}/layout.tsx (82%) create mode 100644 src/app/(dashboard)/[guildUrlName]/page.tsx diff --git a/src/app/(dashboard)/[guildId]/fetchers.ts b/src/app/(dashboard)/[guildId]/fetchers.ts deleted file mode 100644 index fa162d6237..0000000000 --- a/src/app/(dashboard)/[guildId]/fetchers.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { env } from "@/lib/env"; -import type { Guild } from "@/lib/schemas/guild"; -import type { RoleGroup } from "@/lib/schemas/roleGroup"; -import type { PaginatedResponse } from "@/lib/types"; - -export const getGuild = async (urlName: string) => { - const res = await fetch(`${env.NEXT_PUBLIC_API}/guild/urlName/${urlName}`, { - next: { - tags: [`guild-${urlName}`], - }, - }); - const data: Guild = await res.json(); - return data; -}; - -export const getRoleGroups = async (guildId: string) => { - const res = await fetch( - `${env.NEXT_PUBLIC_API}/role-group/search?customQuery=@guildId:{${guildId}}&pageSize=${Number.MAX_SAFE_INTEGER}`, - { - next: { - tags: [`role-groups-${guildId}`], - revalidate: 3600, - }, - }, - ); - const data: PaginatedResponse = await res.json(); - return data.items; -}; diff --git a/src/app/(dashboard)/[guildId]/page.tsx b/src/app/(dashboard)/[guildId]/page.tsx deleted file mode 100644 index e0640bc9bb..0000000000 --- a/src/app/(dashboard)/[guildId]/page.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import type { DynamicRoute } from "@/lib/types"; -import GuildPage from "./[roleGroupId]/page"; - -const DefaultGuildPage = async ({ - params, -}: DynamicRoute<{ guildId: string }>) => { - const { guildId: urlName } = await params; - //const guild = await getGuild(urlName); - //return `Default guild page - ${guild.name}`; - return ; -}; - -export default DefaultGuildPage; diff --git a/src/app/(dashboard)/[guildId]/[roleGroupId]/page.tsx b/src/app/(dashboard)/[guildUrlName]/[roleGroupUrlName]/page.tsx similarity index 74% rename from src/app/(dashboard)/[guildId]/[roleGroupId]/page.tsx rename to src/app/(dashboard)/[guildUrlName]/[roleGroupUrlName]/page.tsx index a3ddc0d062..c95dc40ad2 100644 --- a/src/app/(dashboard)/[guildId]/[roleGroupId]/page.tsx +++ b/src/app/(dashboard)/[guildUrlName]/[roleGroupUrlName]/page.tsx @@ -7,24 +7,25 @@ import type { Guild } from "@/lib/schemas/guild"; import type { Role } from "@/lib/schemas/role"; import type { RoleGroup } from "@/lib/schemas/roleGroup"; import type { DynamicRoute, PaginatedResponse } from "@/lib/types"; +import type { Schemas } from "@guildxyz/types"; import { Lock } from "@phosphor-icons/react/dist/ssr"; const RoleGroupPage = async ({ params, -}: DynamicRoute<{ roleGroupId: string; guildId: string }>) => { - const { roleGroupId: roleGroupIdParam, guildId: guildIdParam } = await params; +}: DynamicRoute<{ roleGroupUrlName: string; guildUrlName: string }>) => { + const { roleGroupUrlName, guildUrlName } = await params; const guild = (await ( - await fetch(`${env.NEXT_PUBLIC_API}/guild/urlName/${guildIdParam}`) + await fetch(`${env.NEXT_PUBLIC_API}/guild/urlName/${guildUrlName}`) ).json()) as Guild; const paginatedRoleGroup = (await ( await fetch( - `${env.NEXT_PUBLIC_API}/role-group/search?customQuery=@guildId:{${guild.id}}&pageSize=${Number.MAX_SAFE_INTEGER}`, + `${env.NEXT_PUBLIC_API}/page/search?customQuery=@guildId:{${guild.id}}&pageSize=${Number.MAX_SAFE_INTEGER}`, ) ).json()) as PaginatedResponse; const roleGroups = paginatedRoleGroup.items; const roleGroup = roleGroups.find( // @ts-expect-error - (rg) => rg.urlName === roleGroupIdParam || rg.id === guild.homeRoleGroupId, + (rg) => rg.urlName === roleGroupUrlName || rg.id === guild.homeRoleGroupId, )!; const paginatedRole = await fetcher>( `${env.NEXT_PUBLIC_API}/role/search?customQuery=@guildId:{${guild.id}}&pageSize=${Number.MAX_SAFE_INTEGER}`, @@ -40,38 +41,18 @@ const RoleGroupPage = async ({ ); }; -const RE = { - id: "aca776be-5a7b-4618-bb7c-1130de066257", - createdAt: 1732825819745, - updatedAt: 1732825819745, - name: "Home - delete", - guildId: "2b3330e4-05fa-4949-a80c-2deeb99d100e", - urlName: "stars-guild-delete", - foreignEntity: "role-group", - foreignIdentifier: "1b06e1e7-5bf8-4f55-8e7b-16c5542d10c3", - description: - 'Grants delete access to the "Home" role group in the "Stars Guild" guild', - type: "GUILD", - permissions: { - read: "b62cda4f-27d6-40dc-9625-34a6ce74912a", - update: "78ac4c72-cc44-4336-b5cd-e73e93fd86e7", - delete: "d713b547-135d-49ab-a9b1-e670ad1871a7", - }, -}; -type Reward = typeof RE; - const RoleCard = async ({ role }: { role: Role }) => { const rewards = (await Promise.all( // @ts-ignore role.rewards?.map(({ rewardId }) => { const req = `${env.NEXT_PUBLIC_API}/reward/id/${rewardId}`; try { - return fetcher(req); + return fetcher(req); } catch { console.error({ rewardId, req }); } }) ?? [], - )) as Reward[]; + )) as Schemas["RewardFull"][]; return ( @@ -114,7 +95,7 @@ const RoleCard = async ({ role }: { role: Role }) => { ); }; -const Reward = ({ reward }: { reward: Reward }) => { +const Reward = ({ reward }: { reward: Schemas["RewardFull"] }) => { return (
{reward.name}
diff --git a/src/app/(dashboard)/[guildId]/actions.ts b/src/app/(dashboard)/[guildUrlName]/actions.ts similarity index 92% rename from src/app/(dashboard)/[guildId]/actions.ts rename to src/app/(dashboard)/[guildUrlName]/actions.ts index 50f0392fc3..910c3c1b0f 100644 --- a/src/app/(dashboard)/[guildId]/actions.ts +++ b/src/app/(dashboard)/[guildUrlName]/actions.ts @@ -4,7 +4,7 @@ import { env } from "@/lib/env"; import { revalidateTag } from "next/cache"; export const revalidateRoleGroups = async (guildId: string) => { - revalidateTag(`role-groups-${guildId}`); + revalidateTag(`page-${guildId}`); }; export const joinGuild = async ({ guildId }: { guildId: string }) => { diff --git a/src/app/(dashboard)/[guildId]/components/CreateRoleGroup.tsx b/src/app/(dashboard)/[guildUrlName]/components/CreateRoleGroup.tsx similarity index 98% rename from src/app/(dashboard)/[guildId]/components/CreateRoleGroup.tsx rename to src/app/(dashboard)/[guildUrlName]/components/CreateRoleGroup.tsx index 3018d1d0ea..adbe056497 100644 --- a/src/app/(dashboard)/[guildId]/components/CreateRoleGroup.tsx +++ b/src/app/(dashboard)/[guildUrlName]/components/CreateRoleGroup.tsx @@ -98,7 +98,7 @@ const CreateRoleGroupDialogForm = ({ if (!token) throw new Error("Unauthorized"); // TODO: custom errors? - return fetcher(`${env.NEXT_PUBLIC_API}/role-group`, { + return fetcher(`${env.NEXT_PUBLIC_API}/page`, { method: "POST", headers: { "X-Auth-Token": token, diff --git a/src/app/(dashboard)/[guildId]/components/GuildTabs.tsx b/src/app/(dashboard)/[guildUrlName]/components/GuildTabs.tsx similarity index 100% rename from src/app/(dashboard)/[guildId]/components/GuildTabs.tsx rename to src/app/(dashboard)/[guildUrlName]/components/GuildTabs.tsx diff --git a/src/app/(dashboard)/[guildId]/components/JoinButton.tsx b/src/app/(dashboard)/[guildUrlName]/components/JoinButton.tsx similarity index 100% rename from src/app/(dashboard)/[guildId]/components/JoinButton.tsx rename to src/app/(dashboard)/[guildUrlName]/components/JoinButton.tsx diff --git a/src/app/(dashboard)/[guildId]/components/RoleGroupNavLink.tsx b/src/app/(dashboard)/[guildUrlName]/components/RoleGroupNavLink.tsx similarity index 100% rename from src/app/(dashboard)/[guildId]/components/RoleGroupNavLink.tsx rename to src/app/(dashboard)/[guildUrlName]/components/RoleGroupNavLink.tsx diff --git a/src/app/(dashboard)/[guildUrlName]/fetchers.ts b/src/app/(dashboard)/[guildUrlName]/fetchers.ts new file mode 100644 index 0000000000..3d91b4805f --- /dev/null +++ b/src/app/(dashboard)/[guildUrlName]/fetchers.ts @@ -0,0 +1,29 @@ +import { env } from "@/lib/env"; +import { fetcher } from "@/lib/fetcher"; +import type { PaginatedResponse } from "@/lib/types"; +import type { Schemas } from "@guildxyz/types"; + +export const getGuild = async (urlName: string) => { + return await fetcher( + `${env.NEXT_PUBLIC_API}/guild/urlName/${urlName}`, + { + next: { + tags: [`guild-${urlName}`], + }, + }, + ); +}; + +export const getRoleGroups = async (guildId: string) => { + return ( + await fetcher>( + `${env.NEXT_PUBLIC_API}/page/search?customQuery=@guildId:{${guildId}}&pageSize=${Number.MAX_SAFE_INTEGER}`, + { + next: { + tags: [`page-${guildId}`], + revalidate: 3600, + }, + }, + ) + ).items; +}; diff --git a/src/app/(dashboard)/[guildId]/layout.tsx b/src/app/(dashboard)/[guildUrlName]/layout.tsx similarity index 82% rename from src/app/(dashboard)/[guildId]/layout.tsx rename to src/app/(dashboard)/[guildUrlName]/layout.tsx index ae10acd8a4..e50e749c1d 100644 --- a/src/app/(dashboard)/[guildId]/layout.tsx +++ b/src/app/(dashboard)/[guildUrlName]/layout.tsx @@ -14,18 +14,17 @@ import { JoinButton } from "./components/JoinButton"; const GuildPage = async ({ params, children, -}: PropsWithChildren>) => { - const { guildId: guildIdParam } = await params; +}: PropsWithChildren>) => { + const { guildUrlName } = await params; const guild = await fetcher( - `${env.NEXT_PUBLIC_API}/guild/urlName/${guildIdParam}`, + `${env.NEXT_PUBLIC_API}/guild/urlName/${guildUrlName}`, ); const token = await getParsedToken(); - if (!token) { - throw new Error("Failed to authenticate"); - } - const user = await fetcher( - `${env.NEXT_PUBLIC_API}/user/id/${token.userId}`, - ); + const user = + token && + (await fetcher( + `${env.NEXT_PUBLIC_API}/user/id/${token.userId}`, + )); return (
@@ -43,7 +42,7 @@ const GuildPage = async ({
}> - + {user && }

diff --git a/src/app/(dashboard)/[guildUrlName]/page.tsx b/src/app/(dashboard)/[guildUrlName]/page.tsx new file mode 100644 index 0000000000..e3516b20f6 --- /dev/null +++ b/src/app/(dashboard)/[guildUrlName]/page.tsx @@ -0,0 +1,10 @@ +import type { DynamicRoute } from "@/lib/types"; +import GuildPage from "./[roleGroupUrlName]/page"; + +const DefaultGuildPage = async ({ + params, +}: DynamicRoute<{ guildUrlName: string }>) => { + return ; +}; + +export default DefaultGuildPage; diff --git a/src/lib/fetcher.ts b/src/lib/fetcher.ts index 2a3340bc48..64393cc420 100644 --- a/src/lib/fetcher.ts +++ b/src/lib/fetcher.ts @@ -11,6 +11,13 @@ export const fetcher = async ( ? await response.json() : await response.text(); + if (process.env.NODE_ENV === "development") { + console.info( + `[${new Date().toLocaleTimeString()} - fetcher]: ${resource}`, + res, + ); + } + if (!response.ok) { if (resource.includes(env.NEXT_PUBLIC_API)) { return Promise.reject({