|
| 1 | +import { Splitter } from '@ark-ui/react/splitter'; |
| 2 | +import React, { type JSX, useEffect, useState } from 'react'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import * as ODSReact from '@ovhcloud/ods-react'; |
| 5 | +import { ORIENTATION, OrientationSwitch } from '../sandbox/actions/OrientationSwitch'; |
| 6 | +import styles from './themeGenerator.module.css'; |
| 7 | +import { ThemeGeneratorTreeView } from './themeGeneratorTreeView/ThemeGeneratorTreeView'; |
| 8 | +import { ThemeGeneratorPreview } from './themeGeneratorPreview/ThemeGeneratorPreview'; |
| 9 | +import { parseCssVariables } from './useThemeGenerator'; |
| 10 | +import { ThemeGeneratorModal } from './themeGeneratorModal/ThemeGeneratorModal'; |
| 11 | +import { ThemeGeneratorJSON } from './themeGeneratorJSON/ThemeGeneratorJSON'; |
| 12 | + |
| 13 | +const ThemeGenerator = (): JSX.Element => { |
| 14 | + const [isFullscreen, setIsFullscreen] = useState(false); |
| 15 | + const [orientation, setOrientation] = useState(ORIENTATION.horizontal); |
| 16 | + const [selectedTheme, setSelectedTheme] = useState('default'); |
| 17 | + const [editedVariables, setEditedVariables] = useState<Record<string, string>>({}); |
| 18 | + const [isCustomTheme, setIsCustomTheme] = useState(false); |
| 19 | + const [isConfirmOpen, setIsConfirmOpen] = useState(false); |
| 20 | + const [pendingTheme, setPendingTheme] = useState<string | null>(null); |
| 21 | + const [isJsonOpen, setIsJsonOpen] = useState(false); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + const loadTheme = async () => { |
| 25 | + if (selectedTheme === 'custom') { |
| 26 | + setIsCustomTheme(true); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + console.log('Selected theme:', selectedTheme); |
| 32 | + |
| 33 | + // Fetch the CSS file from the static directory |
| 34 | + // Path: /themes/{themeName}/index.css (exposed via staticDirs in main.ts) |
| 35 | + const response = await fetch(`/themes/${selectedTheme}/index.css`); |
| 36 | + |
| 37 | + if (!response.ok) { |
| 38 | + throw new Error(`Failed to fetch theme: ${response.statusText}`); |
| 39 | + } |
| 40 | + |
| 41 | + const cssContent = await response.text(); |
| 42 | + |
| 43 | + // Parse CSS variables |
| 44 | + const variables = parseCssVariables(cssContent); |
| 45 | + console.log('Parsed variables:', Object.keys(variables).length, 'variables found'); |
| 46 | + setEditedVariables(variables); |
| 47 | + setIsCustomTheme(false); |
| 48 | + } catch (error) { |
| 49 | + console.error('Failed to load theme:', error); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + loadTheme(); |
| 54 | + }, [selectedTheme]); |
| 55 | + |
| 56 | + |
| 57 | + function onToggleFullscreen() { |
| 58 | + setIsFullscreen((v) => !v); |
| 59 | + } |
| 60 | + |
| 61 | + function onVariableChange(name: string, value: string) { |
| 62 | + setEditedVariables((prev) => ({ |
| 63 | + ...prev, |
| 64 | + [name]: value, |
| 65 | + })); |
| 66 | + |
| 67 | + // Automatically switch to custom theme when user edits a variable |
| 68 | + if (!isCustomTheme) { |
| 69 | + setSelectedTheme('custom'); |
| 70 | + setIsCustomTheme(true); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return <div className={ classNames( |
| 75 | + styles['theme-generator'], |
| 76 | + { [ styles['theme-generator--fullscreen']]: isFullscreen }, |
| 77 | + )}> |
| 78 | + <div className={ styles['theme-generator__menu'] }> |
| 79 | + <div className={styles['theme-generator__menu__left']}> |
| 80 | + <ODSReact.Button |
| 81 | + variant={ ODSReact.BUTTON_VARIANT.ghost } |
| 82 | + onClick={ () => setIsJsonOpen(true) }> |
| 83 | + <ODSReact.Icon name={ODSReact.ICON_NAME.chevronLeftUnderscore} /> |
| 84 | + JSON |
| 85 | + </ODSReact.Button> |
| 86 | + <ODSReact.Switch |
| 87 | + value={selectedTheme} |
| 88 | + onValueChange={(details: { value: string }) => { |
| 89 | + const next = details.value; |
| 90 | + const isLeavingCustom = isCustomTheme && next !== 'custom'; |
| 91 | + |
| 92 | + if (isLeavingCustom) { |
| 93 | + setPendingTheme(next); |
| 94 | + setIsConfirmOpen(true); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + setSelectedTheme(next); |
| 99 | + }} |
| 100 | + > |
| 101 | + <ODSReact.SwitchItem value="default"> |
| 102 | + Default |
| 103 | + </ODSReact.SwitchItem> |
| 104 | + <ODSReact.SwitchItem value="developer"> |
| 105 | + Developer |
| 106 | + </ODSReact.SwitchItem> |
| 107 | + <ODSReact.SwitchItem value="custom"> |
| 108 | + Custom |
| 109 | + </ODSReact.SwitchItem> |
| 110 | + </ODSReact.Switch> |
| 111 | + </div> |
| 112 | + <div className={styles['theme-generator__menu__right']}> |
| 113 | + <OrientationSwitch |
| 114 | + onChange={ (value) => setOrientation(value) } |
| 115 | + orientation={ orientation } /> |
| 116 | + |
| 117 | + <ODSReact.Button |
| 118 | + onClick={ onToggleFullscreen } |
| 119 | + variant={ ODSReact.BUTTON_VARIANT.ghost }> |
| 120 | + <ODSReact.Icon name={ isFullscreen ? ODSReact.ICON_NAME.shrink : ODSReact.ICON_NAME.resize } /> |
| 121 | + </ODSReact.Button> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <Splitter.Root |
| 125 | + className={ styles['theme-generator__container'] } |
| 126 | + orientation={ orientation } |
| 127 | + panels={ [{ id: 'tree-view', minSize: 10 }, { id: 'preview', minSize: 10 }] }> |
| 128 | + <Splitter.Panel id="tree-view"> |
| 129 | + <ThemeGeneratorTreeView |
| 130 | + variables={editedVariables} |
| 131 | + onVariableChange={onVariableChange} /> |
| 132 | + </Splitter.Panel> |
| 133 | + |
| 134 | + <Splitter.ResizeTrigger |
| 135 | + asChild |
| 136 | + aria-label="Resize" |
| 137 | + id="tree-view:preview"> |
| 138 | + <ODSReact.Button |
| 139 | + className={ classNames( |
| 140 | + styles['theme-generator__container__resize'], |
| 141 | + { [styles['theme-generator__container__resize--horizontal']]: orientation === ORIENTATION.horizontal }, |
| 142 | + { [styles['theme-generator__container__resize--vertical']]: orientation === ORIENTATION.vertical }, |
| 143 | + )} |
| 144 | + color={ ODSReact.BUTTON_COLOR.neutral } /> |
| 145 | + </Splitter.ResizeTrigger> |
| 146 | + |
| 147 | + <Splitter.Panel id="preview"> |
| 148 | + <div className={ styles['theme-generator__container__preview'] }> |
| 149 | + <ThemeGeneratorPreview themeVariables={editedVariables} /> |
| 150 | + </div> |
| 151 | + </Splitter.Panel> |
| 152 | + </Splitter.Root> |
| 153 | + |
| 154 | + <ThemeGeneratorModal |
| 155 | + open={ isConfirmOpen } |
| 156 | + targetTheme={ pendingTheme } |
| 157 | + onConfirm={() => { |
| 158 | + if (pendingTheme) { |
| 159 | + setSelectedTheme(pendingTheme); |
| 160 | + } |
| 161 | + setPendingTheme(null); |
| 162 | + setIsConfirmOpen(false); |
| 163 | + }} |
| 164 | + onCancel={() => { |
| 165 | + setPendingTheme(null); |
| 166 | + setIsConfirmOpen(false); |
| 167 | + }} |
| 168 | + /> |
| 169 | + |
| 170 | + <ThemeGeneratorJSON |
| 171 | + open={ isJsonOpen } |
| 172 | + variables={ editedVariables } |
| 173 | + onClose={ () => setIsJsonOpen(false) } |
| 174 | + onReplace={(next) => { |
| 175 | + setEditedVariables(next); |
| 176 | + setSelectedTheme('custom'); |
| 177 | + setIsCustomTheme(true); |
| 178 | + }} |
| 179 | + /> |
| 180 | + </div> |
| 181 | +} |
| 182 | + |
| 183 | +export { ThemeGenerator }; |
0 commit comments