Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make signals visible on event timeline for non admins #8364

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 (
<StyledTip>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -57,7 +56,6 @@ const createSignalQuery = () => {
options: SWRConfiguration = {},
cachePrefix: string = '',
): UseSignalsOutput => {
const { isAdmin } = useContext(AccessContext);
const { isEnterprise } = useUiConfig();
const signalsEnabled = useUiFlag('signals');

Expand All @@ -67,7 +65,7 @@ const createSignalQuery = () => {

const { data, error, mutate, isLoading } =
useConditionalSWR<SignalQueryResponse>(
isEnterprise() && isAdmin && signalsEnabled,
isEnterprise() && signalsEnabled,
fallbackData,
swrKey,
fetcher,
Expand Down
Loading