dashboard/src/components/settings.tsx

181 lines
4.9 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";
2021-07-14 01:18:11 +02:00
import { setTheme, IThemeProps, 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-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};
`;
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-14 01:18:28 +02:00
border-radius: 0;
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
interface ISettingsProps {
2021-07-14 01:18:28 +02:00
themes?: Array<IThemeProps>;
search?: ISearchProps;
}
2020-07-08 19:36:36 +02:00
2021-03-21 19:59:18 +01:00
/**
* Handles the settings-modal
* @param {Array<IThemeProps>} 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>();
const currentLightTheme = getTheme("light").label;
const currentDarkTheme = getTheme("dark").label;
2020-07-08 19:36:36 +02:00
if (themes || search) {
return (
2021-03-21 18:05:24 +01:00
<Modal element="icon" icon="settings" title="Settings">
{themes && (
2021-03-21 18:05:24 +01:00
<Section>
<SectionHeadline>Theme:</SectionHeadline>
<FormContainer>
2021-07-14 01:18:28 +02:00
<Table>
<tbody>
<TableRow>
<HeadCell>Light</HeadCell>
<HeadCell>Dark</HeadCell>
</TableRow>
<TableRow>
<TableCell>
<ThemeSelect
items={themes}
onChange={(theme: IThemeProps) =>
setNewLightTheme(theme)
}
current={currentLightTheme}
testId="light"
></ThemeSelect>
</TableCell>
<TableCell>
<ThemeSelect
items={themes}
onChange={(theme: IThemeProps) =>
setNewDarkTheme(theme)
}
current={currentDarkTheme}
testId="dark"
></ThemeSelect>
</TableCell>
</TableRow>
</tbody>
</Table>
</FormContainer>
2021-07-10 23:57:08 +02:00
<Button
data-testid="button-submit"
onClick={() => {
if (newLightTheme) setTheme("light", newLightTheme);
if (newDarkTheme) setTheme("dark", newDarkTheme);
}}
>
Apply
</Button>
<Button
data-testid="button-refresh"
onClick={() => window.location.reload()}
>
Refresh
</Button>
2021-03-21 18:05:24 +01:00
</Section>
)}
2021-04-22 11:15:41 +02:00
{search && (
2021-03-21 18:05:24 +01:00
<Section>
<SectionHeadline>Search Providers</SectionHeadline>
2021-04-22 11:15:41 +02:00
<>
<Text>Default Search Provider</Text>
<Code>{search.defaultProvider}</Code>
</>
<>
{search.providers && (
<Table>
<tbody>
<TableRow>
<HeadCell>Search Provider</HeadCell>
<HeadCell>Prefix</HeadCell>
</TableRow>
2021-06-30 00:56:01 +02:00
{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>
))}
</tbody>
</Table>
)}
2021-04-22 11:15:41 +02:00
</>
2021-03-21 18:05:24 +01:00
</Section>
)}
</Modal>
);
} else {
return <></>;
}
2020-07-08 19:36:36 +02:00
};
2020-09-08 13:18:58 +02:00
export default Settings;