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

WebUI Dark Mode #8137

Merged
merged 13 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
Binary file modified pkg/samplerepo/assets/sample/images/quickstart-step-00-launch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pkg/samplerepo/assets/sample/images/quickstart-step-01-query.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pkg/samplerepo/assets/sample/images/quickstart-step-02-branch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pkg/samplerepo/assets/sample/images/quickstart-step-03-merge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40,461 changes: 26,762 additions & 13,699 deletions webui/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-router-dom": "^6.20.1",
"react-simple-code-editor": "^0.13.1",
"react-syntax-highlighter": "^15.5.0",
"react-toggle-dark-mode": "^1.1.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UI component of the toggle button.

"rehype-raw": "^7.0.0",
"rehype-react": "^8.0.0",
"rehype-wrap": "^1.1.0",
Expand Down
10 changes: 7 additions & 3 deletions webui/src/lib/components/auth/credentials.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from "react";
import React, {useContext, useState} from "react";

import Modal from "react-bootstrap/Modal";
import Table from "react-bootstrap/Table";
Expand All @@ -9,6 +9,7 @@ import {useAPIWithPagination} from "../../hooks/api";
import {ClipboardButton, DataTable, AlertError, FormattedDate, Loading} from "../controls";
import {ConfirmationButton} from "../modals";
import {Paginator} from "../pagination";
import {AppContext} from "../../hooks/appContext";


