123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- import {isValidElement} from 'react';
- import styled from '@emotion/styled';
- import * as Sentry from '@sentry/react';
- import FormModel, {FieldValue} from 'sentry/components/forms/model';
- import {DEFAULT_TOAST_DURATION} from 'sentry/constants';
- import {t, tct} from 'sentry/locale';
- import IndicatorStore from 'sentry/stores/indicatorStore';
- import space from 'sentry/styles/space';
- type IndicatorType = 'loading' | 'error' | 'success' | 'undo' | '';
- type Options = {
- append?: boolean;
- disableDismiss?: boolean;
- duration?: number;
- modelArg?: {
- id: string;
- model: FormModel;
- undo: () => void;
- };
- undo?: () => void;
- };
- export type Indicator = {
- id: string | number;
- message: React.ReactNode;
- options: Options;
- type: IndicatorType;
- clearId?: null | number;
- };
- export function removeIndicator(indicator: Indicator) {
- IndicatorStore.remove(indicator);
- }
- export function clearIndicators() {
- IndicatorStore.clear();
- }
- export function addMessage(
- msg: React.ReactNode,
- type: IndicatorType,
- options: Options = {}
- ): void {
- const {duration: optionsDuration, append, ...rest} = options;
-
- if (
-
- typeof msg?.message !== 'undefined' &&
-
- typeof msg?.code !== 'undefined' &&
-
- typeof msg?.extra !== 'undefined'
- ) {
- Sentry.captureException(new Error('Attempt to XHR response to Indicators'));
- }
-
- const duration =
- typeof optionsDuration === 'undefined' ? DEFAULT_TOAST_DURATION : optionsDuration;
- const action = append ? 'append' : 'add';
-
-
-
- IndicatorStore[action](msg, type, {...rest, duration});
- }
- function addMessageWithType(type: IndicatorType) {
- return (msg: React.ReactNode, options?: Options) => addMessage(msg, type, options);
- }
- export function addLoadingMessage(
- msg: React.ReactNode = t('Saving changes...'),
- options?: Options
- ) {
- return addMessageWithType('loading')(msg, options);
- }
- export function addErrorMessage(msg: React.ReactNode, options?: Options) {
- if (typeof msg === 'string' || isValidElement(msg)) {
- return addMessageWithType('error')(msg, options);
- }
-
-
-
-
- return addMessageWithType('error')(
- t(
- "You've hit an issue, fortunately we use Sentry to monitor Sentry. So it's likely we're already looking into this!"
- ),
- options
- );
- }
- export function addSuccessMessage(msg: React.ReactNode, options?: Options) {
- return addMessageWithType('success')(msg, options);
- }
- const PRETTY_VALUES: Map<unknown, string> = new Map([
- ['', '<empty>'],
- [null, '<none>'],
- [undefined, '<unset>'],
-
- [true as any, 'enabled'],
- [false as any, 'disabled'],
- ]);
- const prettyFormString = (val: ChangeValue, model: FormModel, fieldName: string) => {
- const descriptor = model.fieldDescriptor.get(fieldName);
- if (descriptor && typeof descriptor.formatMessageValue === 'function') {
- const initialData = model.initialData;
-
-
-
- return descriptor.formatMessageValue(val, {...descriptor, initialData});
- }
- if (PRETTY_VALUES.has(val)) {
- return PRETTY_VALUES.get(val);
- }
- return typeof val === 'object' ? val : String(val);
- };
- type ChangeValue = FieldValue | Record<string, any>;
- type Change = {
- new: ChangeValue;
- old: ChangeValue;
- };
- export function saveOnBlurUndoMessage(
- change: Change,
- model: FormModel,
- fieldName: string
- ) {
- if (!model) {
- return;
- }
- const label = model.getDescriptor(fieldName, 'label');
- if (!label) {
- return;
- }
- const prettifyValue = (val: ChangeValue) => prettyFormString(val, model, fieldName);
-
- const showChangeText = model.getDescriptor(fieldName, 'formatMessageValue') !== false;
- addSuccessMessage(
- tct(
- showChangeText
- ? 'Changed [fieldName] from [oldValue] to [newValue]'
- : 'Changed [fieldName]',
- {
- root: <MessageContainer />,
- fieldName: <FieldName>{label}</FieldName>,
- oldValue: <FormValue>{prettifyValue(change.old)}</FormValue>,
- newValue: <FormValue>{prettifyValue(change.new)}</FormValue>,
- }
- ),
- {
- modelArg: {
- model,
- id: fieldName,
- undo: () => {
- if (!model || !fieldName) {
- return;
- }
- const oldValue = model.getValue(fieldName);
- const didUndo = model.undo();
- const newValue = model.getValue(fieldName);
- if (!didUndo) {
- return;
- }
- if (!label) {
- return;
- }
-
- const saveResult = model.saveField(fieldName, newValue);
- if (!saveResult) {
- addErrorMessage(
- tct(
- showChangeText
- ? 'Unable to restore [fieldName] from [oldValue] to [newValue]'
- : 'Unable to restore [fieldName]',
- {
- root: <MessageContainer />,
- fieldName: <FieldName>{label}</FieldName>,
- oldValue: <FormValue>{prettifyValue(oldValue)}</FormValue>,
- newValue: <FormValue>{prettifyValue(newValue)}</FormValue>,
- }
- )
- );
- return;
- }
- saveResult.then(() => {
- addMessage(
- tct(
- showChangeText
- ? 'Restored [fieldName] from [oldValue] to [newValue]'
- : 'Restored [fieldName]',
- {
- root: <MessageContainer />,
- fieldName: <FieldName>{label}</FieldName>,
- oldValue: <FormValue>{prettifyValue(oldValue)}</FormValue>,
- newValue: <FormValue>{prettifyValue(newValue)}</FormValue>,
- }
- ),
- 'undo',
- {
- duration: DEFAULT_TOAST_DURATION,
- }
- );
- });
- },
- },
- }
- );
- }
- const FormValue = styled('span')`
- font-style: italic;
- margin: 0 ${space(0.5)};
- `;
- const FieldName = styled('span')`
- font-weight: bold;
- margin: 0 ${space(0.5)};
- `;
- const MessageContainer = styled('div')`
- display: flex;
- align-items: center;
- `;
|