dashboard/src/components/settings.tsx

178 lines
4.6 KiB
TypeScript
Raw Normal View History

import React, { useState } from "react";
2020-07-08 19:36:36 +02:00
import styled from "styled-components";
2021-03-23 16:26:14 +01:00
import Select, { Styles, ValueType } from "react-select";
2020-07-08 19:36:36 +02:00
import { ISearchProviderProps } from "./searchBar";
2021-03-21 18:05:24 +01:00
import selectedTheme, { setTheme, IThemeProps } from "../lib/theme";
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-03-21 18:05:24 +01:00
interface IHoverProps {
color?: string;
backgroundColor?: string;
border?: string;
borderColor?: string;
}
2020-07-08 19:36:36 +02:00
2021-03-21 18:05:24 +01:00
interface IPseudoProps extends React.CSSProperties {
"&:hover": IHoverProps
}
2020-07-08 19:36:36 +02:00
const FormContainer = styled.div`
display: grid;
grid-template-columns: auto auto auto;
`;
const Table = styled.table`
font-weight: 400;
background: none;
color: ${selectedTheme.mainColor};
`;
const TableRow = styled.tr`
border-bottom: 1px solid ${selectedTheme.mainColor};
`;
const TableCell = styled.td`
background: none;
padding-top: 0.5rem;
`;
const HeadCell = styled.th`
font-weight: 700;
background: none;
`;
2021-03-21 18:05:24 +01:00
const Section = styled.div`
padding: 1rem 0;
`;
const SectionHeadline = styled(SubHeadline)`
width: 100%;
border-bottom: 1px solid ${selectedTheme.accentColor};
margin-bottom: 0.5rem;
`;
const SelectorStyle: Partial<Styles<IThemeProps, false>> = {
indicatorSeparator: () => ({
display: "none",
}),
container: (base: React.CSSProperties): React.CSSProperties => ({
...base,
margin: "0 2px",
}),
dropdownIndicator: (base: React.CSSProperties): IPseudoProps => ({
...base,
color: selectedTheme.mainColor,
"&:hover": {
color: selectedTheme.mainColor
}
}),
control: (base: React.CSSProperties): IPseudoProps => ({
...base,
fontWeight: 500,
2020-07-08 19:36:36 +02:00
color: selectedTheme.mainColor,
textTransform: "uppercase",
width: "12rem",
background: "none",
2021-03-21 18:05:24 +01:00
borderRadius: 0,
border: "1px solid",
borderColor: selectedTheme.mainColor,
boxShadow: "none",
2020-07-08 19:36:36 +02:00
"&:hover": {
2021-03-21 18:05:24 +01:00
border: "1px solid",
borderColor: selectedTheme.mainColor
2020-07-10 00:04:47 +02:00
},
2020-07-08 19:36:36 +02:00
}),
2021-03-21 18:05:24 +01:00
menu: (base: React.CSSProperties): React.CSSProperties => ({
...base,
2020-07-08 19:36:36 +02:00
backgroundColor: selectedTheme.backgroundColor,
border: "1px solid " + selectedTheme.mainColor,
borderRadius: 0,
2021-03-21 18:05:24 +01:00
boxShadow: "none",
margin: "4px 0"
2020-07-08 19:36:36 +02:00
}),
2021-03-21 18:05:24 +01:00
option: (base: React.CSSProperties): IPseudoProps => ({
...base,
fontWeight: 500,
2020-07-08 19:36:36 +02:00
color: selectedTheme.mainColor,
textTransform: "uppercase",
borderRadius: 0,
2021-03-21 18:05:24 +01:00
boxShadow: "none",
2020-07-08 19:36:36 +02:00
backgroundColor: selectedTheme.backgroundColor,
"&:hover": {
backgroundColor: selectedTheme.mainColor,
2020-07-10 00:04:47 +02:00
color: selectedTheme.backgroundColor,
},
2020-07-08 19:36:36 +02:00
}),
2021-03-21 18:05:24 +01:00
singleValue: (base: React.CSSProperties): React.CSSProperties => ({
...base,
color: selectedTheme.mainColor,
}),
2020-07-08 19:36:36 +02:00
};
interface ISettingsProps {
themes: Array<IThemeProps> | undefined;
providers: Array<ISearchProviderProps> | undefined;
}
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
* @param {Array<ISearchProviderProps>} providers - the list of search providers
*/
const Settings = ({ themes, providers }: ISettingsProps) => {
2021-03-23 16:26:14 +01:00
const [newTheme, setNewTheme] = useState<IThemeProps>();
2020-07-08 19:36:36 +02:00
if (themes && providers) {
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>
<Select
options={themes}
defaultValue={selectedTheme}
2021-03-23 16:26:14 +01:00
onChange={(e: ValueType<IThemeProps, false>) => {
if (e !== null && e !== undefined) setNewTheme(e);
}}
styles={SelectorStyle}
/>
<Button onClick={() => setTheme(JSON.stringify(newTheme))}>
Apply
</Button>
<Button onClick={() => window.location.reload()}>Refresh</Button>
</FormContainer>
2021-03-21 18:05:24 +01:00
</Section>
)}
{providers && (
2021-03-21 18:05:24 +01:00
<Section>
<SectionHeadline>Search Providers</SectionHeadline>
<Table>
<tbody>
<TableRow>
<HeadCell>Search Provider</HeadCell>
<HeadCell>Prefix</HeadCell>
</TableRow>
2021-03-21 18:05:24 +01:00
{providers.map((provider, index) => (
<TableRow key={provider.name + index}>
<TableCell>{provider.name}</TableCell>
<TableCell>{provider.prefix}</TableCell>
</TableRow>
))}
</tbody>
</Table>
</Section>
)}
</Modal>
);
} else {
return <></>;
}
2020-07-08 19:36:36 +02:00
};
2020-09-08 13:18:58 +02:00
export default Settings;