dashboard/src/components/settings.tsx

194 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-06-14 11:29:03 +02:00
import { useState } from "react";
2020-07-08 19:36:36 +02:00
import styled from "styled-components";
2021-07-02 11:41:02 +02:00
import Select from "./select";
2020-07-08 19:36:36 +02:00
2021-04-22 11:15:41 +02:00
import { ISearchProps } from "./searchBar";
2022-02-13 20:57:26 +01:00
import {
setTheme,
IThemeProps,
IThemeDataProps,
getTheme,
} from "../lib/useTheme";
2021-03-21 18:05:24 +01:00
import { Button, SubHeadline } from "./elements";
2020-07-08 19:36:36 +02:00
2020-09-08 13:18:58 +02:00
import Modal from "./modal";
2021-06-21 16:52:16 +02:00
export const FormContainer = styled.div`
2021-11-24 21:30:44 +01:00
display: inline-grid;
grid-template-columns: auto auto;
grid-gap: 0.5rem 1rem;
2021-07-10 23:57:08 +02:00
margin-bottom: 1em;
2020-07-08 19:36:36 +02:00
`;
2021-06-21 16:52:16 +02:00
export const Table = styled.table`
2020-07-08 19:36:36 +02:00
font-weight: 400;
background: none;
2021-04-22 11:15:41 +02:00
width: 100%;
2021-07-10 23:57:08 +02:00
color: ${(props) => props.theme.mainColor};
2020-07-08 19:36:36 +02:00
`;
2021-06-21 16:52:16 +02:00
export const TableRow = styled.tr`
2021-07-10 23:57:08 +02:00
border-bottom: 1px solid ${(props) => props.theme.mainColor};
2020-07-08 19:36:36 +02:00
`;
2021-06-21 16:52:16 +02:00
export const TableCell = styled.td`
2020-07-08 19:36:36 +02:00
background: none;
padding-top: 0.5rem;
`;
2021-06-21 16:52:16 +02:00
export const HeadCell = styled.th`
2020-07-08 19:36:36 +02:00
font-weight: 700;
2021-04-22 11:15:41 +02:00
text-align: left;
2020-07-08 19:36:36 +02:00
`;
2021-06-21 16:52:16 +02:00
export const Section = styled.div`
2021-03-21 18:05:24 +01:00
padding: 1rem 0;
`;
2021-06-21 16:52:16 +02:00
export const SectionHeadline = styled(SubHeadline)`
2021-03-21 18:05:24 +01:00
width: 100%;
2021-07-10 23:57:08 +02:00
border-bottom: 1px solid ${(props) => props.theme.accentColor};
2021-03-21 18:05:24 +01:00
margin-bottom: 0.5rem;
`;
2021-04-22 11:15:41 +02:00
const Text = styled.p`
font-weight: 700;
2021-07-10 23:57:08 +02:00
color: ${(props) => props.theme.accentColor};
2021-04-22 11:15:41 +02:00
`;
const Code = styled.p`
font-family: monospace;
2021-07-10 23:57:08 +02:00
color: ${(props) => props.theme.accentColor};
`;
const ThemeHeader = styled.p`
color: ${(props) => props.theme.accentColor};
`;
2021-06-30 00:56:01 +02:00
const ThemeSelect = styled(Select)`
-webkit-appearance: button;
-moz-appearance: button;
text-transform: uppercase;
font-family: Roboto, sans-serif;
font-weight: 400;
2021-07-10 23:57:08 +02:00
border: 1px solid ${(props) => props.theme.mainColor};
color: ${(props) => props.theme.mainColor};
2021-06-30 00:56:01 +02:00
background: none;
& > option {
2021-07-10 23:57:08 +02:00
background-color: ${(props) => props.theme.backgroundColor};
2021-06-30 00:56:01 +02:00
}
`;
2020-07-08 19:36:36 +02:00
2021-11-24 21:30:44 +01:00
const ContentContainer = styled.div`
display: flex;
justify-content: space-between;
flex-direction: row;
@media (max-width: 766px) {
flex-direction: column;
}
`;
interface ISettingsProps {
2022-02-13 20:57:26 +01:00
themes?: IThemeDataProps;
search?: ISearchProps;
}
2020-07-08 19:36:36 +02:00
2021-03-21 19:59:18 +01:00
/**
* Handles the settings-modal
2022-02-13 20:57:26 +01:00
* @param {IThemeDataProps} themes - the list of themes a user can select between
2021-04-22 11:15:41 +02:00
* @param {ISearchProps} search - the list of search providers
2021-03-21 19:59:18 +01:00
*/
2021-04-22 11:15:41 +02:00
const Settings = ({ themes, search }: ISettingsProps) => {
2021-07-10 23:57:08 +02:00
const [newLightTheme, setNewLightTheme] = useState<IThemeProps>();
const [newDarkTheme, setNewDarkTheme] = useState<IThemeProps>();
2021-11-26 21:06:15 +01:00
const currentLightTheme = getTheme("light").value;
const currentDarkTheme = getTheme("dark").value;
2020-07-08 19:36:36 +02:00
2022-02-13 20:57:26 +01:00
if (themes === undefined && search === undefined) return <></>;
return (
<Modal element="icon" icon="settings" title="Settings">
<ContentContainer>
{themes !== undefined && (
<Section>
<SectionHeadline>Theme</SectionHeadline>
<FormContainer>
<div>
<ThemeHeader>Light</ThemeHeader>
<ThemeSelect
items={themes.themes}
onChange={(theme: IThemeProps) => setNewLightTheme(theme)}
current={currentLightTheme}
testId="light"
></ThemeSelect>
</div>
<div>
<ThemeHeader>Dark</ThemeHeader>
<ThemeSelect
items={themes.themes}
onChange={(theme: IThemeProps) => setNewDarkTheme(theme)}
current={currentDarkTheme}
testId="dark"
></ThemeSelect>
</div>
<div>
<Button
data-testid="button-submit"
onClick={() => {
if (newLightTheme) setTheme("light", newLightTheme);
if (newDarkTheme) setTheme("dark", newDarkTheme);
}}
>
Apply
</Button>
</div>
<div>
<Button
data-testid="button-refresh"
onClick={() => window.location.reload()}
>
Refresh
</Button>
</div>
</FormContainer>
</Section>
)}
{search !== undefined && (
<Section>
<SectionHeadline>Search Providers</SectionHeadline>
<>
<Text>Default Search Provider</Text>
<Code>{search.defaultProvider}</Code>
</>
<>
{search.providers && (
<Table>
<tbody>
<TableRow>
<HeadCell>Search Provider</HeadCell>
<HeadCell>Prefix</HeadCell>
</TableRow>
{search.providers.map(({ name, prefix }, index) => (
<TableRow key={name + index}>
<TableCell>{name}</TableCell>
<TableCell>{prefix}</TableCell>
2021-04-22 11:15:41 +02:00
</TableRow>
2022-02-13 20:57:26 +01:00
))}
</tbody>
</Table>
)}
</>
</Section>
)}
</ContentContainer>
</Modal>
);
2020-07-08 19:36:36 +02:00
};
2020-09-08 13:18:58 +02:00
export default Settings;