123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- import {useCallback, useState} from 'react';
- import styled from '@emotion/styled';
- import {addSuccessMessage} from 'sentry/actionCreators/indicator';
- import {Button} from 'sentry/components/button';
- import ButtonBar from 'sentry/components/buttonBar';
- import EmptyMessage from 'sentry/components/emptyMessage';
- import LoadingError from 'sentry/components/loadingError';
- import Panel from 'sentry/components/panels/panel';
- import PanelBody from 'sentry/components/panels/panelBody';
- import PanelFooter from 'sentry/components/panels/panelFooter';
- import PanelHeader from 'sentry/components/panels/panelHeader';
- import {IconFile, IconFlag, IconHappy, IconMeh, IconSad} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import ConfigStore from 'sentry/stores/configStore';
- import {space} from 'sentry/styles/space';
- import {Event, Project} from 'sentry/types';
- import {trackAnalytics} from 'sentry/utils/analytics';
- import {getAnalyticsDataForEvent} from 'sentry/utils/events';
- import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
- import marked from 'sentry/utils/marked';
- import {useApiQuery} from 'sentry/utils/queryClient';
- import useOrganization from 'sentry/utils/useOrganization';
- import {ExperimentalFeatureBadge} from './experimentalFeatureBadge';
- import {SuggestionLoaderMessage} from './suggestionLoaderMessage';
- import {useOpenAISuggestionLocalStorage} from './useOpenAISuggestionLocalStorage';
- type Props = {
- event: Event;
- onHideSuggestion: () => void;
- projectSlug: Project['slug'];
- };
- function ErrorDescription({
- restriction,
- organizationSlug,
- onRefetch,
- onSetIndividualConsent,
- onHideSuggestion,
- }: {
- onHideSuggestion: () => void;
- onRefetch: () => void;
- onSetIndividualConsent: (consent: boolean) => void;
- organizationSlug: string;
- restriction?: 'subprocessor' | 'individual_consent';
- }) {
- if (restriction === 'subprocessor') {
- return (
- <EmptyMessage
- icon={<IconFile size="xl" />}
- title={t('OpenAI Subprocessor Acknowledgment')}
- description={t(
- 'In order to use this feature, your organization needs to accept the OpenAI Subprocessor Acknowledgment.'
- )}
- action={
- <ButtonBar gap={2}>
- <Button onClick={onHideSuggestion}>{t('Dismiss')}</Button>
- <Button priority="primary" to={`/settings/${organizationSlug}/legal/`}>
- {t('Accept in Settings')}
- </Button>
- </ButtonBar>
- }
- />
- );
- }
- if (restriction === 'individual_consent') {
- const activeSuperUser = isActiveSuperuser();
- return (
- <EmptyMessage
- icon={<IconFlag size="xl" />}
- title={t('We need your consent')}
- description={t(
- 'By using this feature, you agree that OpenAI is a subprocessor and may process the data that you’ve chosen to submit. Sentry makes no guarantees as to the accuracy of the feature’s AI-generated recommendations.'
- )}
- action={
- <ButtonBar gap={2}>
- <Button onClick={onHideSuggestion}>{t('Dismiss')}</Button>
- <Button
- priority="primary"
- onClick={() => {
- onSetIndividualConsent(true);
- onRefetch();
- }}
- disabled={activeSuperUser}
- title={
- activeSuperUser ? t("Superusers can't consent to policies") : undefined
- }
- >
- {t('Confirm')}
- </Button>
- </ButtonBar>
- }
- />
- );
- }
- return <SuggestionLoadingError onRetry={onRefetch} />;
- }
- export function Suggestion({onHideSuggestion, projectSlug, event}: Props) {
- const organization = useOrganization();
- const [suggestedSolutionLocalConfig, setSuggestedSolutionLocalConfig] =
- useOpenAISuggestionLocalStorage();
- const [piiCertified, setPiiCertified] = useState(false);
- const {isStaff} = ConfigStore.get('user');
- const {
- data,
- isLoading: dataIsLoading,
- isError: dataIsError,
- refetch: dataRefetch,
- error,
- } = useApiQuery<{suggestion: string}>(
- [
- `/projects/${organization.slug}/${projectSlug}/events/${event.eventID}/ai-fix-suggest/`,
- {
- query: {
- consent: suggestedSolutionLocalConfig.individualConsent ? 'yes' : undefined,
- pii_certified: isStaff ? (piiCertified ? 'yes' : 'no') : undefined,
- },
- },
- ],
- {
- enabled: isStaff ? (piiCertified ? true : false) : true,
- staleTime: Infinity,
- retry: false,
- }
- );
- const handleFeedbackClick = useCallback(() => {
- addSuccessMessage('Thank you for your feedback!');
- }, []);
- if (isStaff && !piiCertified) {
- return (
- <EmptyMessage
- icon={<IconFlag size="xl" />}
- title={t('PII Certification Required')}
- description={t(
- 'Before using this feature, please confirm that there is no personally identifiable information in this event.'
- )}
- action={
- <ButtonBar gap={2}>
- <Button priority="primary" onClick={() => setPiiCertified(true)}>
- {t('Certify No PII')}
- </Button>
- </ButtonBar>
- }
- />
- );
- }
- return (
- <Panel>
- <Header>
- <Title>
- {t('AI Solution')}
- <ExperimentalFeatureBadge />
- </Title>
- <Button size="xs" onClick={onHideSuggestion}>
- {t('Hide Suggestion')}
- </Button>
- </Header>
- <PanelBody>
- {dataIsLoading ? (
- <LoaderWrapper>
- <div className="ai-suggestion-wheel-of-fortune" />
- <SuggestionLoaderMessage />
- </LoaderWrapper>
- ) : dataIsError ? (
- <ErrorDescription
- onRefetch={dataRefetch}
- organizationSlug={organization.slug}
- onSetIndividualConsent={() =>
- setSuggestedSolutionLocalConfig({individualConsent: true})
- }
- restriction={error?.responseJSON?.restriction as any}
- onHideSuggestion={onHideSuggestion}
- />
- ) : (
- <Content
- dangerouslySetInnerHTML={{
- __html: marked(data.suggestion, {
- gfm: true,
- breaks: true,
- }),
- }}
- />
- )}
- </PanelBody>
- {!dataIsLoading && !dataIsError && (
- <PanelFooter>
- <Feedback>
- <strong>{t('Was this helpful?')}</strong>
- <ButtonBar gap={1}>
- <Button
- icon={<IconSad color="red300" />}
- size="xs"
- onClick={() => {
- trackAnalytics(
- 'ai_suggested_solution.feedback_helpful_nope_button_clicked',
- {
- organization,
- project_id: event.projectID,
- group_id: event.groupID,
- ...getAnalyticsDataForEvent(event),
- }
- );
- handleFeedbackClick();
- }}
- >
- {t('Nope')}
- </Button>
- <Button
- icon={<IconMeh color="yellow300" />}
- size="xs"
- onClick={() => {
- trackAnalytics(
- 'ai_suggested_solution.feedback_helpful_kinda_button_clicked',
- {
- organization,
- project_id: event.projectID,
- group_id: event.groupID,
- ...getAnalyticsDataForEvent(event),
- }
- );
- handleFeedbackClick();
- }}
- >
- {t('Kinda')}
- </Button>
- <Button
- icon={<IconHappy color="green300" />}
- size="xs"
- onClick={() => {
- trackAnalytics(
- 'ai_suggested_solution.feedback_helpful_yes_button_clicked',
- {
- organization,
- project_id: event.projectID,
- group_id: event.groupID,
- ...getAnalyticsDataForEvent(event),
- }
- );
- handleFeedbackClick();
- }}
- >
- {t('Yes, Surprisingly\u2026')}
- </Button>
- </ButtonBar>
- </Feedback>
- </PanelFooter>
- )}
- </Panel>
- );
- }
- const Header = styled(PanelHeader)`
- background: transparent;
- padding: ${space(1)} ${space(2)};
- align-items: center;
- color: ${p => p.theme.gray300};
- `;
- const Feedback = styled('div')`
- padding: ${space(1)} ${space(2)};
- display: grid;
- grid-template-columns: 1fr;
- align-items: center;
- text-align: left;
- gap: ${space(1)};
- font-size: ${p => p.theme.fontSizeSmall};
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- grid-template-columns: 1fr max-content;
- text-align: right;
- gap: ${space(2)};
- }
- `;
- const SuggestionLoadingError = styled(LoadingError)`
- margin-bottom: 0;
- border: none;
- /* This is just to be consitent with other */
- /* padding-top and padding-bottom we are using in the empty state component */
- padding-top: ${space(4)};
- padding-bottom: ${space(4)};
- `;
- const LoaderWrapper = styled('div')`
- padding: ${space(4)} 0;
- text-align: center;
- gap: ${space(2)};
- display: flex;
- flex-direction: column;
- `;
- const Content = styled('div')`
- padding: ${space(2)};
- /* hack until we update backend to send us other heading */
- h4 {
- font-size: ${p => p.theme.fontSizeExtraLarge};
- margin-bottom: ${space(1)};
- }
- `;
- const Title = styled('div')`
- /* to be consistent with the feature badge size */
- height: ${space(2)};
- line-height: ${space(2)};
- display: flex;
- align-items: center;
- `;
|