Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto theme mode added #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ import { ColorModeContext } from './context/color-context';

function NewApp() {
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
const [mode, setMode] = React.useState(
const [customMode, setCustomMode] = React.useState(
localStorage.getItem('qdrant-web-ui-theme') || (prefersDarkMode ? 'dark' : 'light')
);
localStorage.setItem('qdrant-web-ui-theme', mode);

const [mode, setMode] = React.useState(customMode === 'auto' ? (prefersDarkMode ? 'dark' : 'light') : customMode);
localStorage.setItem('qdrant-web-ui-theme', customMode);
const colorMode = React.useMemo(
() => ({
// The dark mode switch would invoke this method
toggleColorMode: () => {
setMode((prevMode) => (prevMode === 'light' ? 'dark' : 'light'));
setCustomMode((prevMode) => (prevMode === 'light' ? 'dark' : prevMode === 'dark' ? 'auto' : 'light'));
},
mode: customMode,
}),
[]
[customMode]
);

React.useEffect(() => {
if (customMode === 'auto') {
setMode(prefersDarkMode ? 'dark' : 'light');
} else {
setMode(customMode);
}
}, [customMode, prefersDarkMode]);

const theme = React.useMemo(
() =>
createTheme({
Expand Down
1 change: 1 addition & 0 deletions src/context/color-context.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';

export const ColorModeContext = React.createContext({
mode: 'auto',
toggleColorMode: () => {},
});
14 changes: 12 additions & 2 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import KeyIcon from '@mui/icons-material/Key';
import { useClient } from '../context/client-context';
import { Logo } from '../components/Logo';
import Sidebar from '../components/Sidebar';
import AutoModeIcon from '@mui/icons-material/BrightnessAuto';

const DrawerHeader = styled('div')(({ theme }) => ({
display: 'flex',
Expand All @@ -27,7 +28,6 @@ export default function MiniDrawer() {
const [open, setOpen] = React.useState(false);
const [version, setVersion] = useState('???');
const colorMode = React.useContext(ColorModeContext);

const [apiKeyDialogOpen, setApiKeyDialogOpen] = useState(false);
const { client: qdrantClient } = useClient();

Expand Down Expand Up @@ -84,7 +84,17 @@ export default function MiniDrawer() {
<Box sx={{ flexGrow: 1 }}></Box>
<Tooltip title="Color Mode">
<IconButton size="large" onClick={colorMode.toggleColorMode}>
{theme.palette.mode === 'dark' ? <LightModeIcon /> : <DarkModeIcon />}
<ColorModeContext.Consumer>
{(colorMode) => {
if (colorMode.mode === 'light') {
return <DarkModeIcon />;
} else if (colorMode.mode === 'dark') {
return <LightModeIcon />;
} else {
return <AutoModeIcon />;
}
}}
</ColorModeContext.Consumer>
</IconButton>
</Tooltip>
<Tooltip title="API Key">
Expand Down
Loading