import { useMantineTheme, Modal, Center, Group, TextInput, Image, Button, Select, AspectRatio, Text, Card, LoadingOverlay, } from '@mantine/core'; import { useForm } from '@mantine/form'; import { motion } from 'framer-motion'; import { useState } from 'react'; import { Apps } from 'tabler-icons-react'; import { useConfig } from '../../tools/state'; 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 ( <> setOpened(false)} title="Add a service" > Add a service setOpened(true)} size={60} /> ); } function MatchIcon(name: string, form: any) { fetch( `https://cdn.jsdelivr.net/gh/walkxhub/dashboard-icons/png/${name .replace(/\s+/g, '-') .toLowerCase()}.png` ) .then((res) => { if (res.status === 200) { form.setFieldValue('icon', res.url); } }) .catch((e) => { // Do nothing }); return false; } export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & any) { const { setOpened } = props; const { addService, config, setConfig } = useConfig(); const [isLoading, setLoading] = useState(false); const form = useForm({ initialValues: { type: props.type ?? 'Other', name: props.name ?? '', icon: props.icon ?? '', url: props.url ?? '', apiKey: props.apiKey ?? (undefined as unknown as string), }, validate: { apiKey: (value: string) => null, // Validate icon with a regex icon: (value: string) => { if (!value.match(/^https?:\/\/.+\.(png|jpg|jpeg|gif)$/)) { return 'Please enter a valid icon URL'; } return null; }, // Validate url with a regex http/https url: (value: string) => { if (!value.match(/^https?:\/\/.+\/$/)) { return 'Please enter a valid URL (that ends with a /)'; } return null; }, }, }); return ( <>
Placeholder
{ // If service already exists, update it. if (config.services && config.services.find((s) => s.name === form.values.name)) { setConfig({ ...config, services: config.services.map((s) => { if (s.name === form.values.name) { return { ...form.values, }; } return s; }), }); } else { addService(form.values); } setOpened(false); form.reset(); })} > { form.setFieldValue('name', event.currentTarget.value); const match = MatchIcon(event.currentTarget.value, form); if (match) { form.setFieldValue('icon', match); } }} error={form.errors.name && 'Invalid icon url'} />