import { ButtonContainer } from '@/components/button-container'; import { InputWithLabel } from '@/components/forms/input-with-label'; import { Button } from '@/components/ui/button'; import { Label } from '@/components/ui/label'; import { useAppParams } from '@/hooks/use-app-params'; import { handleError } from '@/integrations/trpc/react'; import { zodResolver } from '@hookform/resolvers/zod'; import { useRouter, useSearch } from '@tanstack/react-router'; import { Controller, useForm } from 'react-hook-form'; import { toast } from 'sonner'; import { z } from 'zod'; import type { IChartProps } from '@openpanel/validation'; import { Input } from '@/components/ui/input'; import { useTRPC } from '@/integrations/trpc/react'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { ArrowLeftIcon, PlusIcon, SaveIcon } from 'lucide-react'; import { useState } from 'react'; import { popModal } from '.'; import { ModalContent, ModalHeader } from './Modal/Container'; type SaveReportProps = { report: IChartProps; disableRedirect?: boolean; }; const validator = z.object({ name: z.string().min(1, 'Required'), dashboardId: z.string().min(1, 'Required'), }); type IForm = z.infer; export default function SaveReport({ report, disableRedirect, }: SaveReportProps) { const router = useRouter(); const queryClient = useQueryClient(); const { organizationId, projectId } = useAppParams(); const searchParams = useSearch({ from: '/_app/$organizationId/$projectId/reports', shouldThrow: false, }); const dashboardId = searchParams?.dashboardId; const trpc = useTRPC(); const save = useMutation( trpc.report.create.mutationOptions({ onError: handleError, onSuccess(res) { queryClient.invalidateQueries( trpc.report.list.queryFilter({ dashboardId: res.dashboardId, projectId, }), ); const goToReport = () => { router.navigate({ to: '/$organizationId/$projectId/reports/$reportId', params: { organizationId, projectId, reportId: res.id, }, search: searchParams, }); }; toast('Report created', { description: `${res.name}`, action: { label: 'View report', onClick: () => goToReport(), }, }); if (!disableRedirect) { goToReport(); } popModal(); }, }), ); const { register, handleSubmit, formState, control, setValue } = useForm({ resolver: zodResolver(validator), defaultValues: { name: report.name, dashboardId, }, }); return (
{ save.mutate({ report: { ...report, name, }, ...values, }); })} > { return ( ); }} />
); } function SelectDashboard({ value, onChange, projectId, }: { value: string; onChange: (value: string) => void; projectId: string; }) { const trpc = useTRPC(); const [isCreatingNew, setIsCreatingNew] = useState(false); const [newDashboardName, setNewDashboardName] = useState(''); const form = useForm({ resolver: zodResolver(z.object({ name: z.string().min(1, 'Required') })), defaultValues: { name: '', }, }); const dashboardQuery = useQuery( trpc.dashboard.list.queryOptions({ projectId: projectId!, }), ); const dashboardMutation = useMutation( trpc.dashboard.create.mutationOptions({ onError: handleError, async onSuccess(res) { await dashboardQuery.refetch(); onChange(res.id); setIsCreatingNew(false); setNewDashboardName(''); form.reset(); }, }), ); const handleSelectChange = (selectedValue: string) => { if (selectedValue === 'create-new') { setIsCreatingNew(true); onChange(''); // Clear the current selection } else { setIsCreatingNew(false); onChange(selectedValue); } }; const handleCreateDashboard = () => { if (newDashboardName.trim()) { dashboardMutation.mutate({ name: newDashboardName.trim(), projectId, }); } }; const selectedDashboard = dashboardQuery.data?.find((d) => d.id === value); return (
{!isCreatingNew ? (
{dashboardQuery.data?.map((dashboard) => ( ))}
) : (
)}
); }