Skip to content

Commit

Permalink
feat : use mod unique id instead of cuid for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Juknum committed Aug 19, 2024
1 parent daec0ad commit 33189c6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/app/(pages)/mods/[modId]/gallery/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useEffectOnce } from '~/hooks/use-effect-once';
import { BREAKPOINT_MOBILE_LARGE, BREAKPOINT_TABLET, ITEMS_PER_PAGE, ITEMS_PER_ROW } from '~/lib/constants';
import { searchFilter } from '~/lib/utils';
import { getLatestContributionsOfModVersion } from '~/server/data/contributions';
import { getModVersionFromMod } from '~/server/data/mods-version';
import { getModVersionFromModForgeId } from '~/server/data/mods-version';
import { getTexturesFromModVersion } from '~/server/data/texture';

import type { ModVersion, Texture } from '@prisma/client';
Expand Down Expand Up @@ -48,7 +48,7 @@ export default function ModGalleryPage() {
const texturesGroupRef = useRef<HTMLDivElement>(null);

useEffectOnce(() => {
getModVersionFromMod(modId).then((versions) => {
getModVersionFromModForgeId(modId).then((versions) => {
setModVersions(versions);
setModVersionShown(versions[0]?.id ?? null);
});
Expand Down
6 changes: 3 additions & 3 deletions src/app/(pages)/mods/[modId]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { TextureImage } from '~/components/texture-img';
import { useDeviceSize } from '~/hooks/use-device-size';
import { useEffectOnce } from '~/hooks/use-effect-once';
import { BREAKPOINT_MOBILE_LARGE } from '~/lib/constants';
import { getModsFromIds, getModDownloads } from '~/server/data/mods';
import { getModDownloads, getModFromForgeId } from '~/server/data/mods';

import type { Mod } from '@prisma/client';
import type { Downloads } from '~/types';
Expand All @@ -35,8 +35,8 @@ export default function ModLayout({ children }: { children: React.ReactNode }) {
useEffectOnce(() => {
if (!modId) return;

getModsFromIds([modId])
.then((mod) => setMod(mod[0] ?? null))
getModFromForgeId(modId)
.then(setMod)
.then(() => getModDownloads(modId).then(setDownloads))
.finally(() => setLoading(false));
});
Expand Down
10 changes: 5 additions & 5 deletions src/app/(pages)/mods/[modId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { Tile } from '~/components/tile';
import { useDeviceSize } from '~/hooks/use-device-size';
import { useEffectOnce } from '~/hooks/use-effect-once';
import { BREAKPOINT_MOBILE_LARGE, ITEMS_PER_PAGE, RESOLUTIONS_COLORS, EMPTY_PROGRESSION, ITEMS_PER_PAGE_DEFAULT } from '~/lib/constants';
import { getModsFromIds } from '~/server/data/mods';
import { getModVersionFromMod, getModVersionProgressionFromMod } from '~/server/data/mods-version';
import { getModFromForgeId } from '~/server/data/mods';
import { getModVersionFromModForgeId, getModVersionProgressionFromModForgeId } from '~/server/data/mods-version';

import type { Mod, ModVersion } from '@prisma/client';
import type { Progression } from '~/types';
Expand Down Expand Up @@ -107,9 +107,9 @@ export default function ModPage() {
}, [search, versions]);

useEffectOnce(() => {
getModsFromIds([modId]).then((mod) => setMod(mod[0] ?? null));
getModVersionProgressionFromMod(modId).then(setProgressions);
getModVersionFromMod(modId).then((versions) => {
getModFromForgeId(modId).then(setMod);
getModVersionProgressionFromModForgeId(modId).then(setProgressions);
getModVersionFromModForgeId(modId).then((versions) => {
setVersions(versions);
setFilteredVersions(versions);
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/(pages)/mods/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export default function Mods() {
modsShown[activePage - 1] && modsShown[activePage - 1]?.map((m) => (
<Tile
key={m.id}
onClick={() => router.push(`/mods/${m.id}`)}
onClick={() => router.push(`/mods/${m.forgeId}`)}
className="cursor-pointer mod-card"
>
<Stack gap="xs">
Expand Down
11 changes: 7 additions & 4 deletions src/server/data/mods-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ export async function getModVersionsWithModpacks(modId: string): Promise<ModVers
return res;
}

export async function getModVersionFromMod(modId: string): Promise<ModVersion[]> {
return db.modVersion.findMany({ where: { modId } }).then((res) => res.sort((a, b) => sortBySemver(a.version, b.version)));
export async function getModVersionFromModForgeId(forgeId: string): Promise<ModVersion[]> {
const mod = await db.mod.findFirst({ where: { forgeId } });
if (!mod) return [];

return db.modVersion.findMany({ where: { modId: mod.id } });
}

export async function getModVersions(): Promise<ModVersion[]> {
Expand All @@ -69,8 +72,8 @@ export async function getModsVersionsFromResources(resourceIds: string[]): Promi
.then((res) => res.map((modVer) => ({ ...modVer, resources: modVer.resources.map((r) => r.id) })));
}

export async function getModVersionProgressionFromMod(modId: string): Promise<Record<string, Progression>> {
const modVersions = await db.modVersion.findMany({ where: { modId }, select: { id: true } });
export async function getModVersionProgressionFromModForgeId(forgeId: string): Promise<Record<string, Progression>> {
const modVersions = await getModVersionFromModForgeId(forgeId);
const modVersionsIds = modVersions.map((mv) => mv.id);

const progressions: Record<string, Progression> = {};
Expand Down
4 changes: 4 additions & 0 deletions src/server/data/mods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import type { Downloads } from '~/types';

// GET

export async function getModFromForgeId(forgeId: string): Promise<Mod | null> {
return db.mod.findFirst({ where: { forgeId } });
}

export async function getModsFromIds(ids: string[]): Promise<Mod[]> {
return db.mod.findMany({ where: { id: { in: ids } } });
}
Expand Down

0 comments on commit 33189c6

Please sign in to comment.