prompts.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import type {Client} from 'sentry/api';
  2. import {ApiQueryKey, useApiQuery} 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({feature, organizationId, projectId}: PromptCheckParams) {
  93. return useApiQuery<PromptResponse>(
  94. makePromptsCheckQueryKey({feature, organizationId, projectId}),
  95. {
  96. staleTime: 120000,
  97. }
  98. );
  99. }
  100. /**
  101. * Get the status of many prompt
  102. */
  103. export async function batchedPromptsCheck<T extends readonly string[]>(
  104. api: Client,
  105. features: T,
  106. params: {organizationId: string; projectId?: string}
  107. ): Promise<{[key in T[number]]: PromptData}> {
  108. const query = {
  109. feature: features,
  110. organization_id: params.organizationId,
  111. ...(params.projectId === undefined ? {} : {project_id: params.projectId}),
  112. };
  113. const response: PromptResponse = await api.requestPromise('/prompts-activity/', {
  114. query,
  115. });
  116. const responseFeatures = response?.features;
  117. const result: {[key in T[number]]?: PromptData} = {};
  118. if (!responseFeatures) {
  119. return result as {[key in T[number]]: PromptData};
  120. }
  121. for (const featureName of features) {
  122. const item = responseFeatures[featureName];
  123. if (item) {
  124. result[featureName] = {
  125. dismissedTime: item.dismissed_ts,
  126. snoozedTime: item.snoozed_ts,
  127. };
  128. } else {
  129. result[featureName] = null;
  130. }
  131. }
  132. return result as {[key in T[number]]: PromptData};
  133. }