Skip to content

Commit

Permalink
prism themes
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski committed Nov 29, 2024
1 parent 4643c57 commit 935fad6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
28 changes: 23 additions & 5 deletions webapp/chat-app/src/components/MessageList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useCallback} from 'react';
import styled from 'styled-components';
import {useSelector} from 'react-redux';
import {useTheme} from '../hooks/useTheme';
import {RootState} from '../store';
import {logger} from '../utils/logger';
import {Message} from '../types';
Expand Down Expand Up @@ -66,24 +67,35 @@ const MessageContent = styled.div`
padding: 2px 8px;
margin: 2px;
border-radius: 4px;
background-color: rgba(0, 0, 0, 0.1);
background-color: ${({theme}) => theme.colors.surface};
color: ${({theme}) => theme.colors.text.primary};
&:hover {
opacity: 0.8;
background-color: rgba(0, 0, 0, 0.2);
background-color: ${({theme}) => theme.colors.primary};
color: ${({theme}) => theme.colors.background};
}
}
.referenced-message {
cursor: pointer;
padding: 4px;
margin: 4px 0;
border-left: 3px solid #ccc;
border-left: 3px solid ${({theme}) => theme.colors.border};
&.expanded {
background-color: rgba(0, 0, 0, 0.05);
background-color: ${({theme}) => theme.colors.surface};
}
}
/* Style code blocks according to theme */
pre[class*="language-"] {
background: ${({theme}) => theme.colors.surface};
border: 1px solid ${({theme}) => theme.colors.border};
}
code[class*="language-"] {
color: ${({theme}) => theme.colors.text.primary};
text-shadow: none;
}
`;

const extractMessageAction = (target: HTMLElement): { messageId: string | undefined, action: string | undefined } => {
Expand Down Expand Up @@ -183,6 +195,7 @@ interface MessageListProps {
}

const MessageList: React.FC<MessageListProps> = ({messages: propMessages}) => {
const theme = useTheme();
logger.component('MessageList', 'Rendering component', {hasPropMessages: !!propMessages});
// Store tab states on mount
React.useEffect(() => {
Expand Down Expand Up @@ -236,7 +249,12 @@ const MessageList: React.FC<MessageListProps> = ({messages: propMessages}) => {

const processMessageContent = useCallback((content: string) => {
logger.debug('Processing message content', {contentLength: content.length});
return expandMessageReferences(content, messages);
const processed = expandMessageReferences(content, messages);
// Re-highlight code blocks after theme change
requestAnimationFrame(() => {
Prism.highlightAll();
});
return processed;
}, [messages]);

React.useEffect(() => {
Expand Down
28 changes: 28 additions & 0 deletions webapp/chat-app/src/styles/GlobalStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@ const logThemeChange = (theme: DefaultTheme) => {
};

export const GlobalStyles = createGlobalStyle<{ theme: DefaultTheme }>`
/* Override Prism.js theme colors to match current theme */
.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: ${({theme}) => theme.colors.text.secondary};
}
.token.punctuation {
color: ${({theme}) => theme.colors.text.primary};
}
.token.property,
.token.tag,
.token.constant,
.token.symbol {
color: ${({theme}) => theme.colors.primary};
}
.token.boolean,
.token.number {
color: ${({theme}) => theme.colors.warning};
}
.token.selector,
.token.string {
color: ${({theme}) => theme.colors.success};
}
.token.operator,
.token.keyword {
color: ${({theme}) => theme.colors.info};
}
/* Reset styles */
* {
margin: 0;
Expand Down
28 changes: 27 additions & 1 deletion webapp/chat-app/src/themes/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@ import React, {useEffect, useRef} from 'react';
import {ThemeProvider as StyledThemeProvider} from 'styled-components';
import {useSelector} from 'react-redux';
import {RootState} from '../store';
import {logThemeChange, themes} from './themes';
import {logThemeChange, themes, ThemeName} from './themes';
import Prism from 'prismjs';

interface ThemeProviderProps {
children: React.ReactNode;
}

const LOG_PREFIX = '[ThemeProvider]';
// Define Prism themes mapping to our theme names
const prismThemes: Record<ThemeName, string> = {
main: 'prism',
night: 'prism-dark',
forest: 'prism-okaidia',
pony: 'prism-twilight',
alien: 'prism-tomorrow'
};
// Function to load Prism theme
const loadPrismTheme = async (themeName: ThemeName) => {
const prismTheme = prismThemes[themeName] || 'prism';
try {
await import(`prismjs/themes/${prismTheme}.css`);
console.log(`${LOG_PREFIX} Loaded Prism theme: ${prismTheme}`);
} catch (error) {
console.warn(`${LOG_PREFIX} Failed to load Prism theme: ${prismTheme}`, error);
}
};

export const ThemeProvider: React.FC<ThemeProviderProps> = ({children}) => {
const currentTheme = useSelector((state: RootState) => state.ui.theme);
Expand All @@ -28,6 +47,13 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({children}) => {
document.body.className = `theme-${currentTheme}`;
// Add transition class
document.body.classList.add('theme-transition');
// Load and apply Prism theme
loadPrismTheme(currentTheme).then(() => {
// Re-highlight all code blocks with new theme
requestAnimationFrame(() => {
Prism.highlightAll();
});
});
const timer = setTimeout(() => {
document.body.classList.remove('theme-transition');
}, 300);
Expand Down

0 comments on commit 935fad6

Please sign in to comment.