diff --git a/web/src/app/app/_layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons.tsx b/web/src/app/app/_layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons.tsx index 856b53c3b..228cb401c 100644 --- a/web/src/app/app/_layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons.tsx +++ b/web/src/app/app/_layouts/ChatLayout/FileContainer/FileContainerHeader/MetricContainerHeaderButtons.tsx @@ -14,15 +14,15 @@ import { HideButtonContainer } from './HideButtonContainer'; import { FileButtonContainer } from './FileButtonContainer'; import { CreateChatButton } from './CreateChatButtont'; import { SelectableButton } from './SelectableButton'; +import { useMetricFetched } from '@/context/Metrics'; export const MetricContainerHeaderButtons: React.FC = React.memo(() => { - const selectedFileView = useChatLayoutContextSelector( - (x) => x.selectedFileView - ) as MetricFileView; const isPureFile = useChatLayoutContextSelector((x) => x.isPureFile); const selectedFileId = useChatIndividualContextSelector((x) => x.selectedFileId)!; - const metricId = selectedFileId; + const { fetched } = useMetricFetched({ metricId }); + + if (!fetched) return null; return ( diff --git a/web/src/context/Chats/ChatProvider/ChatProvider.tsx b/web/src/context/Chats/ChatProvider/ChatProvider.tsx index f10455fc9..04883fa3d 100644 --- a/web/src/context/Chats/ChatProvider/ChatProvider.tsx +++ b/web/src/context/Chats/ChatProvider/ChatProvider.tsx @@ -63,6 +63,7 @@ export const useBusterChatIndividual = ({ }); const selectedChat: IBusterChat = chat || memoizedFallbackToMetricChat; + const fetched = chat?.created_at !== undefined; useEffect(() => { if (chatId) subscribeToChat({ chatId }); diff --git a/web/src/context/Chats/ChatProvider/helpers/useFileFallback.ts b/web/src/context/Chats/ChatProvider/helpers/useFileFallback.ts index 44d0027d9..1926204ff 100644 --- a/web/src/context/Chats/ChatProvider/helpers/useFileFallback.ts +++ b/web/src/context/Chats/ChatProvider/helpers/useFileFallback.ts @@ -11,16 +11,8 @@ export const useFileFallback = ({ defaultSelectedFile?: SelectedFile; }) => { const fileId = defaultSelectedFile?.id || ''; - const metricTitle = useBusterMetricsIndividualContextSelector((x) => x.metrics[fileId]?.title); - const metricVersionNumber = useBusterMetricsIndividualContextSelector( - (x) => x.metrics[fileId]?.version_number - ); - const dashboardTitle = useBusterDashboardContextSelector( - (x) => x.dashboards[fileId]?.dashboard?.title - ); - const dashboardVersionNumber = useBusterDashboardContextSelector( - (x) => x.dashboards[fileId]?.dashboard?.version_number - ); + const { metricTitle, metricVersionNumber } = useMetricParams(fileId); + const { dashboardTitle, dashboardVersionNumber } = useDashboardParams(fileId); const fileType: 'metric' | 'dashboard' = useMemo(() => { if (defaultSelectedFile?.type === 'metric') { @@ -70,7 +62,6 @@ const fallbackToFileChat = ({ versionNumber: number; type?: 'metric' | 'dashboard'; }): IBusterChat => { - console.log(type); return { id, isNewChat: true, @@ -82,7 +73,7 @@ const fallbackToFileChat = ({ { id: 'init', type: 'text', - message: `I’ve pulled in your ${type}. How can I help? Is there anything you'd like to modify?` + message: `I've pulled in your ${type}. How can I help? Is there anything you'd like to modify?` }, { id, @@ -114,3 +105,23 @@ const fallbackToFileChat = ({ created_by_avatar: '' }; }; + +const useMetricParams = (fileId: string) => { + const metricTitle = useBusterMetricsIndividualContextSelector((x) => x.metrics[fileId]?.title); + const metricVersionNumber = useBusterMetricsIndividualContextSelector( + (x) => x.metrics[fileId]?.version_number + ); + + return { metricTitle, metricVersionNumber }; +}; + +const useDashboardParams = (fileId: string) => { + const dashboardTitle = useBusterDashboardContextSelector( + (x) => x.dashboards[fileId]?.dashboard?.title + ); + const dashboardVersionNumber = useBusterDashboardContextSelector( + (x) => x.dashboards[fileId]?.dashboard?.version_number + ); + + return { dashboardTitle, dashboardVersionNumber }; +}; diff --git a/web/src/context/MetricData/MOCK_DATA.ts b/web/src/context/MetricData/MOCK_DATA.ts index 95c95d8ee..950c44bbc 100644 --- a/web/src/context/MetricData/MOCK_DATA.ts +++ b/web/src/context/MetricData/MOCK_DATA.ts @@ -3,7 +3,7 @@ import type { BusterMetricData } from '../Metrics'; import { faker } from '@faker-js/faker'; const mockData = (): Record[] => { - return Array.from({ length: faker.number.int({ min: 2, max: 615 }) }, (x, index) => ({ + return Array.from({ length: faker.number.int({ min: 2, max: 100 }) }, (x, index) => ({ sales: index + 1, date: faker.date.past({ years: index + 1 }).toISOString(), product: faker.commerce.productName() diff --git a/web/src/context/Metrics/BusterMetricsIndividualProvider/index.ts b/web/src/context/Metrics/BusterMetricsIndividualProvider/index.ts index 3320c22c7..206c10861 100644 --- a/web/src/context/Metrics/BusterMetricsIndividualProvider/index.ts +++ b/web/src/context/Metrics/BusterMetricsIndividualProvider/index.ts @@ -1 +1,2 @@ export * from './BusterMetricsIndividualProvider'; +export * from './useMetricFetched'; diff --git a/web/src/context/Metrics/BusterMetricsIndividualProvider/useMetricFetched.ts b/web/src/context/Metrics/BusterMetricsIndividualProvider/useMetricFetched.ts new file mode 100644 index 000000000..d2b697905 --- /dev/null +++ b/web/src/context/Metrics/BusterMetricsIndividualProvider/useMetricFetched.ts @@ -0,0 +1,9 @@ +import { useBusterMetricsIndividualContextSelector } from './BusterMetricsIndividualProvider'; + +export const useMetricFetched = ({ metricId }: { metricId: string }) => { + const fetched = useBusterMetricsIndividualContextSelector((x) => x.metrics[metricId]?.fetched); + const fetching = useBusterMetricsIndividualContextSelector((x) => x.metrics[metricId]?.fetching); + const error = useBusterMetricsIndividualContextSelector((x) => x.metrics[metricId]?.error); + + return { fetched, fetching, error }; +};