Skip to content

Commit

Permalink
move url parameters context into other hooks
Browse files Browse the repository at this point in the history
Rather than have components use the URL parameters context directly, and
pass the page URL and parameters to hooks, we instead have the hooks
access the TimesSquareUrlParametersContext to get the necessary context
for getting the page URL, etc.
  • Loading branch information
jonathansick committed Apr 2, 2024
1 parent 3313882 commit 79bfe10
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,14 @@ import styled from 'styled-components';
import getConfig from 'next/config';
import Head from 'next/head';
import Error from 'next/error';
import { useRouter } from 'next/router';

import useTimesSquarePage from '../../hooks/useTimesSquarePage';
import TimesSquareParameters from '../TimesSquareParameters';
import TimesSquareUrlParametersContext from '../TimesSquareUrlParametersProvider';

export default function TimesSquareGitHubPagePanel({}) {
const { publicRuntimeConfig } = getConfig();
const router = useRouter();
// const { tsPageUrl } = React.useContext(TimesSquareUrlParametersContext);
const { timesSquareUrl } = publicRuntimeConfig;

// Get components out of the URL path. Only /github-pr/ pages have owner,
// repo, and commit components in the path. In /github/ pages the owner
// and repo are part of tsSlug.
const { tsSlug, owner = null, repo = null, commit = null } = router.query;
console.log('tsSlug: ', tsSlug);

// Since the page's path is a [...tsSlug], we need to join the parts of the
// path to get the full slug. This combines the owner, repo, directory, and
// notebook name for regular /github/ pages, or just the directory and
// notebook name for /github-pr/ pages.
const githubSlug = tsSlug ? tsSlug.join('/') : null;

// Construct the URL for the Times Square API endpoint that gives information
// about the page. GitHub PR pages (github-pr) have different API URLs than
// regular GitHub pages.
const tsPageUrl = router.pathname.startsWith('/times-square/github-pr')
? `${timesSquareUrl}/v1/github-pr/${owner}/${repo}/${commit}/${githubSlug}`
: `${timesSquareUrl}/v1/github/${githubSlug}`;

const pageData = useTimesSquarePage(tsPageUrl);
const pageData = useTimesSquarePage();

if (pageData.loading) {
return <p>Loading...</p>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useGitHubContentsListing from './useGitHubContentsListing';
function TimesSquareMainGitHubNav({ pagePath }) {
const { publicRuntimeConfig } = getConfig();
const { timesSquareUrl } = publicRuntimeConfig;
console.log(timesSquareUrl);
const githubContents = useGitHubContentsListing(timesSquareUrl);

if (githubContents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ const StyledIframe = styled.iframe`
`;

export default function TimesSquareNotebookViewer({}) {
const { tsPageUrl, notebookParameters, displaySettings } = React.useContext(
TimesSquareUrlParametersContext
);
const htmlStatus = useHtmlStatus(
tsPageUrl,
notebookParameters,
displaySettings
);
const { tsPageUrl } = React.useContext(TimesSquareUrlParametersContext);
const htmlStatus = useHtmlStatus();

if (htmlStatus.error) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* dynamic refreshing of data about a page's HTML rendering.
*/

import React from 'react';
import useSWR from 'swr';
import useTimesSquarePage from '../../hooks/useTimesSquarePage';
import { TimesSquareUrlParametersContext } from '../TimesSquareUrlParametersProvider';

const fetcher = (...args) => fetch(...args).then((res) => res.json());

Expand All @@ -20,8 +22,11 @@ export function parameterizeUrl(baseUrl, parameters, displaySettings) {
return url.toString();
}

function useHtmlStatus(pageUrl, parameters, displaySettings) {
const pageData = useTimesSquarePage(pageUrl);
function useHtmlStatus() {
const { notebookParameters: parameters, displaySettings } = React.useContext(
TimesSquareUrlParametersContext
);
const pageData = useTimesSquarePage();

const { data, error } = useSWR(
() => parameterizeUrl(pageData.htmlStatusUrl, parameters, displaySettings),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ function inputFactory(props) {
export default function TimesSquareParameters({}) {
const router = useRouter();

const {
tsPageUrl,
displaySettings,
notebookParameters: userParameters,
} = React.useContext(TimesSquareUrlParametersContext);
const { parameters } = useTimesSquarePage(tsPageUrl);
const { displaySettings, notebookParameters: userParameters } =
React.useContext(TimesSquareUrlParametersContext);
const { parameters } = useTimesSquarePage();

const ajv = new Ajv({ coerceTypes: true });

Expand Down
8 changes: 6 additions & 2 deletions apps/squareone/src/hooks/useTimesSquarePage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React from 'react';
import useSWR from 'swr';

import { TimesSquareUrlParametersContext } from '../components/TimesSquareUrlParametersProvider';

const fetcher = (...args) => fetch(...args).then((res) => res.json());

function useTimesSquarePage(pageUrl) {
const { data, error } = useSWR(pageUrl, fetcher);
function useTimesSquarePage() {
const { tsPageUrl } = React.useContext(TimesSquareUrlParametersContext);
const { data, error } = useSWR(tsPageUrl, fetcher);

return {
error: error,
Expand Down

0 comments on commit 79bfe10

Please sign in to comment.