|
| 1 | +import React, { useMemo, useState } from 'react'; |
| 2 | +import { Button } from '../ui/button'; |
| 3 | +import { |
| 4 | + Dialog, |
| 5 | + DialogContent, |
| 6 | + DialogDescription, |
| 7 | + DialogFooter, |
| 8 | + DialogHeader, |
| 9 | + DialogTitle, |
| 10 | + DialogTrigger, |
| 11 | +} from '../ui/dialog'; |
| 12 | +import { useTranslation } from '@i18next-toolkit/react'; |
| 13 | +import { TbBuildingLighthouse } from 'react-icons/tb'; |
| 14 | +import { |
| 15 | + Sheet, |
| 16 | + SheetContent, |
| 17 | + SheetDescription, |
| 18 | + SheetHeader, |
| 19 | + SheetTitle, |
| 20 | + SheetTrigger, |
| 21 | +} from '../ui/sheet'; |
| 22 | +import { defaultErrorHandler, defaultSuccessHandler, trpc } from '@/api/trpc'; |
| 23 | +import { useCurrentWorkspaceId } from '@/store/user'; |
| 24 | +import { formatDate } from '@/utils/date'; |
| 25 | +import { Input } from '../ui/input'; |
| 26 | +import { toast } from 'sonner'; |
| 27 | +import { useEvent } from '@/hooks/useEvent'; |
| 28 | +import { Badge } from '../ui/badge'; |
| 29 | +import { LuArrowRight, LuPlus } from 'react-icons/lu'; |
| 30 | + |
| 31 | +interface WebsiteLighthouseBtnProps { |
| 32 | + websiteId: string; |
| 33 | +} |
| 34 | +export const WebsiteLighthouseBtn: React.FC<WebsiteLighthouseBtnProps> = |
| 35 | + React.memo((props) => { |
| 36 | + const workspaceId = useCurrentWorkspaceId(); |
| 37 | + const { t } = useTranslation(); |
| 38 | + const [url, setUrl] = useState(''); |
| 39 | + const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false); |
| 40 | + |
| 41 | + const { data, hasNextPage, fetchNextPage, isFetchingNextPage, refetch } = |
| 42 | + trpc.website.getLighthouseReport.useInfiniteQuery( |
| 43 | + { |
| 44 | + workspaceId, |
| 45 | + websiteId: props.websiteId, |
| 46 | + }, |
| 47 | + { |
| 48 | + getNextPageParam: (lastPage) => lastPage.nextCursor, |
| 49 | + } |
| 50 | + ); |
| 51 | + const createMutation = trpc.website.generateLighthouseReport.useMutation({ |
| 52 | + onSuccess: defaultSuccessHandler, |
| 53 | + onError: defaultErrorHandler, |
| 54 | + }); |
| 55 | + |
| 56 | + const allData = useMemo(() => { |
| 57 | + if (!data) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + return [...data.pages.flatMap((p) => p.items)]; |
| 62 | + }, [data]); |
| 63 | + |
| 64 | + const handleGenerateReport = useEvent(async () => { |
| 65 | + if (!url) { |
| 66 | + toast.error(t('Url is required')); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + await createMutation.mutateAsync({ |
| 71 | + workspaceId, |
| 72 | + websiteId: props.websiteId, |
| 73 | + url, |
| 74 | + }); |
| 75 | + setIsCreateDialogOpen(false); |
| 76 | + refetch(); |
| 77 | + }); |
| 78 | + |
| 79 | + return ( |
| 80 | + <Sheet> |
| 81 | + <SheetTrigger> |
| 82 | + <Button variant="outline" size="icon" Icon={TbBuildingLighthouse} /> |
| 83 | + </SheetTrigger> |
| 84 | + <SheetContent> |
| 85 | + <SheetHeader> |
| 86 | + <SheetTitle>{t('Website Lighthouse Reports')}</SheetTitle> |
| 87 | + <SheetDescription> |
| 88 | + {t( |
| 89 | + 'Lighthouse is an open-source, automated tool developed by Google, designed to evaluate the quality of web applications.' |
| 90 | + )} |
| 91 | + </SheetDescription> |
| 92 | + </SheetHeader> |
| 93 | + |
| 94 | + <div className="mt-2 flex flex-col gap-2"> |
| 95 | + <div> |
| 96 | + <Dialog |
| 97 | + open={isCreateDialogOpen} |
| 98 | + onOpenChange={setIsCreateDialogOpen} |
| 99 | + > |
| 100 | + <DialogTrigger> |
| 101 | + <Button |
| 102 | + variant="outline" |
| 103 | + loading={createMutation.isLoading} |
| 104 | + Icon={LuPlus} |
| 105 | + > |
| 106 | + {t('Create Report')} |
| 107 | + </Button> |
| 108 | + </DialogTrigger> |
| 109 | + <DialogContent> |
| 110 | + <DialogHeader> |
| 111 | + <DialogTitle>{t('Generate Lighthouse Report')}</DialogTitle> |
| 112 | + <DialogDescription> |
| 113 | + {t('Its will take a while to generate the report.')} |
| 114 | + </DialogDescription> |
| 115 | + </DialogHeader> |
| 116 | + |
| 117 | + <div> |
| 118 | + <Input |
| 119 | + value={url} |
| 120 | + onChange={(e) => setUrl(e.target.value)} |
| 121 | + placeholder="https://google.com" |
| 122 | + /> |
| 123 | + </div> |
| 124 | + |
| 125 | + <DialogFooter> |
| 126 | + <Button |
| 127 | + loading={createMutation.isLoading} |
| 128 | + onClick={handleGenerateReport} |
| 129 | + > |
| 130 | + {t('Create')} |
| 131 | + </Button> |
| 132 | + </DialogFooter> |
| 133 | + </DialogContent> |
| 134 | + </Dialog> |
| 135 | + </div> |
| 136 | + |
| 137 | + <div className="flex flex-col gap-2"> |
| 138 | + {allData.map((report) => { |
| 139 | + return ( |
| 140 | + <div className="border-border flex items-start gap-2 rounded-lg border p-2"> |
| 141 | + <Badge>{report.status}</Badge> |
| 142 | + |
| 143 | + <div className="flex-1 overflow-hidden"> |
| 144 | + <div className="text-base">{report.url}</div> |
| 145 | + <div className="text-sm opacity-50"> |
| 146 | + {formatDate(report.createdAt)} |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + |
| 150 | + {report.status === 'Success' && ( |
| 151 | + <Button |
| 152 | + variant="outline" |
| 153 | + size="icon" |
| 154 | + Icon={LuArrowRight} |
| 155 | + onClick={() => window.open(`/lh/${report.id}/html`)} |
| 156 | + /> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + ); |
| 160 | + })} |
| 161 | + </div> |
| 162 | + |
| 163 | + {hasNextPage && ( |
| 164 | + <Button |
| 165 | + variant="outline" |
| 166 | + size="icon" |
| 167 | + loading={isFetchingNextPage} |
| 168 | + onClick={() => fetchNextPage()} |
| 169 | + > |
| 170 | + {t('Load More')} |
| 171 | + </Button> |
| 172 | + )} |
| 173 | + </div> |
| 174 | + </SheetContent> |
| 175 | + </Sheet> |
| 176 | + ); |
| 177 | + }); |
| 178 | +WebsiteLighthouseBtn.displayName = 'WebsiteLighthouseBtn'; |
0 commit comments