import {Fragment, useEffect, useState} from 'react'; import styled from '@emotion/styled'; import Button from 'sentry/components/button'; import FieldRequiredBadge from 'sentry/components/forms/field/fieldRequiredBadge'; import TextareaField from 'sentry/components/forms/textareaField'; import {IconDelete} from 'sentry/icons/iconDelete'; import {t} from 'sentry/locale'; import space from 'sentry/styles/space'; import {Project, Tag} from 'sentry/types'; import {LegacyBrowser, SamplingInnerName} from 'sentry/types/sampling'; import useApi from 'sentry/utils/useApi'; import { addCustomTagPrefix, getInnerNameLabel, isCustomTagName, stripCustomTagPrefix, } from '../utils'; import LegacyBrowsers from './legacyBrowsers'; import {TagKeyAutocomplete} from './tagKeyAutocomplete'; import {TagValueAutocomplete} from './tagValueAutocomplete'; import {getMatchFieldPlaceholder, getTagKey} from './utils'; type Condition = { category: SamplingInnerName | string; // string is used for custom tags legacyBrowsers?: Array; match?: string; }; type Props = Pick< React.ComponentProps, 'orgSlug' | 'projectId' > & { conditions: Condition[]; onChange: ( index: number, field: T, value: Condition[T] ) => void; onDelete: (index: number) => void; projectSlug: Project['slug']; }; function Conditions({ conditions, orgSlug, projectId, projectSlug, onDelete, onChange, }: Props) { const api = useApi(); const [tags, setTags] = useState([]); useEffect(() => { async function fetchTags() { try { const response = await api.requestPromise( `/projects/${orgSlug}/${projectSlug}/tags/`, {query: {onlySamplingTags: 1}} ); setTags(response); } catch { // Do nothing, just autocomplete won't suggest any results } } fetchTags(); }, [api, orgSlug, projectSlug]); return ( {conditions.map((condition, index) => { const {category, match, legacyBrowsers} = condition; const displayLegacyBrowsers = category === SamplingInnerName.EVENT_LEGACY_BROWSER; const isCustomTag = isCustomTagName(category); const isBooleanField = category === SamplingInnerName.EVENT_LOCALHOST || category === SamplingInnerName.EVENT_WEB_CRAWLERS; displayLegacyBrowsers; const isAutoCompleteField = category === SamplingInnerName.EVENT_ENVIRONMENT || category === SamplingInnerName.EVENT_RELEASE || category === SamplingInnerName.EVENT_TRANSACTION || category === SamplingInnerName.EVENT_OS_NAME || category === SamplingInnerName.EVENT_DEVICE_FAMILY || category === SamplingInnerName.EVENT_DEVICE_NAME || category === SamplingInnerName.TRACE_ENVIRONMENT || category === SamplingInnerName.TRACE_RELEASE || category === SamplingInnerName.TRACE_TRANSACTION || isCustomTag; return ( {isCustomTag ? ( onChange(index, 'category', addCustomTagPrefix(value)) } value={stripCustomTagPrefix(category)} disabledOptions={conditions .filter( cond => isCustomTagName(cond.category) && cond.category !== category ) .map(cond => stripCustomTagPrefix(cond.category))} /> ) : ( {getInnerNameLabel(category)} )} {!isBooleanField && (isAutoCompleteField ? ( onChange(index, 'match', value)} /> ) : ( onChange(index, 'match', value)} placeholder={getMatchFieldPlaceholder(category)} inline={false} rows={1} autosize hideControlState flexibleControlStateSize required stacked /> ))}