prompts.tsx 3.5 KB

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