prompts.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import {useCallback} from 'react';
  2. import type {Client} from 'sentry/api';
  3. import type {Organization, OrganizationSummary} from 'sentry/types/organization';
  4. import {defined} from 'sentry/utils';
  5. import {promptIsDismissed} from 'sentry/utils/promptIsDismissed';
  6. import type {ApiQueryKey, UseApiQueryOptions} from 'sentry/utils/queryClient';
  7. import {setApiQueryData, useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
  8. import useApi from 'sentry/utils/useApi';
  9. type PromptsUpdateParams = {
  10. /**
  11. * The prompt feature name
  12. */
  13. feature: string;
  14. organization: OrganizationSummary;
  15. status: 'snoozed' | 'dismissed' | 'visible';
  16. /**
  17. * The numeric project ID as a string
  18. */
  19. projectId?: string;
  20. };
  21. /**
  22. * Update the status of a prompt
  23. */
  24. export function promptsUpdate(api: Client, params: PromptsUpdateParams) {
  25. const url = `/organizations/${params.organization.slug}/prompts-activity/`;
  26. return api.requestPromise(url, {
  27. method: 'PUT',
  28. data: {
  29. organization_id: params.organization.id,
  30. project_id: params.projectId,
  31. feature: params.feature,
  32. status: params.status,
  33. },
  34. });
  35. }
  36. type PromptCheckParams = {
  37. /**
  38. * The prompt feature name
  39. */
  40. feature: string | string[];
  41. organization: OrganizationSummary | null;
  42. /**
  43. * The numeric project ID as a string
  44. */
  45. projectId?: string;
  46. };
  47. /**
  48. * Raw response data from the endpoint
  49. */
  50. export type PromptResponseItem = {
  51. /**
  52. * Time since dismissed
  53. */
  54. dismissed_ts?: number;
  55. /**
  56. * Time since snoozed
  57. */
  58. snoozed_ts?: number;
  59. };
  60. export type PromptResponse = {
  61. data?: PromptResponseItem;
  62. features?: {[key: string]: PromptResponseItem};
  63. };
  64. /**
  65. * Processed endpoint response data
  66. */
  67. export type PromptData = null | {
  68. /**
  69. * Time since dismissed
  70. */
  71. dismissedTime?: number;
  72. /**
  73. * Time since snoozed
  74. */
  75. snoozedTime?: number;
  76. };
  77. /**
  78. * Get the status of a prompt
  79. */
  80. export async function promptsCheck(
  81. api: Client,
  82. params: PromptCheckParams
  83. ): Promise<PromptData> {
  84. const query = {
  85. feature: params.feature,
  86. organization_id: params.organization?.id,
  87. ...(params.projectId === undefined ? {} : {project_id: params.projectId}),
  88. };
  89. const url = `/organizations/${params.organization?.slug}/prompts-activity/`;
  90. const response: PromptResponse = await api.requestPromise(url, {
  91. query,
  92. });
  93. if (response?.data) {
  94. return {
  95. dismissedTime: response.data.dismissed_ts,
  96. snoozedTime: response.data.snoozed_ts,
  97. };
  98. }
  99. return null;
  100. }
  101. export const makePromptsCheckQueryKey = ({
  102. feature,
  103. organization,
  104. projectId,
  105. }: PromptCheckParams): ApiQueryKey => {
  106. const url = `/organizations/${organization?.slug}/prompts-activity/`;
  107. return [
  108. url,
  109. {query: {feature, organization_id: organization?.id, project_id: projectId}},
  110. ];
  111. };
  112. export function usePromptsCheck(
  113. {feature, organization, projectId}: PromptCheckParams,
  114. {enabled = true, ...options}: Partial<UseApiQueryOptions<PromptResponse>> = {}
  115. ) {
  116. return useApiQuery<PromptResponse>(
  117. makePromptsCheckQueryKey({feature, organization, projectId}),
  118. {
  119. staleTime: 120000,
  120. retry: false,
  121. enabled: defined(organization) && enabled,
  122. ...options,
  123. }
  124. );
  125. }
  126. export function usePrompt({
  127. feature,
  128. organization,
  129. projectId,
  130. daysToSnooze,
  131. options,
  132. }: {
  133. feature: string;
  134. organization: Organization | null;
  135. daysToSnooze?: number;
  136. options?: Partial<UseApiQueryOptions<PromptResponse>>;
  137. projectId?: string;
  138. }) {
  139. const api = useApi({persistInFlight: true});
  140. const prompt = usePromptsCheck({feature, organization, projectId}, options);
  141. const queryClient = useQueryClient();
  142. const isPromptDismissed =
  143. prompt.isSuccess && prompt.data.data
  144. ? promptIsDismissed(
  145. {
  146. dismissedTime: prompt.data.data.dismissed_ts,
  147. snoozedTime: prompt.data.data.snoozed_ts,
  148. },
  149. daysToSnooze
  150. )
  151. : undefined;
  152. const dismissPrompt = useCallback(() => {
  153. if (!organization) {
  154. return;
  155. }
  156. promptsUpdate(api, {
  157. organization,
  158. projectId,
  159. feature,
  160. status: 'dismissed',
  161. });
  162. // Update cached query data
  163. // Will set prompt to dismissed
  164. setApiQueryData<PromptResponse>(
  165. queryClient,
  166. makePromptsCheckQueryKey({
  167. organization,
  168. feature,
  169. projectId,
  170. }),
  171. () => {
  172. const dimissedTs = new Date().getTime() / 1000;
  173. return {
  174. data: {dismissed_ts: dimissedTs},
  175. features: {[feature]: {dismissed_ts: dimissedTs}},
  176. };
  177. }
  178. );
  179. }, [api, feature, organization, projectId, queryClient]);
  180. const snoozePrompt = useCallback(() => {
  181. if (!organization) {
  182. return;
  183. }
  184. promptsUpdate(api, {
  185. organization,
  186. projectId,
  187. feature,
  188. status: 'snoozed',
  189. });
  190. // Update cached query data
  191. // Will set prompt to snoozed
  192. setApiQueryData<PromptResponse>(
  193. queryClient,
  194. makePromptsCheckQueryKey({
  195. organization,
  196. feature,
  197. projectId,
  198. }),
  199. () => {
  200. const snoozedTs = new Date().getTime() / 1000;
  201. return {
  202. data: {snoozed_ts: snoozedTs},
  203. features: {[feature]: {snoozed_ts: snoozedTs}},
  204. };
  205. }
  206. );
  207. }, [api, feature, organization, projectId, queryClient]);
  208. const showPrompt = useCallback(() => {
  209. if (!organization) {
  210. return;
  211. }
  212. promptsUpdate(api, {
  213. organization,
  214. projectId,
  215. feature,
  216. status: 'visible',
  217. });
  218. // Update cached query data
  219. // Will clear the status/timestamps of a prompt that is dismissed or snoozed
  220. setApiQueryData<PromptResponse>(
  221. queryClient,
  222. makePromptsCheckQueryKey({
  223. organization,
  224. feature,
  225. projectId,
  226. }),
  227. () => {
  228. return {
  229. data: {},
  230. features: {[feature]: {}},
  231. };
  232. }
  233. );
  234. }, [api, feature, organization, projectId, queryClient]);
  235. return {
  236. isLoading: prompt.isPending,
  237. isError: prompt.isError,
  238. isPromptDismissed,
  239. dismissPrompt,
  240. snoozePrompt,
  241. showPrompt,
  242. };
  243. }
  244. /**
  245. * Get the status of many prompts
  246. */
  247. export async function batchedPromptsCheck<T extends readonly string[]>(
  248. api: Client,
  249. features: T,
  250. params: {
  251. organization: OrganizationSummary;
  252. projectId?: string;
  253. }
  254. ): Promise<{[key in T[number]]: PromptData}> {
  255. const query = {
  256. feature: features,
  257. organization_id: params.organization.id,
  258. ...(params.projectId === undefined ? {} : {project_id: params.projectId}),
  259. };
  260. const url = `/organizations/${params.organization.slug}/prompts-activity/`;
  261. const response: PromptResponse = await api.requestPromise(url, {
  262. query,
  263. });
  264. const responseFeatures = response?.features;
  265. const result: {[key in T[number]]?: PromptData} = {};
  266. if (!responseFeatures) {
  267. return result as {[key in T[number]]: PromptData};
  268. }
  269. for (const featureName of features) {
  270. const item = responseFeatures[featureName];
  271. if (item) {
  272. result[featureName] = {
  273. dismissedTime: item.dismissed_ts,
  274. snoozedTime: item.snoozed_ts,
  275. };
  276. } else {
  277. result[featureName] = null;
  278. }
  279. }
  280. return result as {[key in T[number]]: PromptData};
  281. }