prompts.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {Client} from 'sentry/api';
  2. type PromptsUpdateParams = {
  3. /**
  4. * The prompt feature name
  5. */
  6. feature: string;
  7. /**
  8. * The numeric organization ID as a string
  9. */
  10. organizationId: string;
  11. status: 'snoozed' | 'dismissed';
  12. /**
  13. * The numeric project ID as a string
  14. */
  15. projectId?: string;
  16. };
  17. /**
  18. * Update the status of a prompt
  19. */
  20. export function promptsUpdate(api: Client, params: PromptsUpdateParams) {
  21. return api.requestPromise('/prompts-activity/', {
  22. method: 'PUT',
  23. data: {
  24. organization_id: params.organizationId,
  25. project_id: params.projectId,
  26. feature: params.feature,
  27. status: params.status,
  28. },
  29. });
  30. }
  31. type PromptCheckParams = {
  32. /**
  33. * The prompt feature name
  34. */
  35. feature: string;
  36. /**
  37. * The numeric organization ID as a string
  38. */
  39. organizationId: string;
  40. /**
  41. * The numeric project ID as a string
  42. */
  43. projectId?: string;
  44. };
  45. export type PromptResponseItem = {
  46. dismissed_ts?: number;
  47. snoozed_ts?: number;
  48. };
  49. export type PromptResponse = {
  50. data?: PromptResponseItem;
  51. features?: {[key: string]: PromptResponseItem};
  52. };
  53. export type PromptData = null | {
  54. dismissedTime?: number;
  55. snoozedTime?: number;
  56. };
  57. /**
  58. * Get the status of a prompt
  59. */
  60. export async function promptsCheck(
  61. api: Client,
  62. params: PromptCheckParams
  63. ): Promise<PromptData> {
  64. const query = {
  65. feature: params.feature,
  66. organization_id: params.organizationId,
  67. ...(params.projectId === undefined ? {} : {project_id: params.projectId}),
  68. };
  69. const response: PromptResponse = await api.requestPromise('/prompts-activity/', {
  70. query,
  71. });
  72. if (response?.data) {
  73. return {
  74. dismissedTime: response.data.dismissed_ts,
  75. snoozedTime: response.data.snoozed_ts,
  76. };
  77. }
  78. return null;
  79. }
  80. /**
  81. * Get the status of many prompt
  82. */
  83. export async function batchedPromptsCheck<T extends readonly string[]>(
  84. api: Client,
  85. features: T,
  86. params: {organizationId: string; projectId?: string}
  87. ): Promise<{[key in T[number]]: PromptData}> {
  88. const query = {
  89. feature: features,
  90. organization_id: params.organizationId,
  91. ...(params.projectId === undefined ? {} : {project_id: params.projectId}),
  92. };
  93. const response: PromptResponse = await api.requestPromise('/prompts-activity/', {
  94. query,
  95. });
  96. const responseFeatures = response?.features;
  97. const result: {[key in T[number]]?: PromptData} = {};
  98. if (!responseFeatures) {
  99. return result as {[key in T[number]]: PromptData};
  100. }
  101. for (const featureName of features) {
  102. const item = responseFeatures[featureName];
  103. if (item) {
  104. result[featureName] = {
  105. dismissedTime: item.dismissed_ts,
  106. snoozedTime: item.snoozed_ts,
  107. };
  108. } else {
  109. result[featureName] = null;
  110. }
  111. }
  112. return result as {[key in T[number]]: PromptData};
  113. }