export const CredentialsTable = ({userId, currentAccessKey, refresh, after, onPaginate}) => {
Expand Down Expand Up @@ -62,6 +63,9 @@ export const CredentialsTable = ({userId, currentAccessKey, refresh, after, onPa
export const CredentialsShowModal = ({ credentials, show, onHide }) => {
if (!credentials) return <></>;

const {state} = useContext(AppContext);
const buttonVariant = state.settings.darkMode ? "outline-light" : "outline-dark";

return (
<Modal show={show} onHide={onHide} size="lg">
<Modal.Header closeButton>
Expand All @@ -75,14 +79,14 @@ export const CredentialsShowModal = ({ credentials, show, onHide }) => {
<td><strong>Access Key ID</strong></td>
<td><code>{credentials.access_key_id}</code></td>
<td>
<ClipboardButton variant="outline-dark" tooltip="Copy to clipboard" text={credentials.access_key_id}/>
<ClipboardButton variant={buttonVariant} tooltip="Copy to clipboard" text={credentials.access_key_id}/>
</td>
</tr>
<tr>
<td><strong>Secret Access Key</strong></td>
<td><code>{credentials.secret_access_key}</code></td>
<td>
<ClipboardButton variant="outline-dark" tooltip="Copy to clipboard" text={credentials.secret_access_key}/>
<ClipboardButton variant={buttonVariant} tooltip="Copy to clipboard" text={credentials.secret_access_key}/>
</td>
</tr>
</tbody>
Expand Down
26 changes: 26 additions & 0 deletions webui/src/lib/components/darkModeToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, {FC, useContext} from "react";
import {DarkModeSwitch} from "react-toggle-dark-mode";
import {AppActionType, AppContext} from "../hooks/appContext";

const DarkModeToggle: FC = () => {
const {state, dispatch} = useContext(AppContext);

const toggleDarkMode = (isOn: boolean) => {
dispatch({
type: AppActionType.setDarkMode,
value: isOn,
});
};

return (
<DarkModeSwitch
style={{marginRight: '2rem'}}
checked={state.settings.darkMode}
onChange={toggleDarkMode}
size={28}
sunColor={"white"}
/>
);
};

export default DarkModeToggle;
11 changes: 8 additions & 3 deletions webui/src/lib/components/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import React, {FC, useState} from "react";
import React, {FC, useContext, useState} from "react";
import { Outlet, useOutletContext } from "react-router-dom";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint is inconsistent in the WebUI code -
I tried to avoid messing these, but in some files it was a bit too much.

import { StorageConfigProvider } from "../hooks/storageConfig";

import TopNav from './navbar';
import { AppContext } from "../hooks/appContext";

type LayoutOutletContext = [(isLoggedIn: boolean) => void];

const Layout: FC<{logged: boolean}> = ({ logged }) => {
const [isLogged, setIsLogged] = useState(logged ?? true);

// handle global dark mode here
const {state} = useContext(AppContext);
document.documentElement.setAttribute('data-bs-theme', state.settings.darkMode ? 'dark' : 'light')

return (
<>
<TopNav logged={isLogged}/>
<div className="main-app">
<StorageConfigProvider>
<Outlet context={[setIsLogged] satisfies LayoutOutletContext} />
<Outlet context={[setIsLogged] satisfies LayoutOutletContext}/>
</StorageConfigProvider>
</div>
</>
Expand Down
2 changes: 2 additions & 0 deletions webui/src/lib/components/navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import useUser from '../hooks/user'
import {auth, config} from "../api";
import {useRouter} from "../hooks/router";
import {Link} from "./nav";
import DarkModeToggle from "./darkModeToggle";
import {useAPI} from "../hooks/api";
import {Navbar, Nav, NavDropdown} from "react-bootstrap";
import Container from "react-bootstrap/Container";
Expand Down Expand Up @@ -91,6 +92,7 @@ const TopNav = ({logged = true}) => {
<TopNavLink href="/auth">Administration</TopNavLink>
</Nav>

<DarkModeToggle/>
<NavUserInfo/>
</Navbar.Collapse>
</Container>
Expand Down
6 changes: 5 additions & 1 deletion webui/src/lib/components/repository/ObjectsDiff.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, {useContext} from "react";
import {useAPI} from "../../hooks/api";
import {objects} from "../../api";
import ReactDiffViewer, {DiffMethod} from "react-diff-viewer-continued";
Expand All @@ -7,6 +7,7 @@ import {humanSize} from "./tree";
import Alert from "react-bootstrap/Alert";
import {InfoIcon} from "@primer/octicons-react";
import {useStorageConfig} from "../../hooks/storageConfig";
import {AppContext} from "../../hooks/appContext";

const maxDiffSizeBytes = 120 << 10;
const supportedReadableFormats = ["txt", "text", "csv", "tsv", "yaml", "yml", "json"];
Expand Down Expand Up @@ -74,6 +75,8 @@ const NoContentDiff = ({left, right, diffType}) => {
}

const ContentDiff = ({config, repoId, path, leftRef, rightRef, leftSize, rightSize, diffType}) => {
const {state} = useContext(AppContext);

const left = leftRef && useAPI(async () => objects.get(repoId, leftRef, path, config.pre_sign_support_ui),
[repoId, leftRef, path]);
const right = rightRef && useAPI(async () => objects.get(repoId, rightRef, path, config.pre_sign_support_ui),
Expand All @@ -89,6 +92,7 @@ const ContentDiff = ({config, repoId, path, leftRef, rightRef, leftSize, rightSi
oldValue={left && left.response}
newValue={right && right.response}
splitView={false}
useDarkTheme={state.settings.darkMode}
compareMethod={DiffMethod.WORDS}/>
</div>;
}
Expand Down
9 changes: 5 additions & 4 deletions webui/src/lib/components/repository/commits.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import {MetadataRow, MetadataUIButton} from "../../../pages/repositories/reposit
import {Link} from "../nav";
import dayjs from "dayjs";
import Card from "react-bootstrap/Card";
import React from "react";
import React, {useContext} from "react";
import {AppContext} from "../../hooks/appContext";


const CommitActions = ({ repo, commit }) => {

const buttonVariant = "outline-dark";
const {state} = useContext(AppContext);
const buttonVariant = state.settings.darkMode ? "outline-light" : "outline-dark";

return (
<div>
<ButtonGroup className="commit-actions">
<LinkButton
buttonVariant="outline-dark"
buttonVariant={buttonVariant}
href={{pathname: '/repositories/:repoId/objects', params: {repoId: repo.id}, query: {ref: commit.id}}}
tooltip="Browse commit objects">
<BrowserIcon/>
Expand Down
63 changes: 63 additions & 0 deletions webui/src/lib/hooks/appContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { createContext, useReducer } from "react";

type AppContextType = {
settings: AppContext;
};

type AppContext = {
darkMode: boolean;
};

const localStorageKeys = {
darkMode: 'darkMode',
};

enum AppActionType {
setDarkMode = 'setDarkMode',
}

interface Action {
type: AppActionType;
value: boolean;
}

const initialLocalSettings: AppContext = {
darkMode: window.localStorage.getItem(localStorageKeys.darkMode) === String(true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

};

const initialAppContext: AppContextType = {
settings: initialLocalSettings,
};

const appContextReducer = (state: AppContextType, action: Action) => {
switch (action.type) {
case AppActionType.setDarkMode:
window.localStorage.setItem(localStorageKeys.darkMode, String(action.value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

return {...state, settings: {...state.settings, darkMode: action.value}};
default:
return state;
}
}

type ContextType = {
state: AppContextType,
dispatch: React.Dispatch<Action>,
};

const AppContext = createContext<ContextType>({
state: initialAppContext,
dispatch: () => null
});

// @ts-expect-error - it doesn't like the "children" prop
const WithAppContext: React.FC = ({children}) => {
const [state, dispatch] = useReducer(appContextReducer, initialAppContext);

return (
<AppContext.Provider value={{state, dispatch}}>
{children}
</AppContext.Provider>
)
}

export { WithAppContext, AppContext, AppActionType };
Loading
Loading