Skip to content

Commit

Permalink
Use enum to control theme
Browse files Browse the repository at this point in the history
Signed-off-by: Jenny <[email protected]>

format

fix build errors

Signed-off-by: Jenny <[email protected]>

fix import order and linting

Signed-off-by: Jenny <[email protected]>

Add process.env.STYLE_THEME
  • Loading branch information
jenny-s51 committed Nov 18, 2024
1 parent ee1c21c commit ccd6543
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 109 deletions.
31 changes: 8 additions & 23 deletions clients/ui/frontend/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import {
Spinner,
Stack,
StackItem,
Switch,
} from '@patternfly/react-core';
import { BarsIcon } from '@patternfly/react-icons';
import ToastNotifications from '~/shared/components/ToastNotifications';
import { useSettings } from '~/shared/hooks/useSettings';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';
import NavSidebar from './NavSidebar';
import AppRoutes from './AppRoutes';
import { AppContext } from './AppContext';
Expand All @@ -41,22 +41,14 @@ const App: React.FC = () => {
loadError: configError,
} = useSettings();

const [isChecked, setIsChecked] = React.useState<boolean>(window.isSwitched ?? false);

// Sync window.isSwitched with local state when it changes
React.useEffect(() => {
window.isSwitched = isChecked;
document.documentElement.classList.toggle('mui-theme', isChecked);

const event = new CustomEvent('isSwitchedChanged', {
detail: isChecked,
});
window.dispatchEvent(event);
}, [isChecked]);

const handleChange = (_event: React.FormEvent<HTMLInputElement>, checked: boolean) => {
setIsChecked(checked);
};
// Apply the theme based on the value of STYLE_THEME
if (STYLE_THEME === Theme.MUI) {
document.documentElement.classList.add(Theme.MUI);
} else {
document.documentElement.classList.remove(Theme.MUI);
}
}, []);

