From 92903f7339f26fbf4a9a71d67cabcf8543073404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Fri, 4 Oct 2024 10:34:10 +0100 Subject: [PATCH] chore: make signals visible on event timeline for non admins --- .../EventTimelineHeaderTip.tsx | 21 ++++++++++++------- .../getters/useSignalQuery/useSignalQuery.ts | 6 ++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/frontend/src/component/events/EventTimeline/EventTimelineHeader/EventTimelineHeaderTip.tsx b/frontend/src/component/events/EventTimeline/EventTimelineHeader/EventTimelineHeaderTip.tsx index 248c4f3d1bef..05869b91fb40 100644 --- a/frontend/src/component/events/EventTimeline/EventTimelineHeader/EventTimelineHeaderTip.tsx +++ b/frontend/src/component/events/EventTimeline/EventTimelineHeader/EventTimelineHeaderTip.tsx @@ -1,13 +1,12 @@ import { Chip, styled } from '@mui/material'; -import AccessContext from 'contexts/AccessContext'; -import { useSignalEndpoints } from 'hooks/api/getters/useSignalEndpoints/useSignalEndpoints'; import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; import { useUiFlag } from 'hooks/useUiFlag'; -import { useContext } from 'react'; import { useEventTimelineContext } from '../EventTimelineContext'; import { Link, useNavigate } from 'react-router-dom'; import SensorsIcon from '@mui/icons-material/Sensors'; import { usePlausibleTracker } from 'hooks/usePlausibleTracker'; +import { useSignalQuery } from 'hooks/api/getters/useSignalQuery/useSignalQuery'; +import { startOfDay, sub } from 'date-fns'; const StyledTip = styled('div')({ display: 'flex', @@ -22,24 +21,30 @@ const StyledSignalIcon = styled(SensorsIcon)(({ theme }) => ({ const signalsLink = '/integrations/signals'; +const toISODateString = (date: Date) => date.toISOString().split('T')[0]; + export const EventTimelineHeaderTip = () => { const navigate = useNavigate(); + const { timeSpan } = useEventTimelineContext(); + const endDate = new Date(); + const startDate = sub(endDate, timeSpan.value); + const { signals, loading: signalsLoading } = useSignalQuery({ + from: `IS:${toISODateString(startOfDay(startDate))}`, + to: `IS:${toISODateString(endDate)}`, + }); const { signalsSuggestionSeen, setSignalsSuggestionSeen } = useEventTimelineContext(); const { isEnterprise } = useUiConfig(); - const { isAdmin } = useContext(AccessContext); const signalsEnabled = useUiFlag('signals'); - const { signalEndpoints, loading } = useSignalEndpoints(); const { trackEvent } = usePlausibleTracker(); if ( !signalsSuggestionSeen && isEnterprise() && - isAdmin && signalsEnabled && - !loading && - signalEndpoints.length === 0 + !signalsLoading && + signals.length === 0 ) { return ( diff --git a/frontend/src/hooks/api/getters/useSignalQuery/useSignalQuery.ts b/frontend/src/hooks/api/getters/useSignalQuery/useSignalQuery.ts index 6fedd43ff029..cbc2aac3703b 100644 --- a/frontend/src/hooks/api/getters/useSignalQuery/useSignalQuery.ts +++ b/frontend/src/hooks/api/getters/useSignalQuery/useSignalQuery.ts @@ -1,12 +1,11 @@ import type { SWRConfiguration } from 'swr'; -import { useCallback, useContext } from 'react'; +import { useCallback } from 'react'; import { formatApiPath } from 'utils/formatPath'; import handleErrorResponses from '../httpErrorResponseHandler'; import { useClearSWRCache } from 'hooks/useClearSWRCache'; import type { ISignalQuerySignal } from 'interfaces/signal'; import useUiConfig from '../useUiConfig/useUiConfig'; import { useUiFlag } from 'hooks/useUiFlag'; -import AccessContext from 'contexts/AccessContext'; import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR'; type SignalQueryParams = { @@ -57,7 +56,6 @@ const createSignalQuery = () => { options: SWRConfiguration = {}, cachePrefix: string = '', ): UseSignalsOutput => { - const { isAdmin } = useContext(AccessContext); const { isEnterprise } = useUiConfig(); const signalsEnabled = useUiFlag('signals'); @@ -67,7 +65,7 @@ const createSignalQuery = () => { const { data, error, mutate, isLoading } = useConditionalSWR( - isEnterprise() && isAdmin && signalsEnabled, + isEnterprise() && signalsEnabled, fallbackData, swrKey, fetcher,