Skip to content

Commit a495bb0

Browse files
authored
Adapt PrismCode for dark mode (#3348)
1 parent 4e0fb12 commit a495bb0

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

libs/ui-lib/lib/common/components/clusterConfiguration/DiscoveryTroubleshootingModal.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
ModalBody,
1717
} from '@patternfly/react-core';
1818
import { InfoCircleIcon } from '@patternfly/react-icons/dist/js/icons/info-circle-icon';
19-
import { PrismCode, SimpleAIPrismTheme, UiIcon } from '../ui';
19+
import { PrismCode, UiIcon } from '../ui';
2020
import { useTranslation } from '../../hooks/use-translation-wrapper';
2121
import { Trans } from 'react-i18next';
2222
import { saveAs } from 'file-saver';
@@ -70,7 +70,7 @@ export const DiscoveryTroubleshootingModalContent = () => {
7070
)}
7171
</HelperTextItem>
7272
</HelperText>
73-
<PrismCode theme={SimpleAIPrismTheme} code="ssh -i <identity_file> core@<machine-ip>" />
73+
<PrismCode code="ssh -i <identity_file> core@<machine-ip>" />
7474
<HelperText>
7575
<HelperTextItem data-testid="discovery-troubleshooting-ssh-auth-helper">
7676
{t(
@@ -106,7 +106,6 @@ export const DiscoveryTroubleshootingModalContent = () => {
106106
</HelperTextItem>
107107
</HelperText>
108108
<PrismCode
109-
theme={SimpleAIPrismTheme}
110109
code={`chmod +x ./change-iso-password.sh
111110
./change-iso-password.sh <path_to_your_iso_file>`}
112111
/>
@@ -115,14 +114,13 @@ export const DiscoveryTroubleshootingModalContent = () => {
115114
<Content component="p">
116115
{t('ai:The discovery agent is running with the correct parameters')}
117116
</Content>
118-
<PrismCode theme={SimpleAIPrismTheme} code={`ps -ef | grep agent`} />
117+
<PrismCode code={`ps -ef | grep agent`} />
119118
<HelperText>
120119
<HelperTextItem data-testid="discovery-troubleshooting-agent-output-helper">
121120
{t('ai:The output displays the following:')}
122121
</HelperTextItem>
123122
</HelperText>
124123
<PrismCode
125-
theme={SimpleAIPrismTheme}
126124
code={`root 1786 1 0 08:03 ? 00:00:00 /usr/local/bin/agent --url https://api.openshift.com --cluster-id e4c85fbe-77c7-411e-b107-5120f615c4fb --agent-version registry.redhat.io/openshift4/assisted-installer-agent-rhel8:v4.6.0-17 --insecure=false
127125
core 2362 2311 0 08:04 pts/0 00:00:00 grep --color=auto agent`}
128126
/>
@@ -134,14 +132,14 @@ core 2362 2311 0 08:04 pts/0 00:00:00 grep --color=auto agent`}
134132
{t('ai:To verify that the agent ran successfully, check the logs:')}
135133
</HelperTextItem>
136134
</HelperText>
137-
<PrismCode theme={SimpleAIPrismTheme} code="sudo journalctl -u agent.service" />
135+
<PrismCode code="sudo journalctl -u agent.service" />
138136
<HelperText>
139137
<HelperTextItem data-testid="discovery-troubleshooting-network-error-helper">
140138
{t('ai:In the following example, the errors indicate there is a network issue:')}
141139
</HelperTextItem>
142140
</HelperText>
143141
<PrismCode
144-
theme={{ ...SimpleAIPrismTheme, styles: [] }}
142+
styles={[]}
145143
code={`Oct 15 11:26:35 localhost systemd[1]: agent.service: Service RestartSec=3s expired, scheduling restart.
146144
Oct 15 11:26:35 localhost systemd[1]: agent.service: Scheduled restart job, restart counter is at 9.
147145
Oct 15 11:26:35 localhost systemd[1]: Stopped agent.service.
@@ -170,20 +168,19 @@ Oct 15 11:26:35 localhost systemd[1]: Failed to start agent.service.`}
170168
{t('ai:To view detailed agent logs and communication use following command:')}
171169
</HelperTextItem>
172170
</HelperText>
173-
<PrismCode theme={SimpleAIPrismTheme} code="sudo journalctl TAG=agent | less" />
171+
<PrismCode code="sudo journalctl TAG=agent | less" />
174172
</ListItem>
175173
<ListItem>
176174
<Content component="p">{t('ai:Check assisted-installer logs')}</Content>
177175
<PrismCode
178-
theme={SimpleAIPrismTheme}
179176
code={`sudo su
180177
podman ps -a | grep assisted-installer
181178
podman logs <container id>`}
182179
/>
183180
</ListItem>
184181
<ListItem>
185182
<Content component="p">{t('ai:Check bootkube logs')}</Content>
186-
<PrismCode theme={SimpleAIPrismTheme} code={`sudo journalctl -u bootkube`} />
183+
<PrismCode code={`sudo journalctl -u bootkube`} />
187184
</ListItem>
188185
</List>
189186
);

libs/ui-lib/lib/common/components/ui/PrismCode.tsx

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,55 @@ import Highlight, { defaultProps, Language, PrismTheme } from 'prism-react-rende
33
import { Content, ClipboardCopy, clipboardCopyFunc } from '@patternfly/react-core';
44
import { t_global_color_nonstatus_purple_300 } from '@patternfly/react-tokens/dist/js/t_global_color_nonstatus_purple_300';
55
import { t_global_color_nonstatus_blue_default as globalBlue } from '@patternfly/react-tokens/dist/js/t_global_color_nonstatus_blue_default';
6-
import defaultTheme from 'prism-react-renderer/themes/github';
6+
import lightTheme from 'prism-react-renderer/themes/github';
7+
import darkTheme from 'prism-react-renderer/themes/okaidia';
78
import './PrismCode.css';
8-
export const SimpleAIPrismTheme = {
9-
plain: {
10-
color: '#333333',
11-
backgroundColor: defaultTheme.plain.backgroundColor,
12-
fontSize: '.93em',
13-
},
14-
styles: [
15-
{
16-
types: ['variable'],
17-
style: {
18-
color: globalBlue.value,
19-
},
9+
10+
export const getTheme = (styles?: PrismTheme['styles']) => {
11+
let prefersTheme = window.localStorage.getItem('bridge/theme');
12+
if (prefersTheme === 'systemDefault') {
13+
prefersTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
14+
}
15+
16+
const theme = prefersTheme === 'dark' ? darkTheme : lightTheme;
17+
18+
return {
19+
plain: {
20+
color: theme.plain.color,
21+
backgroundColor: theme.plain.backgroundColor,
22+
fontSize: '.93em',
2023
},
21-
{
22-
types: ['class-name', 'function', 'tag', 'attr-name'],
23-
style: {
24-
color: t_global_color_nonstatus_purple_300.value,
24+
styles: styles || [
25+
{
26+
types: ['variable'],
27+
style: {
28+
color: globalBlue.value,
29+
},
2530
},
26-
},
27-
],
31+
{
32+
types: ['class-name', 'function', 'tag', 'attr-name'],
33+
style: {
34+
color: t_global_color_nonstatus_purple_300.value,
35+
},
36+
},
37+
],
38+
};
2839
};
40+
2941
type PrismCodeProps = {
3042
code: string;
3143
language?: Language;
32-
theme?: PrismTheme;
44+
styles?: PrismTheme['styles'];
3345
copiable?: boolean;
3446
};
47+
3548
const PrismCode: React.FC<PrismCodeProps> = ({
3649
code,
50+
styles,
3751
language = 'bash',
38-
theme = defaultTheme,
3952
copiable = false,
4053
}) => (
41-
<Highlight {...defaultProps} code={code} language={language} theme={theme}>
54+
<Highlight {...defaultProps} code={code} language={language} theme={getTheme(styles)}>
4255
{({ className, style, tokens, getLineProps, getTokenProps }) => (
4356
<Content component="pre" className={className} style={style}>
4457
{copiable && (

libs/ui-lib/lib/common/components/ui/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export { default as PopoverIcon } from './PopoverIcon';
2121
export { default as WizardNavItem } from './WizardNavItem';
2222
export { default as Alerts } from './Alerts';
2323
export { default as ExternalLink } from './ExternalLink';
24-
export { default as PrismCode, SimpleAIPrismTheme } from './PrismCode';
24+
export { default as PrismCode } from './PrismCode';
2525
export { default as GridGap } from './GridGap';
2626
export { default as EventsList } from './EventsList';
2727
export { default as ClusterEventsToolbar } from './ClusterEventsToolbar';

0 commit comments

Comments
 (0)