generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dynamic metadata for search results (#842)
<!-- 👋 Hi, thanks for sending a PR to tidelift-me-up-site! 💖. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #67 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/tidelift-me-up-site/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/tidelift-me-up-site/blob/main/.github/CONTRIBUTING.md) were taken ## Overview <!-- Description of what is changed and how the code change does that. --> This PR introduces dynamic metadata generation for the home/search page. It's metadata is now dynamically generated based on the `searchParams` in the URL: > {username} | Tidelift Me Up > {username} has {packageCount} npm packages eligible for Tidelift funding. 💸 This includes when a valid user has 0 packages eligible for funding based on search parameters. The metadata will look like the following: > {username} | Tidelift Me Up > {username} has 0 npm packages eligible for Tidelift funding. 💸 When no `searchParams` are provided, the default metadata is used, which is defined in `layout.tsx`: > Tidelift Me Up > Find your npm packages eligible for Tidelift funding. 💸 Also, something to note is that the API-related logic that was in `page.tsx` has been extracted into a separate file to improve code readability and maintainability.
- Loading branch information
1 parent
774563e
commit 1dc9e6e
Showing
4 changed files
with
68 additions
and
29 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,19 @@ | ||
import { cache } from "react"; | ||
import { EstimatedPackage, tideliftMeUp } from "tidelift-me-up"; | ||
|
||
export type DataOptions = Record<string, unknown>; | ||
export type DataResults = Error | EstimatedPackage[] | undefined; | ||
|
||
export const fetchData = cache( | ||
async (options: DataOptions): Promise<DataResults> => { | ||
let result: DataResults; | ||
|
||
try { | ||
result = options.username ? await tideliftMeUp(options) : undefined; | ||
} catch (error) { | ||
result = error as Error; | ||
} | ||
|
||
return result; | ||
}, | ||
); |
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 { PackageOwnership } from "tidelift-me-up"; | ||
|
||
export type SearchParams = Record<string, unknown>; | ||
|
||
export function getOptions(searchParams: SearchParams) { | ||
return { | ||
ownership: undefinedIfEmpty( | ||
[ | ||
searchParams.author === "on" && "author", | ||
searchParams.maintainer === "on" && "maintainer", | ||
searchParams.publisher === "on" && "publisher", | ||
].filter(Boolean) as PackageOwnership[], | ||
), | ||
since: (searchParams.since || undefined) as string | undefined, | ||
username: (searchParams.username || "") as string, | ||
}; | ||
} | ||
|
||
function undefinedIfEmpty<T>(items: T[]) { | ||
return items.length === 0 ? undefined : items; | ||
} |