From 91f636ca9775c3288c283f49f18b40e7584c8a83 Mon Sep 17 00:00:00 2001 From: Aj - Thomas Date: Thu, 12 May 2022 19:28:10 +0200 Subject: [PATCH 1/7] Basic backend support and config loading from file --- components/Config/SelectConfig.tsx | 16 ++++++ data/configs/default.json | 16 ++++++ package.json | 1 + pages/_app.tsx | 4 +- pages/api/configs/[slug].ts | 61 +++++++++++++++++++++++ pages/api/configs/index.ts | 28 +++++++++++ pages/tryconfig.tsx | 52 +++++++++++++++++++ tools/state.tsx | 80 ++++++++++++++---------------- tools/types.ts | 1 + yarn.lock | 22 ++++++++ 10 files changed, 235 insertions(+), 46 deletions(-) create mode 100644 components/Config/SelectConfig.tsx create mode 100644 data/configs/default.json create mode 100644 pages/api/configs/[slug].ts create mode 100644 pages/api/configs/index.ts create mode 100644 pages/tryconfig.tsx diff --git a/components/Config/SelectConfig.tsx b/components/Config/SelectConfig.tsx new file mode 100644 index 000000000..4abd659fb --- /dev/null +++ b/components/Config/SelectConfig.tsx @@ -0,0 +1,16 @@ +import { Select } from '@mantine/core'; +import { useState } from 'react'; + +export default function SelectConfig(props: any) { + const [value, setValue] = useState(''); + return ( + { + loadConfig(e ?? 'default'); + }} + data={ + // If config list is empty, return the current config + configList.length === 0 ? [config.name] : configList + } + /> + + + ); } diff --git a/tools/state.tsx b/tools/state.tsx index 6c1d25754..64a99b48d 100644 --- a/tools/state.tsx +++ b/tools/state.tsx @@ -9,6 +9,7 @@ type configContextType = { config: Config; setConfig: (newconfig: Config) => void; loadConfig: (name: string) => void; + getConfigs: () => Promise; }; const configContext = createContext({ @@ -23,6 +24,7 @@ const configContext = createContext({ }, setConfig: () => {}, loadConfig: async (name: string) => {}, + getConfigs: async () => [], }); export function useConfig() { @@ -40,14 +42,7 @@ type Props = { export function ConfigProvider({ children }: Props) { const [config, setConfigInternal] = useState({ name: 'default', - services: [ - { - type: 'Other', - name: 'example', - icon: 'https://c.tenor.com/o656qFKDzeUAAAAC/rick-astley-never-gonna-give-you-up.gif', - url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', - }, - ], + services: [], settings: { searchBar: true, searchUrl: 'https://www.google.com/search?q=', @@ -58,7 +53,6 @@ export function ConfigProvider({ children }: Props) { async function loadConfig(configName: string) { try { const response = await axios.get(`/api/configs/${configName}`); - console.log('response', response); setConfigInternal(response.data); showNotification({ title: 'Config', @@ -85,10 +79,16 @@ export function ConfigProvider({ children }: Props) { setConfigInternal(newconfig); } + async function getConfigs(): Promise { + const response = await axios.get('/api/configs'); + return response.data; + } + const value = { config, setConfig, loadConfig, + getConfigs, }; return {children}; } From 98e4da5a3b6f92a3dbba02bfd0fdba1ea41fc2d0 Mon Sep 17 00:00:00 2001 From: Aj - Thomas Date: Thu, 12 May 2022 21:57:01 +0200 Subject: [PATCH 3/7] =?UTF-8?q?Remove=20JSON=20and=20add=20=F0=9F=8D=AA=20?= =?UTF-8?q?Tryconfig=20save=20cookies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/configs/default.json | 2 +- data/configs/low.json | 16 ---------------- data/configs/undefined.json | 3 --- pages/tryconfig.tsx | 1 + 4 files changed, 2 insertions(+), 20 deletions(-) delete mode 100644 data/configs/low.json delete mode 100644 data/configs/undefined.json diff --git a/data/configs/default.json b/data/configs/default.json index 57f8faa53..9e1d1669a 100644 --- a/data/configs/default.json +++ b/data/configs/default.json @@ -2,7 +2,7 @@ "name": "default", "services": [ { - "type": "Other hello", + "type": "Other", "name": "example", "icon": "https://c.tenor.com/o656qFKDzeUAAAAC/rick-astley-never-gonna-give-you-up.gif", "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ" diff --git a/data/configs/low.json b/data/configs/low.json deleted file mode 100644 index c3482808d..000000000 --- a/data/configs/low.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "low", - "services": [ - { - "type": "I am ", - "name": "example", - "icon": "https://c.tenor.com/o656qFKDzeUAAAAC/rick-astley-never-gonna-give-you-up.gif", - "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ" - } - ], - "settings": { - "searchBar": true, - "searchUrl": "https://www.google.com/search?q=", - "enabledModules": [] - } -} \ No newline at end of file diff --git a/data/configs/undefined.json b/data/configs/undefined.json deleted file mode 100644 index c92e56872..000000000 --- a/data/configs/undefined.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "true": "" -} \ No newline at end of file diff --git a/pages/tryconfig.tsx b/pages/tryconfig.tsx index 37d2c2565..6fb38734c 100644 --- a/pages/tryconfig.tsx +++ b/pages/tryconfig.tsx @@ -79,6 +79,7 @@ export default function TryConfig(props: any) { 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 From 84b7d4bbdcd1840d798a11e6a8a22b89f048290d Mon Sep 17 00:00:00 2001 From: Aj - Thomas Date: Thu, 12 May 2022 22:11:28 +0200 Subject: [PATCH 4/7] Fix cookie in Upload form and fix download name --- components/Config/LoadConfig.tsx | 11 +++++++++-- components/Config/SaveConfig.tsx | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/components/Config/LoadConfig.tsx b/components/Config/LoadConfig.tsx index a590e20ac..d36fecd19 100644 --- a/components/Config/LoadConfig.tsx +++ b/components/Config/LoadConfig.tsx @@ -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'; @@ -69,15 +70,21 @@ export default function LoadConfigComponent(props: any) { }); return; } + const newConfig: Config = JSON.parse(e); showNotification({ autoClose: 5000, radius: 'md', - title: Config loaded successfully, + title: ( + + Config {newConfig.name} loaded successfully + + ), color: 'green', icon: , message: undefined, }); - setConfig(JSON.parse(e)); + setCookies('config-name', newConfig.name, { maxAge: 60 * 60 * 24 * 30 }); + setConfig(newConfig); }); }} accept={['application/json']} diff --git a/components/Config/SaveConfig.tsx b/components/Config/SaveConfig.tsx index d12f7ad22..cfaf196d4 100644 --- a/components/Config/SaveConfig.tsx +++ b/components/Config/SaveConfig.tsx @@ -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 ( From ff2fd2febdedfdfb5a3adf8be5dc1e33796fa58a Mon Sep 17 00:00:00 2001 From: Aj - Thomas Date: Thu, 12 May 2022 22:16:22 +0200 Subject: [PATCH 5/7] Add a ConfigChanger in the settings menu --- components/Config/ConfigChanger.tsx | 31 ++++++++++++++++++++++++++++ components/Settings/SettingsMenu.tsx | 2 ++ 2 files changed, 33 insertions(+) create mode 100644 components/Config/ConfigChanger.tsx diff --git a/components/Config/ConfigChanger.tsx b/components/Config/ConfigChanger.tsx new file mode 100644 index 000000000..0fc531b05 --- /dev/null +++ b/components/Config/ConfigChanger.tsx @@ -0,0 +1,31 @@ +import { Loader, Select } 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 ; + } + return ( + Date: Thu, 12 May 2022 22:35:21 +0200 Subject: [PATCH 7/7] Fix default search engine in settings menu --- components/Settings/SettingsMenu.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/Settings/SettingsMenu.tsx b/components/Settings/SettingsMenu.tsx index 99afc8a98..b8fed02eb 100644 --- a/components/Settings/SettingsMenu.tsx +++ b/components/Settings/SettingsMenu.tsx @@ -42,9 +42,9 @@ function SettingsMenu(props: any) { 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