Skip to content

Commit

Permalink
feat: speed feature ⚡
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-maulana-tkp committed Feb 6, 2021
1 parent 33db9b9 commit c865772
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 2 deletions.
4 changes: 2 additions & 2 deletions components/FeaturedPost/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export default function HeroPost ({ title, coverImage, date, excerpt, author, sl
return (
<section>
<div className="mb-8 relative">
<div className="absolute bg-red-400 z-10 text-sm px-3 py-1 mt-2 ml-2 rounded-full">FEATURED</div>
<div className="absolute bg-red-400 bottom-0 z-10 text-xs px-2 py-1 mb-4 ml-2 rounded-full">FEATURED</div>
<Image
src={coverImage}
alt={`Cover ${title}`}
width={1240}
height={500}
height={600}
layout='intrinsic'
sizes="100vw"
priority={true}
Expand Down
3 changes: 3 additions & 0 deletions components/Header/Default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default function Header () {
<Link href="/talks">
<a className="text-link mr-2 md:mr-4 font-bold">{'Talks'}</a>
</Link>
<Link href="/speed">
<a className="text-link mr-2 md:mr-4 font-bold">{'Speed'}</a>
</Link>
<Link href="/support">
<a className="text-link mr-2 md:mr-4 font-bold">{'Support'}</a>
</Link>
Expand Down
3 changes: 3 additions & 0 deletions components/Header/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export default function Intro () {
<Link href="/talks">
<a className="text-link font-bold mr-2 md:mr-4">{'Talks'}</a>
</Link>
<Link href="/speed">
<a className="text-link mr-2 md:mr-4 font-bold">{'Speed'}</a>
</Link>
<Link href="/support">
<a className="text-link font-bold mr-2 md:mr-4">{'Support'}</a>
</Link>
Expand Down
15 changes: 15 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ function getRelatedPost (tag, slug, lang = 'id') {
return related.slice(0, RELATED_POST_COUNT)
}

function getPsiReportData () {
const reportDir = join(process.cwd(), 'psi-reports')
const files = fs.readdirSync(reportDir).filter((file) => file !== 'LAST_UPDATED.txt').reverse()
const allData = []

files.forEach(file => {
const fileContent = fs.readFileSync(join(reportDir, file), 'utf8')
const jsonData = JSON.parse(fileContent)
allData.push(jsonData)
})

return allData
}

exports.getPostSlugs = getPostSlugs
exports.getPostSlugsEn = getPostSlugsEn
exports.getPostBySlug = getPostBySlug
Expand All @@ -179,3 +193,4 @@ exports.getFeaturedPost = getFeaturedPost
exports.getAvailablePage = getAvailablePage
exports.getPostsByTag = getPostsByTag
exports.getRelatedPost = getRelatedPost
exports.getPsiReportData = getPsiReportData
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@mdx-js/loader": "1.6.21",
"@next/mdx": "10.0.3",
"byte-size": "^7.0.0",
"classnames": "2.2.6",
"date-fns": "2.16.1",
"gray-matter": "4.0.2",
Expand Down
214 changes: 214 additions & 0 deletions pages/speed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { Fragment, useState } from 'react'
import byteSize from 'byte-size'

import Meta from '@/components/Meta/Custom'
import LayoutArticle from '@/components/Layout/Default'
import { SITE_METADATA } from '@/lib/constants'
import { getPsiReportData } from '@/lib/api'

function getPerfColorClass (value) {
if (value <= 0.49) {
return 'text-red-600'
}
if (value <= 0.89) {
return 'text-orange-500'
}

return 'text-green-500'
}

function getWebVitalColorClass (value, good, avg) {
if (value <= good) {
return 'text-green-500'
}

if (value <= avg) {
return 'text-orange-500'
}

return 'text-red-600'
}

function getLCPColorClass (value) {
return getWebVitalColorClass(value, 2500, 4000)
}

function getFIDColorClass (value) {
return getWebVitalColorClass(value, 100, 300)
}

function getCLSColorClass (value) {
return getWebVitalColorClass(value, 0.1, 0.25)
}

export default function Speed ({ data }) {
const [expanded, setExpanded] = useState('')
const newestTimestamp = data[0].timestamp

const isNewestTimestamp = (timestamp) => timestamp === newestTimestamp

const getExpandedClass = (timestamp) => {
if (!isNewestTimestamp(timestamp)) {
return timestamp === expanded ? 'show' : 'hidden'
}
return ''
}

const handleExpand = (timestamp) => {
if (!isNewestTimestamp(timestamp)) {
if (expanded === timestamp) {
setExpanded('')
} else {
setExpanded(timestamp)
}
}
}

return (
<>
<LayoutArticle>
<Fragment>
<Meta
title="Speed | mazipan.space"
description="Speed | mazipan.space"
url={`${SITE_METADATA.url}/speed`}
/>
<h2 className="mb-8 text-6xl md:text-7xl font-bold tracking-tighter leading-tight">
Speed ⚡️
</h2>
<div className="content">

<div className="mb-4">
Data generated by{' '}
<a
href="https://github.com/mazipan/psi-gh-action"
target="_blank"
rel="noreferrer"
alt="psi-gh-action"
className="border-dashed border-red-500 border-b-2 text-red-500"
>
psi-gh-action
</a>
</div>
{data.map((d) => (
<div
className={`px-6 py-4 mb-2 rounded-lg overflow-hidden shadow-lg ${isNewestTimestamp(d.timestamp) ? '' : 'dark:bg-gray-800 cursor-pointer'}`}
key={d.timestamp}
onClick={() => {
handleExpand(d.timestamp)
}}
>
<h3 className="mb-4 text-2xl md:text-3xl font-bold tracking-tighter leading-tight">
🗓 {d.timestamp.substring(0, 10)}
</h3>

<div className={`expander ${getExpandedClass(d.timestamp)}`}>
{d.reports &&
d.reports.map((report) => (
<div key={`${report.url}${report.device}`}>
<h4 className="mb-4 text-lg md:text-xl font-bold capitalize">
{report.device === 'desktop' ? '💻' : '📱'} {report.device}
</h4>
<div className="mb-4 grid grid-cols-2 md:grid-cols-4">
<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>PERF</small>
<div
className={`text-4xl md:text-6xl font-bold ${getPerfColorClass(
report.perf
)}`}
>
{report.perf * 100}
</div>
</div>

<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>FID</small>
<div
className={`text-4xl md:text-6xl font-bold ${getFIDColorClass(
report.fid
)}`}
>
{report.fid}
</div>
</div>

<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>LCP</small>
<div
className={`text-4xl md:text-6xl font-bold ${getLCPColorClass(
report.lcp
)}`}
>
{report.lcp}
</div>
</div>

<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>CLS</small>
<div
className={`text-4xl md:text-6xl font-bold ${getCLSColorClass(
report.cls
)}`}
>
{report.cls}
</div>
</div>

<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>FCP</small>
<div
className={'text-4xl md:text-6xl font-bold'}
>
{report.fcp}
</div>
</div>
<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>TTI</small>
<div
className={'text-4xl md:text-6xl font-bold'}
>
{report.tti}
</div>
</div>

<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>Total Request</small>
<div
className={'text-4xl md:text-6xl font-bold'}
>
{report.req}
</div>
</div>

<div className="mb-2 mr-2 px-6 py-4 rounded text-center">
<small>Total Size</small>
<div
>
<span className={'text-4xl md:text-6xl font-bold'}>{byteSize(report.size).value}</span>
<small className={'text-lg md:text-xl font-bold'}>{byteSize(report.size).unit}</small>
</div>
</div>

</div>
</div>
))}
</div>
</div>
))}

</div>
</Fragment>
</LayoutArticle>
</>
)
}

export async function getStaticProps () {
const data = getPsiReportData()

return {
props: {
data
}
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,11 @@ builtin-status-codes@^3.0.0:
resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8"
integrity sha1-hZgoeOIbmOHGZCXgPQF0eI9Wnug=

byte-size@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/byte-size/-/byte-size-7.0.0.tgz#36528cd1ca87d39bd9abd51f5715dc93b6ceb032"
integrity sha512-NNiBxKgxybMBtWdmvx7ZITJi4ZG+CYUgwOSZTfqB1qogkRHrhbQE/R2r5Fh94X+InN5MCYz6SvB/ejHMj/HbsQ==

bytes@3.1.0, bytes@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
Expand Down

1 comment on commit c865772

@vercel
Copy link

@vercel vercel bot commented on c865772 Feb 6, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.