prompts.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. const data = response?.data;
  73. if (!data) {
  74. return null;
  75. }
  76. return {
  77. dismissedTime: data.dismissed_ts,
  78. snoozedTime: data.snoozed_ts,
  79. };
  80. }
  81. /**
  82. * Get the status of many prompt
  83. */
  84. export async function batchedPromptsCheck<T extends readonly string[]>(
  85. api: Client,
  86. features: T,
  87. params: {organizationId: string; projectId?: string}
  88. ): Promise<{[key in T[number]]: PromptData}> {
  89. const query = {
  90. feature: features,
  91. organization_id: params.organizationId,
  92. ...(params.projectId === undefined ? {} : {project_id: params.projectId}),
  93. };
  94. const response: PromptResponse = await api.requestPromise('/prompts-activity/', {
  95. query,
  96. });
  97. const responseFeatures = response?.features;
  98. const result: {[key in T[number]]?: PromptData} = {};
  99. if (!responseFeatures) {
  100. return result as {[key in T[number]]: PromptData};
  101. }
  102. for (const featureName of features) {
  103. const item = responseFeatures[featureName];
  104. if (item) {
  105. result[featureName] = {
  106. dismissedTime: item.dismissed_ts,
  107. snoozedTime: item.snoozed_ts,
  108. };
  109. } else {
  110. result[featureName] = null;
  111. }
  112. }
  113. return result as {[key in T[number]]: PromptData};
  114. }