-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Colocate data fetching; use proper server components
Performing all image search requests upfront, before rendering any image components, wasn't really in line with how server components should work. This approach was initially chosen to obtain a single `function` AI state that contains data from all searches. With the introduction of the `onDataFetched` prop, we can achieve the same outcome while moving data fetching directly into the server components, where it belongs. This change also makes it easier to add loading skeletons later on.
- Loading branch information
1 parent
7958caf
commit 41eb830
Showing
4 changed files
with
112 additions
and
64 deletions.
There are no files selected for viewing
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
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,65 @@ | ||
import * as React from 'react'; | ||
import {type ImageSearchParams, searchImages} from './google-image-search.js'; | ||
import { | ||
createImageSearchResult, | ||
prepareImageSearchResultForAiState, | ||
} from './image-search-result.js'; | ||
import {ImageSelector} from './image-selector.js'; | ||
import {ProgressiveImage} from './progressive-image.js'; | ||
|
||
export interface ImagesProps { | ||
readonly title: string; | ||
readonly notFoundMessage: string; | ||
readonly errorMessage: string; | ||
readonly searchParams: ImageSearchParams; | ||
readonly onDataFetched: (dataForAiState: unknown) => void; | ||
} | ||
|
||
export async function Images({ | ||
title, | ||
notFoundMessage, | ||
errorMessage, | ||
searchParams, | ||
onDataFetched, | ||
}: ImagesProps): Promise<React.ReactElement> { | ||
const response = await searchImages(searchParams); | ||
|
||
const result = createImageSearchResult({ | ||
response, | ||
title, | ||
notFoundMessage, | ||
errorMessage, | ||
}); | ||
|
||
onDataFetched(prepareImageSearchResultForAiState(result)); | ||
|
||
if (result.status !== `found`) { | ||
const message = | ||
result.status === `not-found` | ||
? result.notFoundMessage | ||
: result.errorMessage; | ||
|
||
return ( | ||
<p className="text-sm"> | ||
<em>{message}</em> | ||
</p> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="space-y-3"> | ||
<h4 className="text-l font-bold">{result.title}</h4> | ||
{result.images.map(({thumbnailUrl, url, width, height}) => ( | ||
<ImageSelector key={thumbnailUrl} url={url}> | ||
<ProgressiveImage | ||
thumbnailUrl={thumbnailUrl} | ||
url={url} | ||
width={width} | ||
height={height} | ||
alt={result.title} | ||
/> | ||
</ImageSelector> | ||
))} | ||
</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