prompts.tsx 3.7 KB

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