Create MoreComponents container
This commit is contained in:
parent
a466349707
commit
f263329b6c
@ -3,9 +3,9 @@ import { LARGE_PHOTOS_TO_SHOW, generateOgImageMetaForPhotos } from '@/photo';
|
|||||||
import PhotosEmptyState from '@/photo/PhotosEmptyState';
|
import PhotosEmptyState from '@/photo/PhotosEmptyState';
|
||||||
import { Metadata } from 'next/types';
|
import { Metadata } from 'next/types';
|
||||||
import { MAX_PHOTOS_TO_SHOW_OG } from '@/photo/image-response';
|
import { MAX_PHOTOS_TO_SHOW_OG } from '@/photo/image-response';
|
||||||
import MoreComponents from '@/components/MoreComponents';
|
|
||||||
import PhotosLarge from '@/photo/PhotosLarge';
|
import PhotosLarge from '@/photo/PhotosLarge';
|
||||||
import { Suspense } from 'react';
|
import { Suspense } from 'react';
|
||||||
|
import { MorePhotosLarge } from '@/photo/MorePhotosLarge';
|
||||||
|
|
||||||
export const revalidate = 3600;
|
export const revalidate = 3600;
|
||||||
|
|
||||||
@ -31,24 +31,10 @@ export default async function HomePage() {
|
|||||||
? <div className="space-y-1">
|
? <div className="space-y-1">
|
||||||
<PhotosLarge photos={photos} />
|
<PhotosLarge photos={photos} />
|
||||||
<Suspense>
|
<Suspense>
|
||||||
<MoreComponents
|
<MorePhotosLarge
|
||||||
label="More photos"
|
|
||||||
initialOffset={LARGE_PHOTOS_TO_SHOW}
|
initialOffset={LARGE_PHOTOS_TO_SHOW}
|
||||||
itemsPerRequest={LARGE_PHOTOS_TO_SHOW}
|
itemsPerRequest={LARGE_PHOTOS_TO_SHOW}
|
||||||
getNextComponent={async (offset, limit) => {
|
totalPhotosCount={count}
|
||||||
'use server';
|
|
||||||
const photos = await getPhotosCached({ limit: offset + limit })
|
|
||||||
.catch(() => undefined);
|
|
||||||
if (!photos) {
|
|
||||||
return { didError: true };
|
|
||||||
} else {
|
|
||||||
const nextPhotos = photos.slice(offset);
|
|
||||||
return {
|
|
||||||
nextComponent: <PhotosLarge photos={nextPhotos} />,
|
|
||||||
isFinished: offset + limit >= count,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</Suspense>
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -21,7 +21,7 @@ export default function MoreComponents({
|
|||||||
getNextComponent: (offset: number, limit: number) => Promise<{
|
getNextComponent: (offset: number, limit: number) => Promise<{
|
||||||
nextComponent?: JSX.Element,
|
nextComponent?: JSX.Element,
|
||||||
isFinished?: boolean,
|
isFinished?: boolean,
|
||||||
didError?: boolean,
|
didFail?: boolean,
|
||||||
}>
|
}>
|
||||||
label?: string
|
label?: string
|
||||||
triggerOnView?: boolean
|
triggerOnView?: boolean
|
||||||
@ -63,8 +63,8 @@ export default function MoreComponents({
|
|||||||
initialOffset + (indexToLoad - 1) * itemsPerRequest,
|
initialOffset + (indexToLoad - 1) * itemsPerRequest,
|
||||||
itemsPerRequest,
|
itemsPerRequest,
|
||||||
)
|
)
|
||||||
.then(({ nextComponent, isFinished, didError }) => {
|
.then(({ nextComponent, isFinished, didFail }) => {
|
||||||
if (!didError && nextComponent) {
|
if (!didFail && nextComponent) {
|
||||||
setComponents(current => {
|
setComponents(current => {
|
||||||
const updatedComponents = [...current];
|
const updatedComponents = [...current];
|
||||||
updatedComponents[indexToLoad] = nextComponent;
|
updatedComponents[indexToLoad] = nextComponent;
|
||||||
|
|||||||
35
src/photo/MorePhotosLarge.tsx
Normal file
35
src/photo/MorePhotosLarge.tsx
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import MoreComponents from '@/components/MoreComponents';
|
||||||
|
import PhotosLarge from './PhotosLarge';
|
||||||
|
import { getPhotosCached } from '@/cache';
|
||||||
|
|
||||||
|
export function MorePhotosLarge({
|
||||||
|
initialOffset,
|
||||||
|
itemsPerRequest,
|
||||||
|
totalPhotosCount,
|
||||||
|
}: {
|
||||||
|
initialOffset: number
|
||||||
|
itemsPerRequest: number
|
||||||
|
totalPhotosCount: number
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<MoreComponents
|
||||||
|
label="More photos"
|
||||||
|
initialOffset={initialOffset}
|
||||||
|
itemsPerRequest={itemsPerRequest}
|
||||||
|
getNextComponent={async (offset, limit) => {
|
||||||
|
'use server';
|
||||||
|
const photos = await getPhotosCached({ limit: offset + limit })
|
||||||
|
.catch(() => undefined);
|
||||||
|
if (!photos) {
|
||||||
|
return { didFail: true };
|
||||||
|
} else {
|
||||||
|
const nextPhotos = photos.slice(offset);
|
||||||
|
return {
|
||||||
|
nextComponent: <PhotosLarge photos={nextPhotos} />,
|
||||||
|
isFinished: offset + limit >= totalPhotosCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user