From a839f1ae1b1d7035929e2d448d3021a76af6ecd8 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 8 Dec 2018 20:27:45 +0100 Subject: [PATCH] hostname check --- package.json | 1 + src/components/atoms/HostnameCheck.jsx | 41 +++++++++++++++ .../atoms/HostnameCheck.module.scss | 15 ++++++ src/components/atoms/Vcard.jsx | 52 ++++++++++--------- src/components/organisms/Header.jsx | 20 ++++--- 5 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 src/components/atoms/HostnameCheck.jsx create mode 100644 src/components/atoms/HostnameCheck.module.scss diff --git a/package.json b/package.json index 498299d..78231ba 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "Matthias Kretschmann ", "scripts": { "start": "./node_modules/gatsby/dist/bin/gatsby.js develop", + "ssr": "npm run build && serve -s public/", "lint:js": "eslint ./gatsby-*.js && eslint ./src/**/*.{js,jsx}", "lint:css": "stylelint ./src/**/*.{css,scss}", "lint": "npm run lint:js && npm run lint:css", diff --git a/src/components/atoms/HostnameCheck.jsx b/src/components/atoms/HostnameCheck.jsx new file mode 100644 index 0000000..9fec9f8 --- /dev/null +++ b/src/components/atoms/HostnameCheck.jsx @@ -0,0 +1,41 @@ +import React, { PureComponent } from 'react' +import styles from './HostnameCheck.module.scss' + +const allowedHosts = [ + 'matthiaskretschmann.com', + 'beta.matthiaskretschmann.com', + 'localhost' +] + +export default class HostnameInfo extends PureComponent { + checkAllowedHost = () => { + if (typeof window !== 'undefined' && window.location) { + return allowedHosts.includes(window.location.hostname) + } + } + + state = { + isAllowedHost: true + } + + componentDidMount() { + const isAllowedHost = this.checkAllowedHost() + this.setState({ isAllowedHost }) + } + + render() { + if (this.state.isAllowedHost) return null + + return ( + <> + + + ) + } +} diff --git a/src/components/atoms/HostnameCheck.module.scss b/src/components/atoms/HostnameCheck.module.scss new file mode 100644 index 0000000..699e68c --- /dev/null +++ b/src/components/atoms/HostnameCheck.module.scss @@ -0,0 +1,15 @@ +@import 'variables'; + +.hostnameInfo { + position: sticky; + top: 0; + z-index: 99; + padding: $spacer / 2 $spacer; + font-size: $font-size-small; + font-weight: bold; + background: rgba($brand-light, .8); + + :global(.dark) & { + background: rgba($body-background-color--dark, .8); + } +} diff --git a/src/components/atoms/Vcard.jsx b/src/components/atoms/Vcard.jsx index 1ce4a5d..c79e117 100644 --- a/src/components/atoms/Vcard.jsx +++ b/src/components/atoms/Vcard.jsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { PureComponent } from 'react' import { StaticQuery, graphql } from 'gatsby' import saveAs from 'file-saver' import vCard from 'vcf' @@ -31,30 +31,34 @@ const query = graphql` } ` -const Vcard = () => ( - { - const meta = data.dataYaml +export default class Vcard extends PureComponent { + render() { + return ( + { + const meta = data.dataYaml - const handleAddressbookClick = e => { - e.preventDefault() - constructVcard(meta) - } + const handleAddressbookClick = e => { + e.preventDefault() + constructVcard(meta) + } - return ( - - Add to addressbook - - ) - }} - /> -) + return ( + + Add to addressbook + + ) + }} + /> + ) + } +} // Construct the download from a blob of the just constructed vCard, // and save it to user's file system @@ -120,5 +124,3 @@ const toDataURL = (src, callback, outputFormat) => { img.src = src } } - -export default Vcard diff --git a/src/components/organisms/Header.jsx b/src/components/organisms/Header.jsx index fce3e84..c795a0e 100644 --- a/src/components/organisms/Header.jsx +++ b/src/components/organisms/Header.jsx @@ -5,6 +5,7 @@ import Networks from '../molecules/Networks' import Availability from '../molecules/Availability' import ThemeSwitch from '../molecules/ThemeSwitch' import LogoUnit from '../molecules/LogoUnit' +import HostnameCheck from '../atoms/HostnameCheck' import styles from './Header.module.scss' const query = graphql` @@ -32,17 +33,20 @@ export default class Header extends PureComponent { const meta = data.dataYaml return ( -
- + <> + +
+ - - - + + + - + - -
+ +
+ ) }} />