From 583b6a94befcf24cf46ab64ec98be05d495e4b0f Mon Sep 17 00:00:00 2001 From: aliang <1098486429@qq.com> Date: Fri, 26 Apr 2024 12:04:18 +0800 Subject: [PATCH] fix(ui): update github providers (#1969) * fix(ui): update github providers * remove useless var --- .../github/components/github-form.tsx | 260 ++++++++++++------ .../components/provider-detail-form.tsx | 91 +----- .../github/new/components/new-page.tsx | 32 +-- 3 files changed, 193 insertions(+), 190 deletions(-) diff --git a/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/components/github-form.tsx b/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/components/github-form.tsx index a2015493a17b..2b1c11cee917 100644 --- a/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/components/github-form.tsx +++ b/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/components/github-form.tsx @@ -2,10 +2,25 @@ import * as React from 'react' import Link from 'next/link' +import { useRouter } from 'next/navigation' import { zodResolver } from '@hookform/resolvers/zod' +import { isEmpty } from 'lodash-es' import { useForm, UseFormReturn } from 'react-hook-form' +import { toast } from 'sonner' import * as z from 'zod' +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger +} from '@/components/ui/alert-dialog' +import { Button, buttonVariants } from '@/components/ui/button' import { Form, FormControl, @@ -15,7 +30,7 @@ import { FormLabel, FormMessage } from '@/components/ui/form' -import { IconExternalLink } from '@/components/ui/icons' +import { IconExternalLink, IconSpinner } from '@/components/ui/icons' import { Input } from '@/components/ui/input' export const createGithubProviderSchema = z.object({ @@ -41,8 +56,10 @@ export type UpdateGithubProviderFormValues = z.infer< interface GithubProviderFormProps { isNew?: boolean defaultValues?: Partial> - footer: React.ReactNode onSubmit: (values: any) => Promise + onDelete?: () => Promise + cancleable?: boolean + deletable?: boolean } export const GithubProviderForm = React.forwardRef< @@ -52,87 +69,172 @@ export const GithubProviderForm = React.forwardRef< > }, GithubProviderFormProps ->(({ isNew, defaultValues, footer, onSubmit }, ref) => { - const formSchema = isNew - ? createGithubProviderSchema - : updateGithubProviderSchema - const form = useForm>({ - resolver: zodResolver(formSchema), - defaultValues - }) +>( + ( + { isNew, defaultValues, onSubmit, onDelete, cancleable = true, deletable }, + ref + ) => { + const router = useRouter() + const formSchema = isNew + ? createGithubProviderSchema + : updateGithubProviderSchema + + const [deleteAlertVisible, setDeleteAlertVisible] = React.useState(false) + const [isDeleting, setIsDeleting] = React.useState(false) + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues + }) + const { isSubmitting, dirtyFields } = form.formState + const isDirty = !isEmpty(dirtyFields) + + const handleDeleteRepositoryProvider: React.MouseEventHandler< + HTMLButtonElement + > = async e => { + e.preventDefault() + if (!onDelete) return - React.useImperativeHandle( - ref, - () => { - return { - form + setIsDeleting(true) + try { + await onDelete() + } catch (error) { + toast.error('Failed to delete GitHub repository provider') + } finally { + setIsDeleting(false) } - }, - [form] - ) + } - return ( -
-
- - ( - - Display name - - A display name to help identifying different providers. - - - - - - - )} - /> - ( - - Personal Access Token - -
- Create a dedicated service user and generate a{' '} - - fine-grained personal access - {' '} - token with the member role for the organization or all - projects to be managed. -
-
• Contents (Read-only)
-
- - - + React.useImperativeHandle( + ref, + () => { + return { + form + } + }, + [form] + ) + + return ( + +
+ + ( + + Display name + + A display name to help identifying different providers. + + + + + + + )} + /> + ( + + Personal Access Token + +
+ Create a dedicated service user and generate a{' '} + + fine-grained personal access + {' '} + token with the member role for the organization or all + projects to be managed. +
+
• Contents (Read-only)
+
+ + + + +
+ )} + /> +
+
- - )} - /> - {footer} - -
- - ) -}) +
+
+ {cancleable && ( + + )} + {deletable && ( + + + + + + + + Are you absolutely sure? + + + This will delete the provider and remove any + repositories that have already been added to the + provider. + + + + Cancel + + {isDeleting && } + Yes, delete it + + + + + )} + +
+
+ +
+ + ) + } +) GithubProviderForm.displayName = 'GithubProviderForm' diff --git a/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/detail/components/provider-detail-form.tsx b/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/detail/components/provider-detail-form.tsx index 048ad7b6f7d8..617e94223b13 100644 --- a/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/detail/components/provider-detail-form.tsx +++ b/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/detail/components/provider-detail-form.tsx @@ -1,27 +1,12 @@ 'use client' import React from 'react' -import { isEmpty } from 'lodash-es' import { UseFormReturn } from 'react-hook-form' import { toast } from 'sonner' import * as z from 'zod' import { graphql } from '@/lib/gql/generates' import { useMutation } from '@/lib/tabby/gql' -import { - AlertDialog, - AlertDialogAction, - AlertDialogCancel, - AlertDialogContent, - AlertDialogDescription, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogTitle, - AlertDialogTrigger -} from '@/components/ui/alert-dialog' -import { Button, buttonVariants } from '@/components/ui/button' -import { FormMessage } from '@/components/ui/form' -import { IconSpinner } from '@/components/ui/icons' import { GithubProviderForm, @@ -61,11 +46,7 @@ export const UpdateProviderForm: React.FC = ({ const formRef = React.useRef<{ form: UseFormReturn }>(null) - const [deleteAlertVisible, setDeleteAlertVisible] = React.useState(false) - const [isDeleting, setIsDeleting] = React.useState(false) const form = formRef.current?.form - const isSubmitting = form?.formState?.isSubmitting - const isDirty = !isEmpty(form?.formState?.dirtyFields) const deleteGithubRepositoryProvider = useMutation( deleteGithubRepositoryProviderMutation @@ -94,24 +75,14 @@ export const UpdateProviderForm: React.FC = ({ }) } - const handleDeleteRepositoryProvider: React.MouseEventHandler< - HTMLButtonElement - > = async e => { - e.preventDefault() - setIsDeleting(true) - try { - const res = await deleteGithubRepositoryProvider({ id }) - if (res?.data?.deleteGithubRepositoryProvider) { - onDelete?.() - } else { - toast.error( - res?.error?.message || 'Failed to delete GitHub repository provider' - ) - } - } catch (error) { - toast.error('Failed to delete GitHub repository provider') - } finally { - setIsDeleting(false) + const handleDeleteRepositoryProvider = async () => { + const res = await deleteGithubRepositoryProvider({ id }) + if (res?.data?.deleteGithubRepositoryProvider) { + onDelete?.() + } else { + toast.error( + res?.error?.message || 'Failed to delete GitHub repository provider' + ) } } @@ -119,50 +90,10 @@ export const UpdateProviderForm: React.FC = ({ -
- -
-
- - - - - - - Are you absolutely sure? - - This will delete the provider and remove any repositories - that have already been added to the provider. - - - - Cancel - - {isDeleting && } - Yes, delete it - - - - - -
- - } onSubmit={onSubmit} + onDelete={handleDeleteRepositoryProvider} + deletable + cancleable={false} /> ) } diff --git a/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/new/components/new-page.tsx b/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/new/components/new-page.tsx index 83e971d7f74d..f3c94ce4d2b0 100644 --- a/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/new/components/new-page.tsx +++ b/ee/tabby-ui/app/(dashboard)/settings/(integrations)/repository/github/new/components/new-page.tsx @@ -8,9 +8,6 @@ import { toast } from 'sonner' import { graphql } from '@/lib/gql/generates' import { useMutation } from '@/lib/tabby/gql' -import { Button } from '@/components/ui/button' -import { FormMessage } from '@/components/ui/form' -import { IconSpinner } from '@/components/ui/icons' import { CreateGithubProviderFormValues, @@ -31,8 +28,6 @@ export const NewProvider = () => { const formRef = React.useRef<{ form: UseFormReturn }>(null) - const isSubmitting = formRef.current?.form?.formState?.isSubmitting - const createGithubRepositoryProviderMutation = useMutation( createGithubRepositoryProvider, { @@ -56,32 +51,7 @@ export const NewProvider = () => { return (
- -
- -
-
- - -
-
- } - onSubmit={handleSubmit} - /> + ) }