import { NextPage , GetStaticPropsResult } from 'next' ; import Head from 'next/head' ; import React from 'react' ; import { Item } from '../components/widgets/VerticalList' ; import HomePage , { HomeProps } from '../components/pages/home/HomePage' ; import axios from 'axios' ; import cheerio from 'cheerio' ; const Page = ( props ) => { return ( < > < Head > < title > Hackage : The Haskell community ’ s package registry < /title> < meta name = "description" content = "Hackage is a package registry for Haskell" > < /meta> < /Head> < HomePage { ... props } /> < />); } ; export async function getStaticProps ( ) { let pkgs = [ ] ; try { pkgs = await ( await axios ( "https://hackage.haskell.org/packages/" , { headers : { "Content-Type" : "application/json" } , } ) ) . data ; } catch ( err ) { console . log ( err ) ; } const packageListsSize = 10 ; let topPackages = [ ] ; try { topPackages = await fetchTopPackages ( packageListsSize ) ; } catch ( err ) { console . log ( err ) ; } let recentlyUpdatedPackages = [ ] ; try { recentlyUpdatedPackages = await fetchRecentlyUpdatedPackages ( packageListsSize ) ; } catch ( err ) { console . log ( err ) ; } return { props : { packages : { recentlyUpdated : recentlyUpdatedPackages , top : topPackages , totalCount : pkgs . length } , editorsPick : [ { title : '💪 Became a Haskell Foundation volunteer!' , href : 'https://github.com/haskellfoundation/volunteering/issues/new?assignees=&labels=Volunteer+Available&template=volunteer-available.yml' , description : 'Find a Haskell project opportunity for you!' , } , { title : '🛠 Announcement for the Compiler Tooling Task Force' , href : 'https://discourse.haskell.org/t/announcement-for-the-compiler-tooling-task-force/3893' } , { title : '🔐 A new future for cryptography in Haskell' , href : 'https://discourse.haskell.org/t/a-new-future-for-cryptography-in-haskell/3888/28' } , { title : '📃 State of the Haskell ecosystem' , href : 'https://github.com/Gabriel439/post-rfc/blob/main/sotu.md' , description : 'by Gabriella Gonzalez' , } , { title : 'Volunteer Available. Frontend, DevOps. ' , href : 'https://github.com/haskellfoundation/volunteering/issues/3' , description : 'The author of this project is looking for a Haskell project to join.' , } ] , community : { latest : [ ] , hot : [ ] , jobs : [ ] } , packageListsSize } , revalidate : 30 } ; } async function fetchTopPackages ( count ) { let html = '' ; try { html = await ( await axios . get ( 'https://hackage.haskell.org/packages/top' ) ) . data ; } catch ( err ) { console . log ( err ) ; } const $ = cheerio . load ( html ) ; let tds ; try { tds = $ ( '#content td' ) . slice ( 0 , count * 2 ) . filter ( i => i % 2 === 0 ) ; } catch ( err ) { console . log ( err ) ; } const items = tds ? tds . map ( ( _ , td ) => { const name = $ ( 'a' , td ) . first ( ) . text ( ) ; return { title : name } ; } ) . toArray ( ) : [ ] ; return items ; } async function fetchRecentlyUpdatedPackages ( count ) { let html = '' ; try { html = await ( await axios . get ( 'https://hackage.haskell.org/packages/recent' ) ) . data ; } catch ( err ) { console . log ( err ) ; } const $ = cheerio . load ( html ) ; let tds ; try { tds = $ ( '#content td' ) . slice ( 0 , count * 3 ) . filter ( ( i ) => ( i + 1 ) % 3 === 0 ) ; } catch ( err ) { console . log ( err ) ; } const items = tds ? tds . map ( ( _ , td ) => { const nameAndVersion = $ ( 'a' , td ) . html ( ) as string ; const version = nameAndVersion . replace ( /.*-(?=.+$)/ , '' ) ; const name = nameAndVersion . slice ( 0 , nameAndVersion . length - version . length - 1 ) ; return { title : name , description : version } ; } ) . toArray ( ) : [ ] ; return items ; } export default Page ;