const contextValue = React.useMemo(
() =>
Expand Down Expand Up @@ -119,13 +111,6 @@ const App: React.FC = () => {
</MastheadMain>

<MastheadContent>
<Switch
id="simple-switch"
label="Toggle MUI Theme"
isChecked={isChecked}
onChange={handleChange}
ouiaId="BasicSwitch"
/>
{/* TODO: [Auth-enablement] Add logout and user status once we enable itNavigates to register page from table toolbar */}
</MastheadContent>
</Masthead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CheckIcon, TimesIcon } from '@patternfly/react-icons';
import { KeyValuePair } from '~/shared/types';
import { EitherNotBoth } from '~/shared/typeHelpers';
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';

type ModelPropertiesTableRowProps = {
allExistingKeys: string[];
Expand Down Expand Up @@ -49,23 +50,6 @@ const ModelPropertiesTableRow: React.FC<ModelPropertiesTableRowProps> = ({

const [isValueExpanded, setIsValueExpanded] = React.useState(false);

const [isSwitched, setIsSwitched] = React.useState<boolean>(window.isSwitched ?? false);

// Listen for changes to `window.isSwitched`
React.useEffect(() => {
const handleSwitchChange = (event: Event) => {
if (event instanceof CustomEvent) {
setIsSwitched(event.detail); // Update state with the new value
}
};

window.addEventListener('isSwitchedChanged', handleSwitchChange);

return () => {
window.removeEventListener('isSwitchedChanged', handleSwitchChange);
};
}, []);

let keyValidationError: string | null = null;
if (unsavedKey !== key && allExistingKeys.includes(unsavedKey)) {
keyValidationError = 'Key must not match an existing property key or label';
Expand Down Expand Up @@ -141,7 +125,7 @@ const ModelPropertiesTableRow: React.FC<ModelPropertiesTableRowProps> = ({
<Td dataLabel="Key" width={45} modifier="breakWord">
{isEditing ? (
<>
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset className="tr-fieldset-wrapper" component={propertyKeyInput} />
) : (
propertyKeyInput
Expand All @@ -161,7 +145,7 @@ const ModelPropertiesTableRow: React.FC<ModelPropertiesTableRowProps> = ({
</Td>
<Td dataLabel="Value" width={45} modifier="breakWord">
{isEditing ? (
isSwitched ? (
STYLE_THEME === Theme.MUI ? (
<FormFieldset className="tr-fieldset-wrapper" component={propertyValueInput} />
) : (
propertyValueInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { asEnumMember } from '~/app/utils';
import ModelVersionsTable from '~/app/pages/modelRegistry/screens/ModelVersions/ModelVersionsTable';
import SimpleSelect from '~/shared/components/SimpleSelect';
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';

type ModelVersionListViewProps = {
modelVersions: ModelVersion[];
Expand All @@ -54,18 +55,6 @@ const ModelVersionListView: React.FC<ModelVersionListViewProps> = ({
const [searchType, setSearchType] = React.useState<SearchType>(SearchType.KEYWORD);
const [search, setSearch] = React.useState('');

const [isSwitched, setIsSwitched] = React.useState<boolean>(window.isSwitched ?? false);

React.useEffect(() => {
const handleSwitchChange = () => {
setIsSwitched(window.isSwitched ?? false);
};
window.addEventListener('isSwitchedChanged', handleSwitchChange);
return () => {
window.removeEventListener('isSwitchedChanged', handleSwitchChange);
};
}, []);

const searchTypes = [SearchType.KEYWORD, SearchType.AUTHOR];

const [isArchivedModelVersionKebabOpen, setIsArchivedModelVersionKebabOpen] =
Expand Down Expand Up @@ -161,7 +150,7 @@ const ModelVersionListView: React.FC<ModelVersionListViewProps> = ({
/>
</ToolbarFilter>
<ToolbarItem>
{isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset
className="toolbar-fieldset-wrapper"
component={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { asEnumMember } from '~/app/utils';
import { filterModelVersions } from '~/app/pages/modelRegistry/screens/utils';
import EmptyModelRegistryState from '~/app/pages/modelRegistry/screens/components/EmptyModelRegistryState';
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';
import ModelVersionsArchiveTable from './ModelVersionsArchiveTable';

type ModelVersionsArchiveListViewProps = {
Expand All @@ -32,18 +33,6 @@ const ModelVersionsArchiveListView: React.FC<ModelVersionsArchiveListViewProps>

const searchTypes = [SearchType.KEYWORD, SearchType.AUTHOR];

const [isSwitched, setIsSwitched] = React.useState<boolean>(window.isSwitched ?? false);

React.useEffect(() => {
const handleSwitchChange = () => {
setIsSwitched(window.isSwitched ?? false);
};
window.addEventListener('isSwitchedChanged', handleSwitchChange);
return () => {
window.removeEventListener('isSwitchedChanged', handleSwitchChange);
};
}, []);

const filteredModelVersions = filterModelVersions(unfilteredmodelVersions, search, searchType);

if (unfilteredmodelVersions.length === 0) {
Expand Down Expand Up @@ -87,7 +76,7 @@ const ModelVersionsArchiveListView: React.FC<ModelVersionsArchiveListViewProps>
/>
</ToolbarFilter>
<ToolbarItem>
{isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset
className="toolbar-fieldset-wrapper"
component={
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { FormGroup, TextInput } from '@patternfly/react-core';
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';

type PrefilledModelRegistryFieldProps = {
mrName?: string;
Expand All @@ -13,7 +14,7 @@ const PrefilledModelRegistryField: React.FC<PrefilledModelRegistryFieldProps> =

return (
<FormGroup className="form-group-disabled" label="Model registry" isRequired fieldId="mr-name">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={mrNameInput} field="Model Registry" />
) : (
mrNameInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import FormSection from '~/shared/components/pf-overrides/FormSection';
import ApplicationsPage from '~/shared/components/ApplicationsPage';
import { modelRegistryUrl, registeredModelUrl } from '~/app/pages/modelRegistry/screens/routeUtils';
import { ValueOf } from '~/shared/typeHelpers';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';
import { useRegisterModelData, RegistrationCommonFormData } from './useRegisterModelData';
import { isRegisterModelSubmitDisabled, registerModel } from './utils';
import { useRegistrationCommonState } from './useRegistrationCommonState';
Expand Down Expand Up @@ -92,7 +93,7 @@ const RegisterModel: React.FC = () => {
isRequired
fieldId="mr-name"
>
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={modelRegistryInput} field="Model Registry" />
) : (
modelRegistryInput
Expand All @@ -105,7 +106,7 @@ const RegisterModel: React.FC = () => {
description="Provide general details that apply to all versions of this model."
>
<FormGroup label="Model name" isRequired fieldId="model-name">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={modelNameInput} field="Model Name" />
) : (
modelNameInput
Expand All @@ -116,7 +117,7 @@ const RegisterModel: React.FC = () => {
label="Model description"
fieldId="model-description"
>
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={modelDescriptionInput} field="Model Description" />
) : (
modelDescriptionInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormGroup, TextInput } from '@patternfly/react-core';
import { TypeaheadSelect, TypeaheadSelectOption } from '@patternfly/react-templates';
import { RegisteredModel } from '~/app/types';
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';

type RegisteredModelSelectorProps = {
registeredModels: RegisteredModel[];
Expand Down Expand Up @@ -47,7 +48,7 @@ const RegisteredModelSelector: React.FC<RegisteredModelSelectorProps> = ({
*/
return (
<FormGroup label="Model name" className="form-group-disabled" isRequired fieldId="model-name">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={modelNameInput} field="Model Name" />
) : (
modelNameInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { UpdateObjectAtPropAndValue } from '~/shared/types';
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
import FormSection from '~/shared/components/pf-overrides/FormSection';
import { ModelVersion } from '~/app/types';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';
import { ModelLocationType, RegistrationCommonFormData } from './useRegisterModelData';
// import { ConnectionModal } from './ConnectionModal';

Expand Down Expand Up @@ -173,7 +174,7 @@ const RegistrationCommonFormSections: React.FC<RegistrationCommonFormSectionsPro
}
>
<FormGroup label="Version name" isRequired fieldId="version-name">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={versionNameInput} field="Version Name" />
) : (
versionNameInput
Expand All @@ -191,21 +192,21 @@ const RegistrationCommonFormSections: React.FC<RegistrationCommonFormSectionsPro
label="Version description"
fieldId="version-description"
>
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={versionDescriptionInput} field="Version Description" />
) : (
versionDescriptionInput
)}
</FormGroup>
<FormGroup label="Source model format" fieldId="source-model-format">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={sourceModelFormatInput} field="Source Model Format" />
) : (
sourceModelFormatInput
)}
</FormGroup>
<FormGroup label="Source model format version" fieldId="source-model-format-version">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset
component={sourceModelFormatVersionInput}
field="Source Model Format Version"
Expand Down Expand Up @@ -253,28 +254,32 @@ const RegistrationCommonFormSections: React.FC<RegistrationCommonFormSectionsPro
isRequired
fieldId="location-endpoint"
>
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={endpointInput} field="Endpoint" />
) : (
endpointInput
)}
</FormGroup>
<FormGroup className={spacing.mlLg} label="Bucket" isRequired fieldId="location-bucket">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={bucketInput} field="Bucket" />
) : (
bucketInput
)}
</FormGroup>
<FormGroup className={spacing.mlLg} label="Region" fieldId="location-region">
{window.isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={regionInput} field="Region" />
) : (
regionInput
)}
</FormGroup>
<FormGroup className={spacing.mlLg} label="Path" isRequired fieldId="location-path">
{window.isSwitched ? <FormFieldset component={pathInput} field="Path" /> : pathInput}
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={pathInput} field="Path" />
) : (
pathInput
)}
</FormGroup>
<FormHelperText className="path-helper-text">
<HelperText>
Expand All @@ -301,7 +306,11 @@ const RegistrationCommonFormSections: React.FC<RegistrationCommonFormSectionsPro
{modelLocationType === ModelLocationType.URI && (
<>
<FormGroup className={spacing.mlLg} label="URI" isRequired fieldId="location-uri">
{window.isSwitched ? <FormFieldset component={uriInput} field="URI" /> : uriInput}
{STYLE_THEME === Theme.MUI ? (
<FormFieldset component={uriInput} field="URI" />
) : (
uriInput
)}
</FormGroup>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '~/app/pages/modelRegistry/screens/routeUtils';
import EmptyModelRegistryState from '~/app/pages/modelRegistry/screens/components/EmptyModelRegistryState';
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
import { STYLE_THEME, Theme } from '~/shared/utilities/const';
import RegisteredModelTable from './RegisteredModelTable';
import RegisteredModelsTableToolbar from './RegisteredModelsTableToolbar';

Expand All @@ -37,18 +38,6 @@ const RegisteredModelListView: React.FC<RegisteredModelListViewProps> = ({
const [searchType, setSearchType] = React.useState<SearchType>(SearchType.KEYWORD);
const [search, setSearch] = React.useState('');

const [isSwitched, setIsSwitched] = React.useState<boolean>(window.isSwitched ?? false);

React.useEffect(() => {
const handleSwitchChange = () => {
setIsSwitched(window.isSwitched ?? false);
};
window.addEventListener('isSwitchedChanged', handleSwitchChange);
return () => {
window.removeEventListener('isSwitchedChanged', handleSwitchChange);
};
}, []);

const searchTypes = React.useMemo(() => [SearchType.KEYWORD, SearchType.OWNER], []);

if (unfilteredRegisteredModels.length === 0) {
Expand Down Expand Up @@ -109,7 +98,7 @@ const RegisteredModelListView: React.FC<RegisteredModelListViewProps> = ({
/>
</ToolbarFilter>
<ToolbarItem variant="label-group">
{isSwitched ? (
{STYLE_THEME === Theme.MUI ? (
<FormFieldset
className="toolbar-fieldset-wrapper"
component={
Expand Down
Loading

0 comments on commit ccd6543

Please sign in to comment.