diff --git a/components/AppShelf/AddAppShelfItem.tsx b/components/AppShelf/AddAppShelfItem.tsx index d8e619a9d..e78eccb92 100644 --- a/components/AppShelf/AddAppShelfItem.tsx +++ b/components/AppShelf/AddAppShelfItem.tsx @@ -10,9 +10,9 @@ import { AspectRatio, Text, Card, + LoadingOverlay, } from '@mantine/core'; -import { useForm } from '@mantine/hooks'; -import { UseForm } from '@mantine/hooks/lib/use-form/use-form'; +import { useForm } from '@mantine/form'; import { motion } from 'framer-motion'; import { useState } from 'react'; import { Apps } from 'tabler-icons-react'; @@ -72,16 +72,7 @@ export default function AddItemShelfItem(props: any) { ); } -function MatchIcon( - name: string, - form: UseForm<{ - type: any; - name: any; - icon: any; - url: any; - apiKey: any; - }> -) { +function MatchIcon(name: string, form: any) { // TODO: In order to avoid all the requests, we could fetch // https://data.jsdelivr.com/v1/package/gh/IceWhaleTech/AppIcon@main // and then iterate over the files -> files -> name and then remove the extension (.png) @@ -102,6 +93,8 @@ function MatchIcon( 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', @@ -110,6 +103,23 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & 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 ( @@ -152,44 +162,40 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & form.setFieldValue('icon', match); } }} - error={form.errors.name && 'Invalid name'} + error={form.errors.name && 'Invalid icon url'} /> { - form.setFieldValue('icon', event.currentTarget.value); - }} - error={form.errors.icon && 'Icon url is invalid'} + {...form.getInputProps('icon')} /> form.setFieldValue('url', event.currentTarget.value)} - error={form.errors.url && 'Service url is invalid'} + {...form.getInputProps('url')} />