Merge pull request #61 from ajnart/multiple-configs

Multiple configs
This commit is contained in:
Aj - Thomas
2022-05-13 02:37:33 +02:00
committed by GitHub
19 changed files with 429 additions and 96 deletions

View File

@@ -1,5 +1,4 @@
import {
useMantineTheme,
Modal,
Center,
Group,
@@ -21,9 +20,7 @@ import { ServiceTypeList } from '../../tools/types';
import { AppShelfItemWrapper } from './AppShelfItemWrapper';
export default function AddItemShelfItem(props: any) {
const { addService } = useConfig();
const [opened, setOpened] = useState(false);
const theme = useMantineTheme();
return (
<>
<Modal
@@ -83,7 +80,7 @@ function MatchIcon(name: string, form: any) {
form.setFieldValue('icon', res.url);
}
})
.catch((e) => {
.catch(() => {
// Do nothing
});
@@ -92,7 +89,7 @@ function MatchIcon(name: string, form: any) {
export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & any) {
const { setOpened } = props;
const { addService, config, setConfig } = useConfig();
const { config, setConfig } = useConfig();
const [isLoading, setLoading] = useState(false);
const form = useForm({
@@ -104,7 +101,7 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } &
apiKey: props.apiKey ?? (undefined as unknown as string),
},
validate: {
apiKey: (value: string) => null,
apiKey: () => null,
// Validate icon with a regex
icon: (value: string) => {
if (!value.match(/^https?:\/\/.+\.(png|jpg|jpeg|gif)$/)) {
@@ -143,7 +140,10 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } &
}),
});
} else {
addService(form.values);
setConfig({
...config,
services: [...config.services, form.values],
});
}
setOpened(false);
form.reset();

View File

@@ -1,35 +1,18 @@
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { motion } from 'framer-motion';
import {
Text,
AspectRatio,
SimpleGrid,
Card,
useMantineTheme,
Image,
Group,
Space,
} from '@mantine/core';
import { Text, AspectRatio, SimpleGrid, Card, Image, Group, Space } from '@mantine/core';
import { useConfig } from '../../tools/state';
import { serviceItem } from '../../tools/types';
import AddItemShelfItem from './AddAppShelfItem';
import { AppShelfItemWrapper } from './AppShelfItemWrapper';
import AppShelfMenu from './AppShelfMenu';
const AppShelf = (props: any) => {
const { config, addService, removeService, setConfig } = useConfig();
/* A hook that is used to load the config from local storage. */
useEffect(() => {
const localConfig = localStorage.getItem('config');
if (localConfig) {
setConfig(JSON.parse(localConfig));
}
}, []);
const AppShelf = () => {
const { config } = useConfig();
return (
<SimpleGrid m="xl" cols={5} spacing="xl">
{config.services.map((service, i) => (
{config.services.map((service) => (
<AppShelfItem key={service.name} service={service} />
))}
<AddItemShelfItem />
@@ -39,16 +22,14 @@ const AppShelf = (props: any) => {
export function AppShelfItem(props: any) {
const { service }: { service: serviceItem } = props;
const theme = useMantineTheme();
const { removeService } = useConfig();
const [hovering, setHovering] = useState(false);
return (
<motion.div
key={service.name}
onHoverStart={(e) => {
onHoverStart={() => {
setHovering(true);
}}
onHoverEnd={(e) => {
onHoverEnd={() => {
setHovering(false);
}}
>
@@ -79,7 +60,7 @@ export function AppShelfItem(props: any) {
opacity: hovering ? 1 : 0,
}}
>
<AppShelfMenu service={service} removeitem={removeService} />
<AppShelfMenu service={service} />
</motion.div>
</Group>
</Card.Section>

View File

@@ -2,10 +2,12 @@ import { Menu, Modal, Text } from '@mantine/core';
import { showNotification } from '@mantine/notifications';
import { useState } from 'react';
import { Check, Edit, Trash } from 'tabler-icons-react';
import { useConfig } from '../../tools/state';
import { AddAppShelfItemForm } from './AddAppShelfItem';
export default function AppShelfMenu(props: any) {
const { service, removeitem: removeItem } = props;
const { service } = props;
const { config, setConfig } = useConfig();
const [opened, setOpened] = useState(false);
return (
<>
@@ -40,7 +42,10 @@ export default function AppShelfMenu(props: any) {
<Menu.Item
color="red"
onClick={(e: any) => {
removeItem(service.name);
setConfig({
...config,
services: config.services.filter((s) => s.name !== service.name),
});
showNotification({
autoClose: 5000,
title: (

View File

@@ -0,0 +1,37 @@
import { Center, Loader, Select, Tooltip } from '@mantine/core';
import { setCookies } from 'cookies-next';
import { useEffect, useState } from 'react';
import { useConfig } from '../../tools/state';
export default function ConfigChanger() {
const { config, loadConfig, setConfig, getConfigs } = useConfig();
const [configList, setConfigList] = useState([] as string[]);
useEffect(() => {
getConfigs().then((configs) => setConfigList(configs));
// setConfig(initialConfig);
}, [config]);
// If configlist is empty, return a loading indicator
if (configList.length === 0) {
return (
<Center>
<Tooltip label={"Loading your configs. This doesn't load in vercel."}>
<Loader />
</Tooltip>
</Center>
);
}
return (
<Select
defaultValue={config.name}
label="Config loader"
onChange={(e) => {
loadConfig(e ?? 'default');
setCookies('config-name', e ?? 'default', { maxAge: 60 * 60 * 24 * 30 });
}}
data={
// If config list is empty, return the current config
configList.length === 0 ? [config.name] : configList
}
/>
);
}

View File

@@ -4,6 +4,7 @@ import { DropzoneStatus, FullScreenDropzone } from '@mantine/dropzone';
import { showNotification } from '@mantine/notifications';
import { useRef } from 'react';
import { useRouter } from 'next/router';
import { setCookies } from 'cookies-next';
import { useConfig } from '../../tools/state';
import { Config } from '../../tools/types';
@@ -48,7 +49,7 @@ export const dropzoneChildren = (status: DropzoneStatus, theme: MantineTheme) =>
);
export default function LoadConfigComponent(props: any) {
const { saveConfig, setConfig } = useConfig();
const { setConfig } = useConfig();
const theme = useMantineTheme();
const router = useRouter();
const openRef = useRef<() => void>();
@@ -69,15 +70,21 @@ export default function LoadConfigComponent(props: any) {
});
return;
}
const newConfig: Config = JSON.parse(e);
showNotification({
autoClose: 5000,
radius: 'md',
title: <Text>Config loaded successfully</Text>,
title: (
<Text>
Config <b>{newConfig.name}</b> loaded successfully
</Text>
),
color: 'green',
icon: <Check />,
message: undefined,
});
setConfig(JSON.parse(e));
setCookies('config-name', newConfig.name, { maxAge: 60 * 60 * 24 * 30 });
setConfig(newConfig);
});
}}
accept={['application/json']}

View File

@@ -7,7 +7,7 @@ export default function SaveConfigComponent(props: any) {
const { config } = useConfig();
function onClick(e: any) {
if (config) {
fileDownload(JSON.stringify(config, null, '\t'), 'config.json');
fileDownload(JSON.stringify(config, null, '\t'), `${config.name}.json`);
}
}
return (

View File

@@ -0,0 +1,16 @@
import { Select } from '@mantine/core';
import { useState } from 'react';
export default function SelectConfig(props: any) {
const [value, setValue] = useState<string | null>('');
return (
<Select
value={value}
onChange={setValue}
data={[
{ value: 'default', label: 'Default' },
{ value: 'yourmom', label: 'Your mom' },
]}
/>
);
}

View File

@@ -16,6 +16,7 @@ import { AlertCircle, Settings as SettingsIcon } from 'tabler-icons-react';
import { CURRENT_VERSION, REPO_URL } from '../../data/constants';
import { useConfig } from '../../tools/state';
import { ColorSchemeSwitch } from '../ColorSchemeToggle/ColorSchemeSwitch';
import ConfigChanger from '../Config/ConfigChanger';
import SaveConfigComponent from '../Config/SaveConfig';
import ModuleEnabler from './ModuleEnabler';
@@ -28,7 +29,6 @@ function SettingsMenu(props: any) {
{ label: 'DuckDuckGo', value: 'https://duckduckgo.com/?q=' },
{ label: 'Bing', value: 'https://bing.com/search?q=' },
];
return (
<Group direction="column" grow>
<Alert
@@ -42,9 +42,9 @@ function SettingsMenu(props: any) {
<Group>
<SegmentedControl
title="Search engine"
defaultValue={
value={
// Match config.settings.searchUrl with a key in the matches array
matches.find((match) => match.value === config.settings.searchUrl)?.value || 'Google'
matches.find((match) => match.value === config.settings.searchUrl)?.value ?? 'Google'
}
onChange={
// Set config.settings.searchUrl to the value of the selected item
@@ -79,6 +79,7 @@ function SettingsMenu(props: any) {
</Group>
<ModuleEnabler />
<ColorSchemeSwitch />
<ConfigChanger />
<SaveConfigComponent />
<Text
style={{