-
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.
Merge pull request #55 from lsst-sqre/tickets/DM-34723
DM-34723: Port from lsst-sqre/times-square-ui
- Loading branch information
Showing
23 changed files
with
483 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
/* | ||
* The NotebookIframe controls the iframe with HTML content | ||
* from Times Square with a notebook render. | ||
*/ | ||
|
||
import styled from 'styled-components'; | ||
|
||
import useHtmlStatus from './useHtmlStatus'; | ||
|
||
const StyledIframe = styled.iframe` | ||
--shadow-color: 0deg 0% 74%; | ||
--shadow-elevation-medium: 0.1px 0.7px 0.9px hsl(var(--shadow-color) / 0.16), | ||
0.4px 2.4px 3px -0.6px hsl(var(--shadow-color) / 0.2), | ||
0.8px 5.3px 6.7px -1.1px hsl(var(--shadow-color) / 0.24), | ||
1.9px 11.9px 15px -1.7px hsl(var(--shadow-color) / 0.28); | ||
border: 0px solid black; | ||
box-shadow: var(--shadow-elevation-medium); | ||
width: 100%; | ||
height: 100%; | ||
`; | ||
|
||
export default function NotebookIframe({ | ||
tsHtmlUrl, | ||
tsHtmlStatusUrl, | ||
parameters, | ||
}) { | ||
const htmlUrl = new URL(tsHtmlUrl); | ||
parameters.forEach((item) => htmlUrl.searchParams.set(item[0], item[1])); | ||
|
||
const htmlStatus = useHtmlStatus(tsHtmlStatusUrl, parameters); | ||
|
||
if (htmlStatus.error) { | ||
return ( | ||
<div> | ||
<p>Error contacting API at {`${tsHtmlStatusUrl}`}</p> | ||
</div> | ||
); | ||
} | ||
|
||
if (htmlStatus.loading) { | ||
return ( | ||
<div> | ||
<p>Loading...</p> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<StyledIframe | ||
src={htmlStatus.htmlUrl || htmlUrl.toString()} | ||
key={htmlStatus.iframeKey} | ||
></StyledIframe> | ||
); | ||
} |
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,2 @@ | ||
export * from './TimesSquareViewer'; | ||
export { default } from './TimesSquareViewer'; |
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,32 @@ | ||
/* | ||
* useHtmlStatus hook fetches data from the Times Square | ||
* /v1/pages/:page/htmlstatus endpoint using the SWR hook to enable | ||
* dynamic refreshing of data about a page's HTML rendering. | ||
*/ | ||
|
||
import useSWR from 'swr'; | ||
|
||
const fetcher = (...args) => fetch(...args).then((res) => res.json()); | ||
|
||
function useHtmlStatus(htmlStatusUrl, parameters) { | ||
const url = new URL(htmlStatusUrl); | ||
parameters.forEach((item) => url.searchParams.set(item[0], item[1])); | ||
const fullHtmlStatusUrl = url.toString(); | ||
|
||
const { data, error } = useSWR(fullHtmlStatusUrl, fetcher, { | ||
// ping every 1 second while browser in focus. | ||
// TODO back this off once HTML is loaded? | ||
refreshInterval: 1000, | ||
}); | ||
|
||
return { | ||
error: error, | ||
loading: !error && !data, | ||
htmlAvailable: data ? data.available : false, | ||
htmlHash: data ? data.html_hash : null, | ||
htmlUrl: data ? data.html_url : null, | ||
iframeKey: data && data.available ? data.html_hash : 'html-not-available', | ||
}; | ||
} | ||
|
||
export default useHtmlStatus; |
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,27 @@ | ||
/* | ||
* Mock Times Square API endpoint: /times-square/v1/pages | ||
* | ||
* This endpoint lists available pages. | ||
*/ | ||
|
||
import getConfig from 'next/config'; | ||
|
||
export default function handler(req, res) { | ||
const { publicRuntimeConfig } = getConfig(); | ||
const { timesSquareUrl } = publicRuntimeConfig; | ||
|
||
const createPage = (name) => { | ||
const pageBaseUrl = `${timesSquareUrl}/v1/pages/${name}`; | ||
return { | ||
name, | ||
title: name, | ||
self_url: pageBaseUrl, | ||
}; | ||
}; | ||
|
||
const content = [createPage('mypage'), createPage('anotherpage')]; | ||
|
||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(content)); | ||
} |
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,45 @@ | ||
/* | ||
* Mock Times Square API endpoint: /times-square/v1/pages/:page | ||
*/ | ||
import getConfig from 'next/config'; | ||
|
||
export default function handler(req, res) { | ||
const { page } = req.query; | ||
const { publicRuntimeConfig } = getConfig(); | ||
const { timesSquareUrl } = publicRuntimeConfig; | ||
const pageBaseUrl = `${timesSquareUrl}/v1/pages/${page}`; | ||
|
||
if (page == 'not-found') { | ||
// simulate a page that doesn't exist in the backend | ||
res.statusCode = 404; | ||
res.end(); | ||
return; | ||
} | ||
|
||
const content = { | ||
name: page, | ||
title: `Title for ${page}`, | ||
description: '<p>This is the description.</p>', | ||
self_url: pageBaseUrl, | ||
source_url: `${pageBaseUrl}/source`, | ||
rendered_url: `${pageBaseUrl}/rendered`, | ||
html_url: `${pageBaseUrl}/html`, | ||
html_status_url: `${pageBaseUrl}/htmlstatus`, | ||
parameters: { | ||
a: { | ||
type: 'number', | ||
default: 42, | ||
description: 'A number.', | ||
}, | ||
b: { | ||
type: 'string', | ||
default: 'Hello', | ||
description: 'A string.', | ||
}, | ||
}, | ||
}; | ||
|
||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(content)); | ||
} |
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,28 @@ | ||
/* | ||
* Mock Times Square API endpoint: /times-square/v1/pages/[page]/html | ||
*/ | ||
|
||
const htmlContent = ` | ||
<!doctype html> | ||
<html class="no-js" lang=""> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Test document</title> | ||
</head> | ||
<body> | ||
<h1>Test content</h1> | ||
<p>Hello world</p> | ||
</body> | ||
</html> | ||
`; | ||
|
||
export default function handler(req, res) { | ||
const { page } = req.query; | ||
console.log(req.url); | ||
|
||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.end(htmlContent); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/pages/api/dev/times-square/v1/pages/[page]/htmlstatus.js
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,26 @@ | ||
/* | ||
* Mock Times Square API endpoint: /times-square/api/v1/pages/:page/htmlstatus | ||
*/ | ||
import getConfig from 'next/config'; | ||
|
||
export default function handler(req, res) { | ||
const { page, a } = req.query; | ||
const { publicRuntimeConfig } = getConfig(); | ||
const { timesSquareUrl } = publicRuntimeConfig; | ||
|
||
const pageBaseUrl = `${timesSquareUrl}/v1/pages/${page}`; | ||
|
||
const content = { | ||
available: a != '2', // magic value to toggle status modes | ||
html_url: `${pageBaseUrl}/html?a={a}`, | ||
html_hash: a != '2' ? '12345' : null, | ||
}; | ||
|
||
console.log(content); | ||
|
||
console.log('Pinged status'); | ||
|
||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(content)); | ||
} |
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
Oops, something went wrong.