Skip to content

[CT-3468] - bb/package-count #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ export function copyTextToClipboard(text: string) {
navigator.clipboard.writeText(text);
}

export const API_URL = 'https://api.rdocumentation.org';
export const API_URL = 'https://api.rdocumentation.org';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "with-tailwindcss",
"version": "1.0.0",
"scripts": {
"dev": "next",
"dev": "next dev -p 3010",
"build": "next build",
"start": "next start",
"lint": "eslint . --ext ts,tsx,js,jsx,json --ignore-path .gitignore",
Expand Down
27 changes: 23 additions & 4 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { GetServerSideProps } from 'next';
import { useRouter } from 'next/router';
import { useState } from 'react';

import HomeSearchBar from '../components/HomeSearchBar';
import Layout from '../components/Layout';
import { API_URL } from '../lib/utils';

export default function HomePage() {
export default function HomePage({ packageCount }: { packageCount?: number }) {
const [searchInput, setSearchInput] = useState('');
const router = useRouter();

Expand All @@ -25,9 +27,9 @@ export default function HomePage() {
title="Home"
>
<div className="w-full max-w-4xl mx-auto mt-32 md:mt-56">
<h1 className="text-xl md:text-2xl lg:text-3xl">
Search all R packages on CRAN and Bioconductor
</h1>
<div className="text-xl md:text-2xl lg:text-3xl">
{`Search all ${packageCount > 0 ? `${packageCount.toLocaleString(undefined, {maximumFractionDigits:0})}`:''} R packages on CRAN and Bioconductor`}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to turn this into a utility function that can be reused 👌

</div>
<form onSubmit={onSubmitSearch}>
<HomeSearchBar
onChange={handleChangeSearchInput}
Expand All @@ -38,3 +40,20 @@ export default function HomePage() {
</Layout>
);
}

export const getServerSideProps: GetServerSideProps = async (_context) => {
let packageCount;
try {
const response = await fetch(`${API_URL}/api/packages?limit=${Number.MAX_SAFE_INTEGER}`, {
method: 'HEAD'
});
packageCount = parseInt(response.headers.get('x-total-count'));
} catch (error) {
packageCount = null
}
return {
props: {
packageCount
}
};
};