From c6e4edc69c6f12408151fb418ed531291a120f14 Mon Sep 17 00:00:00 2001 From: Sesh Sadasivam Date: Wed, 17 Jan 2024 15:52:26 +0000 Subject: [PATCH] [Primer Spec Preview] Add a button to redirect to a diff view --- src_js/components/PreviewDiffButton.tsx | 81 +++++++++++++++++++++++++ src_js/components/PrimerSpec.tsx | 2 + 2 files changed, 83 insertions(+) create mode 100644 src_js/components/PreviewDiffButton.tsx diff --git a/src_js/components/PreviewDiffButton.tsx b/src_js/components/PreviewDiffButton.tsx new file mode 100644 index 0000000..ea4f65c --- /dev/null +++ b/src_js/components/PreviewDiffButton.tsx @@ -0,0 +1,81 @@ +import { Fragment, h } from 'preact'; + +const PRIMER_SPEC_PREVIEW_HOSTNAME = 'preview.sesh.rs'; +const GITHUB_REPO_REGEX_FROM_PREVIEW_URL = + /^https:\/\/preview\.sesh\.rs\/previews\/([A-Za-z0-9_\-]+)\/([A-Za-z0-9_\-]+)\/\d+\/(.*)/; + +const HTML_DIFF_URL = 'https://services.w3.org/htmldiff'; + +export function PreviewDiffButton() { + if (window.location.hostname !== PRIMER_SPEC_PREVIEW_HOSTNAME) { + return null; + } + + const productionUrl = getProductionUrl(); + if (!productionUrl) { + return null; + } + + return ( + + + + + ); +} + +function getProductionUrl() { + // const matches = window.location.href.match( + // GITHUB_REPO_REGEX_FROM_PREVIEW_URL, + // ); + const matches = + 'https://preview.sesh.rs/previews/eecs485staff/p1-insta485-static/492/setup_macos.html'.match( + GITHUB_REPO_REGEX_FROM_PREVIEW_URL, + ); + if (matches && matches.length >= 4) { + const org = matches[1]; + const repo = matches[2]; + const path = matches[3]; + return `https://${org}.github.io/${repo}/${path}`; + } + return null; +} + +function buildDiffUrl(publishedUrl: string) { + const queryParams = new URLSearchParams({ + doc1: publishedUrl, + doc2: window.location.href, + }); + return `${HTML_DIFF_URL}?${queryParams}`; +} diff --git a/src_js/components/PrimerSpec.tsx b/src_js/components/PrimerSpec.tsx index 9ea4943..b9d9113 100644 --- a/src_js/components/PrimerSpec.tsx +++ b/src_js/components/PrimerSpec.tsx @@ -11,6 +11,7 @@ import { useAfterPrint, useBeforePrint } from '../utils/hooks/print'; import useSmallScreen from '../utils/hooks/useSmallScreen'; import Config from '../Config'; import MainContent from './main_content'; +import { PreviewDiffButton } from './PreviewDiffButton'; import Settings from './settings'; import Sidebar from './sidebar'; import Topbar from './Topbar'; @@ -150,6 +151,7 @@ export default function PrimerSpec(props: PropsType): h.JSX.Element { onSubthemeNameChange={(name) => setTheme({ name })} onSubthemeModeChange={(mode) => setTheme({ mode })} /> + ); }