From 2d7c265081086257bba6954e9207e92dc4e988d7 Mon Sep 17 00:00:00 2001 From: brian-reeder <14955584+brian-reeder@users.noreply.github.com> Date: Wed, 28 Dec 2022 10:39:58 -0600 Subject: [PATCH] Add new page for Field Expansion --- pages/fields.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pages/fields.js diff --git a/pages/fields.js b/pages/fields.js new file mode 100644 index 0000000..0d0dd5b --- /dev/null +++ b/pages/fields.js @@ -0,0 +1,70 @@ +import Head from 'next/head'; + +import { useRouter } from 'next/router'; +import { useState, useCallback, useEffect } from 'react'; + +import { encode, decode } from '../lib/b64url.js'; + +export default function Home() { + const [fields, setFields] = useState('NULL'); + + const router = useRouter(); + + useEffect( ()=> { + if(!router.isReady) return; + + const handleHashChange = () => { + const hash = getHash(); + console.log(hash); + + var contents = 'New Value'; + + if(hash === undefined) { + contents = 'Undefined'; + } + else { + setFields(JSON.stringify(hash, '\n', 2)); + return; + } + + if(hash.version >= 1.0) { + const newTitle = hash.title !== undefined ? hash.title : 'CyberMARS'; + const newArtifacts = hash.artifacts !== undefined ? hash.artifacts : []; + const newTemplates = hash.templates !== undefined ? hash.templates : []; + + setTitle(newTitle); + setArtifacts([...newArtifacts]); + setTemplates([...newTemplates]); + } + + setFields(contents); + return; + }; + + window.addEventListener('hashchange', handleHashChange); + window.addEventListener('load', handleHashChange); + //router.events.on('hashChangeComplete', handleHashChange); + handleHashChange(); + }, [router.isReady]); + + function getHash() { + console.log('Test') + const path = window.location.hash.split('/'); + const appState = path.length > 1 ? decode(path[1]) : { + 'artifacts': [] + }; + return appState; + } + + return ( + <> +