From 9a18d43ea6587b39f1af21a49f8d8a474ab3357c Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Tue, 10 Oct 2023 09:19:08 +0100 Subject: [PATCH] Refactored theme demo selector functionality (#18541) refs https://github.com/TryGhost/Product/issues/3999 Refactored the theme demo functionality to remove the hard-coded reference to the source theme and enable the functionality for any theme. --- .../components/providers/ServiceProvider.tsx | 3 +- .../settings/site/theme/OfficialThemes.tsx | 5 +- .../settings/site/theme/ThemePreview.tsx | 57 ++++++++++++++----- apps/admin-x-settings/src/main.tsx | 12 +++- .../admin/app/components/admin-x/settings.js | 12 +++- 5 files changed, 67 insertions(+), 22 deletions(-) diff --git a/apps/admin-x-settings/src/components/providers/ServiceProvider.tsx b/apps/admin-x-settings/src/components/providers/ServiceProvider.tsx index 73f8813a7f..b85972e652 100644 --- a/apps/admin-x-settings/src/components/providers/ServiceProvider.tsx +++ b/apps/admin-x-settings/src/components/providers/ServiceProvider.tsx @@ -4,8 +4,9 @@ import {DefaultHeaderTypes} from '../../utils/unsplash/UnsplashTypes'; import {ZapierTemplate} from '../settings/advanced/integrations/ZapierModal'; export type ThemeVariant = { - image: string; category: string; + previewUrl: string; + image: string; }; export type OfficialTheme = { diff --git a/apps/admin-x-settings/src/components/settings/site/theme/OfficialThemes.tsx b/apps/admin-x-settings/src/components/settings/site/theme/OfficialThemes.tsx index 3cfc31674c..34a024e8e9 100644 --- a/apps/admin-x-settings/src/components/settings/site/theme/OfficialThemes.tsx +++ b/apps/admin-x-settings/src/components/settings/site/theme/OfficialThemes.tsx @@ -12,8 +12,9 @@ const hasVariants = (theme: OfficialTheme) => theme.variants && theme.variants.l const getAllVariants = (theme: OfficialTheme) : ThemeVariant[] => { const variants = [{ - image: theme.image, - category: theme.category + category: theme.category, + previewUrl: theme.previewUrl, + image: theme.image }]; if (theme.variants && theme.variants.length > 0) { diff --git a/apps/admin-x-settings/src/components/settings/site/theme/ThemePreview.tsx b/apps/admin-x-settings/src/components/settings/site/theme/ThemePreview.tsx index 31f185c89a..0a0244345b 100644 --- a/apps/admin-x-settings/src/components/settings/site/theme/ThemePreview.tsx +++ b/apps/admin-x-settings/src/components/settings/site/theme/ThemePreview.tsx @@ -8,14 +8,26 @@ import NiceModal from '@ebay/nice-modal-react'; import PageHeader from '../../../../admin-x-ds/global/layout/PageHeader'; import React, {useState} from 'react'; import Select, {SelectOption} from '../../../../admin-x-ds/global/form/Select'; -import {OfficialTheme} from '../../../providers/ServiceProvider'; +import {OfficialTheme, ThemeVariant} from '../../../providers/ServiceProvider'; import {Theme} from '../../../../api/themes'; -const sourceDemos = [ - {label: 'News', value: 'news', url: 'https://source.ghost.io'}, - {label: 'Magazine', value: 'magazine', url: 'https://source-magazine.ghost.io'}, - {label: 'Newsletter', value: 'newsletter', url: 'https://source-newsletter.ghost.io'} -]; +const hasVariants = (theme: OfficialTheme) => theme.variants && theme.variants.length > 0; + +const getAllVariants = (theme: OfficialTheme) : ThemeVariant[] => { + const variants = [{ + image: theme.image, + category: theme.category, + previewUrl: theme.previewUrl + }]; + + if (theme.variants && theme.variants.length > 0) { + variants.push(...theme.variants); + } + + return variants; +}; + +const generateVariantOptionValue = (variant: ThemeVariant) => variant.category.toLowerCase(); const ThemePreview: React.FC<{ selectedTheme?: OfficialTheme; @@ -33,12 +45,29 @@ const ThemePreview: React.FC<{ onInstall }) => { const [previewMode, setPreviewMode] = useState('desktop'); - const [currentSourceDemo, setCurrentSourceDemo] = useState(sourceDemos[0]); + const [selectedVariant, setSelectedVariant] = useState(undefined); if (!selectedTheme) { return null; } + let previewUrl = selectedTheme.previewUrl; + + const variantOptions = getAllVariants(selectedTheme).map((variant) => { + return { + label: variant.category, + value: generateVariantOptionValue(variant) + }; + }); + + if (hasVariants(selectedTheme)) { + if (selectedVariant === undefined) { + setSelectedVariant(variantOptions[0]); + } + + previewUrl = getAllVariants(selectedTheme).find(variant => generateVariantOptionValue(variant) === selectedVariant?.value)?.previewUrl || previewUrl; + } + let installButtonLabel = `Install ${selectedTheme.name}`; if (isInstalling) { @@ -87,7 +116,7 @@ const ThemePreview: React.FC<{ backIcon onBack={onBack} /> - {selectedTheme.name === 'Source' ? + {hasVariants(selectedTheme) ? <>