-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamic page title and desc. (#152)
- Loading branch information
Showing
6 changed files
with
78 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
import { useGlobals } from '../globals.js' | ||
import { usePredictions } from '../contexts/predictions' | ||
|
||
const useDocMeta = () => { | ||
const metaDesc = useRef<Element>() | ||
const { agency, stop } = useGlobals() | ||
const { predictions } = usePredictions() | ||
|
||
useEffect(() => { | ||
const meta = document.querySelector('meta[name="description"]') | ||
|
||
if (meta) { | ||
metaDesc.current = meta | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (metaDesc.current) { | ||
if (agency && stop) { | ||
let event = 'arrival' | ||
let title = `${stop.title}` | ||
|
||
if (predictions.length) { | ||
const [one, two] = predictions[0].values | ||
|
||
if (one.isDeparture) { | ||
event = 'departure' | ||
} | ||
|
||
title += `: ${one.minutes || 'Now'}` | ||
|
||
if (two) { | ||
title += ` & ${two.minutes || 'Now'}` | ||
} | ||
|
||
title += ' min' | ||
} | ||
|
||
document.title = title | ||
metaDesc.current.setAttribute( | ||
'content', | ||
`Maps providing real-time ${event} times of vehicles for ${agency.title} stop ${stop.title}.` | ||
) | ||
} else { | ||
document.title = | ||
'Busmap - Stop and vehicle locations of popular transit agencies.' | ||
metaDesc.current.setAttribute( | ||
'content', | ||
'Maps providing real-time arrival and departure times of vehicles for bus stops along routes in San Francisco Muni CIS, Toronto Transit Commission, OmniTrans and other transit agencies across North and South America.' | ||
) | ||
} | ||
} | ||
}, [agency, stop, predictions]) | ||
} | ||
|
||
export { useDocMeta } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters