-
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: add extra button to navbar (#202)
- Loading branch information
Showing
9 changed files
with
130 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
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,54 @@ | ||
import { config } from "../../config" | ||
|
||
type NavbarExtraButton = { | ||
text: string | ||
link: string | ||
visible: boolean | ||
textColor?: `#${string}` | ||
backgroundColor?: `#${string}` | ||
id?: string | ||
ttl: number | ||
} | ||
|
||
type LocalStorageNavbarExtraButton = { | ||
button: NavbarExtraButton | ||
expiresAt: number | ||
} | ||
|
||
const getExtraButton = async () => { | ||
const cachedExtraButton = localStorage.getItem("navbarExtraButton") | ||
if (cachedExtraButton) { | ||
try { | ||
const parsed = JSON.parse( | ||
cachedExtraButton | ||
) as LocalStorageNavbarExtraButton | ||
if (parsed.expiresAt > Date.now()) { | ||
return parsed.button | ||
} | ||
} catch (error) { | ||
// error parsing cached data, ignore and fetch from Contentful | ||
} | ||
} | ||
try { | ||
const SPACE_ID = config.get("CONTENTFUL_SPACE_ID") | ||
const ENV = config.get("CONTENTFUL_ENV") | ||
const ACCESS_TOKEN = config.get("CONTENTFUL_NAVBAR_ACCESS_TOKEN") | ||
const ENTRY_ID = config.get("CONTENTFUL_NAVBAR_ENTRY_ID") | ||
const CONTENTFUL_CDN_URL = config.get("CONTENTFUL_CONTENTFUL_CDN_URL") | ||
const CONTENTFUL_URL = `${CONTENTFUL_CDN_URL}/spaces/${SPACE_ID}/environments/${ENV}/entries/${ENTRY_ID}?access_token=${ACCESS_TOKEN}` | ||
const response = await fetch(CONTENTFUL_URL) | ||
const entry = await response.json() | ||
const button = entry.fields as NavbarExtraButton | ||
localStorage.setItem( | ||
"navbarExtraButton", | ||
JSON.stringify({ button, expiresAt: Date.now() + button.ttl * 1000 }) | ||
) | ||
return button | ||
} catch (error) { | ||
console.error(error) | ||
return null | ||
} | ||
} | ||
|
||
export { getExtraButton } | ||
export type { NavbarExtraButton, LocalStorageNavbarExtraButton } |
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