Skip to content

Commit bbbeee8

Browse files
authored
Merge pull request #115 from datacamp/bb/package-count
[CT-3468] - bb/package-count
2 parents 9336ce9 + 8669239 commit bbbeee8

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

lib/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ export function copyTextToClipboard(text: string) {
4141
navigator.clipboard.writeText(text);
4242
}
4343

44-
export const API_URL = 'https://api.rdocumentation.org';
44+
export const API_URL = 'https://api.rdocumentation.org';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "with-tailwindcss",
33
"version": "1.0.0",
44
"scripts": {
5-
"dev": "next",
5+
"dev": "next dev -p 3010",
66
"build": "next build",
77
"start": "next start",
88
"lint": "eslint . --ext ts,tsx,js,jsx,json --ignore-path .gitignore",

pages/index.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { GetServerSideProps } from 'next';
12
import { useRouter } from 'next/router';
23
import { useState } from 'react';
34

45
import HomeSearchBar from '../components/HomeSearchBar';
56
import Layout from '../components/Layout';
7+
import { API_URL } from '../lib/utils';
68

7-
export default function HomePage() {
9+
export default function HomePage({ packageCount }: { packageCount?: number }) {
810
const [searchInput, setSearchInput] = useState('');
911
const router = useRouter();
1012

@@ -25,9 +27,9 @@ export default function HomePage() {
2527
title="Home"
2628
>
2729
<div className="w-full max-w-4xl mx-auto mt-32 md:mt-56">
28-
<h1 className="text-xl md:text-2xl lg:text-3xl">
29-
Search all R packages on CRAN and Bioconductor
30-
</h1>
30+
<div className="text-xl md:text-2xl lg:text-3xl">
31+
{`Search all ${packageCount > 0 ? `${packageCount.toLocaleString(undefined, {maximumFractionDigits:0})}`:''} R packages on CRAN and Bioconductor`}
32+
</div>
3133
<form onSubmit={onSubmitSearch}>
3234
<HomeSearchBar
3335
onChange={handleChangeSearchInput}
@@ -38,3 +40,20 @@ export default function HomePage() {
3840
</Layout>
3941
);
4042
}
43+
44+
export const getServerSideProps: GetServerSideProps = async (_context) => {
45+
let packageCount;
46+
try {
47+
const response = await fetch(`${API_URL}/api/packages?limit=${Number.MAX_SAFE_INTEGER}`, {
48+
method: 'HEAD'
49+
});
50+
packageCount = parseInt(response.headers.get('x-total-count'));
51+
} catch (error) {
52+
packageCount = null
53+
}
54+
return {
55+
props: {
56+
packageCount
57+
}
58+
};
59+
};

0 commit comments

Comments
 (0)