Files
shields/frontend/components/main.js
Paul Melnikow d8ce045ead Adopt Gatsby (#2906)
While Next.js can handle static sites, we've had a few issues with it, notably a performance hit at runtime and some bugginess around routing and SSR. Gatsby being fully intended for high-performance static sites makes it a great technical fit for the Shields frontend. The `createPages()` API should be a really nice way to add a page for each service family, for example.

This migrates the frontend from Next.js to Gatsby. Gatsby is a powerful tool, which has a bit of downside as there's a lot to dig through. Overall I found configuration easier than Next.js. There are a lot of plugins and for the most part they worked out of the box. The documentation is good.

Links are cleaner now: there is no #. This will break old links though perhaps we could add some redirection to help with that. The only one I’m really concerned about `/#/endpoint`. I’m not sure if folks are deep-linking to the category pages.

There are a lot of enhancements we could add, in order to speed up the site even more. In particular we could think about inlining the SVGs rather than making separate requests for each one.

While Gatsby recommends GraphQL, it's not required. To keep things simple and reduce the learning curve, I did not use it here.

Close #1943 
Fix #2837 Fix #2616
2019-02-06 16:37:55 -05:00

178 lines
4.7 KiB
JavaScript

import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import groupBy from 'lodash.groupby'
import {
categories,
findCategory,
services,
getDefinitionsForCategory,
} from '../lib/service-definitions'
import ServiceDefinitionSetHelper from '../lib/service-definitions/service-definition-set-helper'
import { baseUrl } from '../constants'
import Meta from './meta'
import Header from './header'
import SuggestionAndSearch from './suggestion-and-search'
import DonateBox from './donate'
import MarkupModal from './markup-modal'
import Usage from './usage'
import Footer from './footer'
import {
CategoryHeading,
CategoryHeadings,
CategoryNav,
} from './category-headings'
import BadgeExamples from './badge-examples'
import { BaseFont, GlobalStyle } from './common'
const AppContainer = styled(BaseFont)`
text-align: center;
`
export default class Main extends React.Component {
constructor(props) {
super(props)
this.state = {
isSearchInProgress: false,
isQueryTooShort: false,
searchResults: undefined,
selectedExample: undefined,
}
this.searchTimeout = 0
this.handleExampleSelected = this.handleExampleSelected.bind(this)
this.dismissMarkupModal = this.dismissMarkupModal.bind(this)
this.searchQueryChanged = this.searchQueryChanged.bind(this)
}
static propTypes = {
// `pageContext` is the `context` passed to `createPage()` in
// `gatsby-node.js`. In the case of the index page, `pageContext` is empty.
pageContext: {
category: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
}),
}.isRequired,
}
performSearch(query) {
const isQueryTooShort = query.length === 1
let searchResults
if (query.length >= 2) {
const flat = ServiceDefinitionSetHelper.create(services)
.notDeprecated()
.search(query)
.toArray()
searchResults = groupBy(flat, 'category')
}
this.setState({
isSearchInProgress: false,
isQueryTooShort,
searchResults,
})
}
searchQueryChanged(query) {
/*
Add a small delay before showing search results
so that we wait until the user has stopped typing
before we start loading stuff.
This
a) reduces the amount of badges we will load and
b) stops the page from 'flashing' as the user types, like this:
https://user-images.githubusercontent.com/7288322/42600206-9b278470-85b5-11e8-9f63-eb4a0c31cb4a.gif
*/
this.setState({ isSearchInProgress: true })
window.clearTimeout(this.searchTimeout)
this.searchTimeout = window.setTimeout(() => this.performSearch(query), 500)
}
handleExampleSelected(example) {
this.setState({ selectedExample: example })
}
dismissMarkupModal() {
this.setState({ selectedExample: undefined })
}
renderCategory(category, definitions) {
const { id } = category
return (
<div key={id}>
<CategoryHeading category={category} />
<BadgeExamples
definitions={definitions}
onClick={this.handleExampleSelected}
baseUrl={baseUrl}
/>
</div>
)
}
renderMain() {
const {
pageContext: { category },
} = this.props
const { isSearchInProgress, isQueryTooShort, searchResults } = this.state
if (isSearchInProgress) {
return <div>searching...</div>
} else if (isQueryTooShort) {
return <div>Search term must have 2 or more characters</div>
} else if (searchResults) {
return Object.entries(searchResults).map(([categoryId, definitions]) =>
this.renderCategory(findCategory(categoryId), definitions)
)
} else if (category) {
const definitions = ServiceDefinitionSetHelper.create(
getDefinitionsForCategory(category.id)
)
.notDeprecated()
.toArray()
return (
<div>
<CategoryNav categories={categories} />
{this.renderCategory(category, definitions)}
</div>
)
} else {
return <CategoryHeadings categories={categories} />
}
}
render() {
const { selectedExample } = this.state
return (
<AppContainer id="app">
<GlobalStyle />
<Meta />
<Header />
<MarkupModal
example={selectedExample}
onRequestClose={this.dismissMarkupModal}
baseUrl={baseUrl}
/>
<section>
<SuggestionAndSearch
queryChanged={this.searchQueryChanged}
onBadgeClick={this.handleExampleSelected}
baseUrl={baseUrl}
/>
<DonateBox />
</section>
{this.renderMain()}
<Usage baseUrl={baseUrl} />
<Footer baseUrl={baseUrl} />
</AppContainer>
)
}
}