diff --git a/src/components/Config/ConfigChanger.tsx b/src/components/Config/ConfigChanger.tsx index 4826fcdd8..19a88b179 100644 --- a/src/components/Config/ConfigChanger.tsx +++ b/src/components/Config/ConfigChanger.tsx @@ -1,12 +1,12 @@ import { Center, Dialog, Loader, Notification, Select, Tooltip } from '@mantine/core'; import { useToggle } from '@mantine/hooks'; -import { useQuery } from '@tanstack/react-query'; +import { notifications } from '@mantine/notifications'; +import { IconCheck } from '@tabler/icons-react'; import { setCookie } from 'cookies-next'; import { useTranslation } from 'next-i18next'; import { useRouter } from 'next/router'; import { useState } from 'react'; -import { notifications } from '@mantine/notifications'; -import { IconCheck } from '@tabler/icons-react'; +import { api } from '~/utils/api'; import { useConfigContext } from '../../config/provider'; export default function ConfigChanger() { @@ -95,10 +95,4 @@ export default function ConfigChanger() { ); } -const useConfigsQuery = () => - useQuery({ - queryKey: ['config/get-all'], - queryFn: fetchConfigs, - }); - -const fetchConfigs = async () => (await (await fetch('/api/configs')).json()) as string[]; +const useConfigsQuery = () => api.config.all.useQuery(); diff --git a/src/server/api/root.ts b/src/server/api/root.ts index 2f8a5e08b..38b89870f 100644 --- a/src/server/api/root.ts +++ b/src/server/api/root.ts @@ -1,6 +1,7 @@ import { createTRPCRouter } from '~/server/api/trpc'; import { appRouter } from './routers/app'; import { rssRouter } from './routers/rss'; +import { configRouter } from './routers/config'; /** * This is the primary router for your server. @@ -10,6 +11,7 @@ import { rssRouter } from './routers/rss'; export const rootRouter = createTRPCRouter({ app: appRouter, rss: rssRouter, + config: configRouter, }); // export type definition of API diff --git a/src/server/api/routers/config.ts b/src/server/api/routers/config.ts new file mode 100644 index 000000000..d695f7e7f --- /dev/null +++ b/src/server/api/routers/config.ts @@ -0,0 +1,12 @@ +import fs from 'fs'; +import { createTRPCRouter, publicProcedure } from '../trpc'; + +export const configRouter = createTRPCRouter({ + all: publicProcedure.query(async () => { + // Get all the configs in the /data/configs folder + // All the files that end in ".json" + const files = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json')); + // Strip the .json extension from the file name + return files.map((file) => file.replace('.json', '')); + }), +});