From 94290df09e7f4644ed8cbecddbc028a191d072bd Mon Sep 17 00:00:00 2001 From: Frank Calise Date: Wed, 10 Apr 2024 23:42:54 -0400 Subject: [PATCH] feat: add package and name to URL params --- src/components/common/VersionSelector.tsx | 6 ++++++ src/components/pages/Home.tsx | 21 +++++++++++++++++++-- src/utils/update-url.ts | 10 ++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/components/common/VersionSelector.tsx b/src/components/common/VersionSelector.tsx index e23366e4..b094e163 100644 --- a/src/components/common/VersionSelector.tsx +++ b/src/components/common/VersionSelector.tsx @@ -229,12 +229,16 @@ const VersionSelector = ({ isPackageNameDefinedInURL, showDiff, showReleaseCandidates, + appPackage, + appName, }: { packageName: string language: string isPackageNameDefinedInURL: boolean showDiff: (args: { fromVersion: string; toVersion: string }) => void showReleaseCandidates: boolean + appPackage: string + appName: string }) => { const { isLoading, isDone, releaseVersions } = useFetchReleaseVersions({ packageName, @@ -363,6 +367,8 @@ const VersionSelector = ({ isPackageNameDefinedInURL, fromVersion: localFromVersion, toVersion: localToVersion, + appPackage, + appName, }) } diff --git a/src/components/pages/Home.tsx b/src/components/pages/Home.tsx index c21ef618..bd276fcf 100644 --- a/src/components/pages/Home.tsx +++ b/src/components/pages/Home.tsx @@ -5,6 +5,7 @@ import { Card, Input, Typography, ConfigProvider, theme } from 'antd' import GitHubButton, { ReactGitHubButtonProps } from 'react-github-btn' import ReactGA from 'react-ga' import createPersistedState from 'use-persisted-state' +import queryString from 'query-string' import VersionSelector from '../common/VersionSelector' import DiffViewer from '../common/DiffViewer' import Settings from '../common/Settings' @@ -107,6 +108,19 @@ const SettingsContainer = styled.div` flex: 1; ` +const getAppInfoInURL = (): { + appPackage: string + appName: string +} => { + // Parses `/?name=RnDiffApp&package=com.rndiffapp` from URL + const { name, package: pkg } = queryString.parse(window.location.search) + + return { + appPackage: pkg as string, + appName: name as string, + } +} + interface StarButtonProps extends ReactGitHubButtonProps { className?: string } @@ -138,8 +152,9 @@ const Home = () => { [`${SHOW_LATEST_RCS}`]: false, }) - const [appName, setAppName] = useState('') - const [appPackage, setAppPackage] = useState('') + const appInfoInURL = getAppInfoInURL() + const [appName, setAppName] = useState(appInfoInURL.appName) + const [appPackage, setAppPackage] = useState(appInfoInURL.appPackage) // Avoid UI lag when typing. const deferredAppName = useDeferredValue(appName) @@ -303,6 +318,8 @@ const Home = () => { packageName={packageName} language={language} isPackageNameDefinedInURL={isPackageNameDefinedInURL} + appPackage={appPackage} + appName={appName} /> {/* diff --git a/src/utils/update-url.ts b/src/utils/update-url.ts index fdf619f4..680e184d 100644 --- a/src/utils/update-url.ts +++ b/src/utils/update-url.ts @@ -6,12 +6,16 @@ export function updateURL({ isPackageNameDefinedInURL, fromVersion, toVersion, + appPackage, + appName, }: { packageName: string language: string isPackageNameDefinedInURL: boolean fromVersion: string toVersion: string + appPackage: string + appName: string }) { const url = new URL(window.location.origin) url.pathname = window.location.pathname @@ -29,6 +33,12 @@ export function updateURL({ if (packageName === PACKAGE_NAMES.RNW) { url.searchParams.set('language', language) } + if (appPackage) { + url.searchParams.set('package', appPackage) + } + if (appName) { + url.searchParams.set('name', appName) + } window.history.replaceState(null, '', url.toString()) }