'use client'; import { useContext } from 'react'; import { Icon, LoadingButton, InlineEditField, useToasts } from 'react-basics'; import { useMessages, useApi, useNavigation, useTeamUrl } from 'components/hooks'; import { ReportContext } from './Report'; import styles from './ReportHeader.module.css'; import { REPORT_TYPES } from 'lib/constants'; export function ReportHeader({ icon }) { const { report, updateReport } = useContext(ReportContext); const { formatMessage, labels, messages } = useMessages(); const { showToast } = useToasts(); const { router } = useNavigation(); const { renderTeamUrl } = useTeamUrl(); const { post, useMutation } = useApi(); const { mutate: create, isPending: isCreating } = useMutation({ mutationFn: (data: any) => post(`/reports`, data), }); const { mutate: update, isPending: isUpdating } = useMutation({ mutationFn: (data: any) => post(`/reports/${data.id}`, data), }); const { name, description, parameters } = report || {}; const { websiteId, dateRange } = parameters || {}; const defaultName = formatMessage(labels.untitled); const handleSave = async () => { if (!report.id) { create(report, { onSuccess: async ({ id }) => { showToast({ message: formatMessage(messages.saved), variant: 'success' }); router.push(renderTeamUrl(`/reports/${id}`)); }, }); } else { update(report, { onSuccess: async () => { showToast({ message: formatMessage(messages.saved), variant: 'success' }); }, }); } }; const handleNameChange = (name: string) => { updateReport({ name: name || defaultName }); }; const handleDescriptionChange = (description: string) => { updateReport({ description }); }; if (!report) { return null; } return (