refactor: remove unused code and improve display view in status page

This commit is contained in:
moonrailgun 2024-09-16 00:18:28 +08:00
parent 427e9e3eb7
commit f5151aa2a4
3 changed files with 70 additions and 59 deletions

View File

@ -2,12 +2,12 @@ import React from 'react';
import { MonitorPicker } from '../MonitorPicker'; import { MonitorPicker } from '../MonitorPicker';
import { useTranslation } from '@i18next-toolkit/react'; import { useTranslation } from '@i18next-toolkit/react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { LuMinus, LuMinusCircle, LuPlus } from 'react-icons/lu'; import { LuMinusCircle, LuPlus } from 'react-icons/lu';
import { MarkdownEditor } from '@/components/MarkdownEditor'; import { MarkdownEditor } from '@/components/MarkdownEditor';
import { z } from 'zod'; import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { useEventWithLoading } from '@/hooks/useEvent'; import { useEventWithLoading } from '@/hooks/useEvent';
import { Input as AntdInput, Divider, Typography } from 'antd'; import { Input as AntdInput, Typography } from 'antd';
import { import {
Form, Form,
FormControl, FormControl,
@ -22,6 +22,7 @@ import { Controller, useFieldArray, useForm } from 'react-hook-form';
import { domainRegex, slugRegex } from '@tianji/shared'; import { domainRegex, slugRegex } from '@tianji/shared';
import { useElementSize } from '@/hooks/useResizeObserver'; import { useElementSize } from '@/hooks/useResizeObserver';
import { Switch } from '@/components/ui/switch'; import { Switch } from '@/components/ui/switch';
import { Separator } from '@/components/ui/separator';
const Text = Typography.Text; const Text = Typography.Text;
@ -140,7 +141,7 @@ export const MonitorStatusPageEditForm: React.FC<MonitorStatusPageEditFormProps>
name="description" name="description"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>{t('Description')}</FormLabel> <FormLabel optional={true}>{t('Description')}</FormLabel>
<FormControl> <FormControl>
<MarkdownEditorFormItem {...field} /> <MarkdownEditorFormItem {...field} />
</FormControl> </FormControl>
@ -155,7 +156,7 @@ export const MonitorStatusPageEditForm: React.FC<MonitorStatusPageEditFormProps>
name="domain" name="domain"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>{t('Custom Domain')}</FormLabel> <FormLabel optional={true}>{t('Custom Domain')}</FormLabel>
<FormControl> <FormControl>
<Input {...field} /> <Input {...field} />
</FormControl> </FormControl>
@ -178,51 +179,41 @@ export const MonitorStatusPageEditForm: React.FC<MonitorStatusPageEditFormProps>
render={() => ( render={() => (
<FormItem> <FormItem>
<FormLabel>{t('Monitor List')}</FormLabel> <FormLabel>{t('Monitor List')}</FormLabel>
{fields.map((field, i) => { {fields.map((field, i) => (
const { onChange: onMonitorChange, ...monitorProps } = <>
form.register(`monitorList.${i}.id`, { {i !== 0 && <Separator className="my-0.5" />}
onChange: (e) => console.log(e.target.value),
});
const { onChange: onShowCurrentChange, ...showCurrentProps } =
form.register(`monitorList.${i}.showCurrent`);
return ( <div key={field.key} className="mb-2 flex flex-col gap-1">
<> <Controller
{i !== 0 && <Divider className="my-0.5" />} control={form.control}
name={`monitorList.${i}.id`}
render={({ field }) => <MonitorPicker {...field} />}
/>
<div key={field.key} className="mb-2 flex flex-col gap-1"> <div className="flex flex-1 items-center">
<Controller <Controller
control={form.control} control={form.control}
name={`monitorList.${i}.id`} name={`monitorList.${i}.showCurrent`}
render={({ field }) => <MonitorPicker {...field} />} render={({ field }) => (
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
)}
/> />
<div className="flex flex-1 items-center"> <span className="ml-1 flex-1 align-middle text-sm">
<Controller {t('Show Latest Value')}
control={form.control} </span>
name={`monitorList.${i}.showCurrent`}
render={({ field }) => (
<Switch
{...showCurrentProps}
checked={field.value}
onCheckedChange={field.onChange}
/>
)}
/>
<span className="ml-1 flex-1 align-middle text-sm"> <LuMinusCircle
{t('Show Latest Value')} className="cursor-pointer text-lg"
</span> onClick={() => remove(i)}
/>
<LuMinusCircle
className="cursor-pointer text-lg"
onClick={() => remove(i)}
/>
</div>
</div> </div>
</> </div>
); </>
})} ))}
<FormMessage /> <FormMessage />
<Button <Button

View File

@ -13,7 +13,14 @@ import { useTranslation } from '@i18next-toolkit/react';
import { Link, useNavigate } from '@tanstack/react-router'; import { Link, useNavigate } from '@tanstack/react-router';
import { Helmet } from 'react-helmet'; import { Helmet } from 'react-helmet';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { MarkdownViewer } from '@/components/MarkdownEditor'; import { MarkdownViewer } from '@/components/MarkdownEditor';
import {
Sheet,
SheetContent,
SheetHeader,
SheetTrigger,
} from '@/components/ui/sheet';
interface MonitorStatusPageProps { interface MonitorStatusPageProps {
slug: string; slug: string;
@ -83,6 +90,26 @@ export const MonitorStatusPage: React.FC<MonitorStatusPageProps> = React.memo(
} }
); );
const editBtn = (
<Sheet open={editMode} onOpenChange={setEditMode}>
<SheetTrigger>
<Button>{t('Edit')}</Button>
</SheetTrigger>
<SheetContent side="right" className="overflow-auto">
<SheetHeader>{t('Modify Status Page Info')}</SheetHeader>
<Separator className="my-4" />
<MonitorStatusPageEditForm
isLoading={loading}
initialValues={initialValues}
onFinish={handleSave}
onCancel={() => setEditMode(false)}
/>
</SheetContent>
</Sheet>
);
return ( return (
<div className="flex h-full w-full"> <div className="flex h-full w-full">
<Helmet> <Helmet>
@ -92,21 +119,9 @@ export const MonitorStatusPage: React.FC<MonitorStatusPageProps> = React.memo(
</title> </title>
</Helmet> </Helmet>
{editMode && (
<div className="w-1/3 overflow-auto border-r border-gray-300 px-4 py-8 dark:border-gray-600">
<MonitorStatusPageEditForm
isLoading={loading}
initialValues={initialValues}
onFinish={handleSave}
onCancel={() => setEditMode(false)}
/>
</div>
)}
<div <div
className={clsx( className={clsx(
'mx-auto overflow-auto px-4 py-8', 'mx-auto w-full overflow-auto px-4 py-8 md:w-4/5 xl:w-3/5'
!editMode ? 'w-full' : 'w-2/3'
)} )}
> >
<div className="flex"> <div className="flex">
@ -115,11 +130,11 @@ export const MonitorStatusPage: React.FC<MonitorStatusPageProps> = React.memo(
<ColorSchemeSwitcher /> <ColorSchemeSwitcher />
</div> </div>
{allowEdit && !editMode && ( {allowEdit && (
<div className="mb-4 flex gap-2"> <div className="mb-4 flex gap-2">
<Button onClick={() => setEditMode(true)}>{t('Edit')}</Button> {editBtn}
{showBackBtn && ( {showBackBtn && !editMode && (
<Link to="/"> <Link to="/">
<Button variant="outline">{t('Back to Admin')}</Button> <Button variant="outline">{t('Back to Admin')}</Button>
</Link> </Link>

View File

@ -12,6 +12,7 @@ import {
import { cn } from "@/utils/style" import { cn } from "@/utils/style"
import { Label } from "@/components/ui/label" import { Label } from "@/components/ui/label"
import { useTranslation } from "@i18next-toolkit/react"
const Form = FormProvider const Form = FormProvider
@ -86,9 +87,10 @@ FormItem.displayName = "FormItem"
const FormLabel = React.forwardRef< const FormLabel = React.forwardRef<
React.ElementRef<typeof LabelPrimitive.Root>, React.ElementRef<typeof LabelPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> & {optional?: boolean}
>(({ className, ...props }, ref) => { >(({ className, ...props }, ref) => {
const { error, formItemId } = useFormField() const { error, formItemId } = useFormField()
const { t } = useTranslation()
return ( return (
<Label <Label
@ -96,7 +98,10 @@ const FormLabel = React.forwardRef<
className={cn(error && "text-destructive", className)} className={cn(error && "text-destructive", className)}
htmlFor={formItemId} htmlFor={formItemId}
{...props} {...props}
/> >
{props.children}
{props.optional && <span className="text-xs opacity-40 ml-1">{t('Optional')}</span>}
</Label>
) )
}) })
FormLabel.displayName = "FormLabel" FormLabel.displayName = "FormLabel"