prompts.tsx 3.8 KB

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