prompts.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 = prompt.isSuccess
  143. ? promptIsDismissed(
  144. {
  145. dismissedTime: prompt.data?.data?.dismissed_ts,
  146. snoozedTime: prompt.data?.data?.snoozed_ts,
  147. },
  148. daysToSnooze
  149. )
  150. : undefined;
  151. const dismissPrompt = useCallback(() => {
  152. if (!organization) {
  153. return;
  154. }
  155. promptsUpdate(api, {
  156. organization,
  157. projectId,
  158. feature,
  159. status: 'dismissed',
  160. });
  161. // Update cached query data
  162. // Will set prompt to dismissed
  163. setApiQueryData<PromptResponse>(
  164. queryClient,
  165. makePromptsCheckQueryKey({
  166. organization,
  167. feature,
  168. projectId,
  169. }),
  170. () => {
  171. const dimissedTs = new Date().getTime() / 1000;
  172. return {
  173. data: {dismissed_ts: dimissedTs},
  174. features: {[feature]: {dismissed_ts: dimissedTs}},
  175. };
  176. }
  177. );
  178. }, [api, feature, organization, projectId, queryClient]);
  179. const snoozePrompt = useCallback(() => {
  180. if (!organization) {
  181. return;
  182. }
  183. promptsUpdate(api, {
  184. organization,
  185. projectId,
  186. feature,
  187. status: 'snoozed',
  188. });
  189. // Update cached query data
  190. // Will set prompt to snoozed
  191. setApiQueryData<PromptResponse>(
  192. queryClient,
  193. makePromptsCheckQueryKey({
  194. organization,
  195. feature,
  196. projectId,
  197. }),
  198. () => {
  199. const snoozedTs = new Date().getTime() / 1000;
  200. return {
  201. data: {snoozed_ts: snoozedTs},
  202. features: {[feature]: {snoozed_ts: snoozedTs}},
  203. };
  204. }
  205. );
  206. }, [api, feature, organization, projectId, queryClient]);
  207. const showPrompt = useCallback(() => {
  208. if (!organization) {
  209. return;
  210. }
  211. promptsUpdate(api, {
  212. organization,
  213. projectId,
  214. feature,
  215. status: 'visible',
  216. });
  217. // Update cached query data
  218. // Will clear the status/timestamps of a prompt that is dismissed or snoozed
  219. setApiQueryData<PromptResponse>(
  220. queryClient,
  221. makePromptsCheckQueryKey({
  222. organization,
  223. feature,
  224. projectId,
  225. }),
  226. () => {
  227. return {
  228. data: {},
  229. features: {[feature]: {}},
  230. };
  231. }
  232. );
  233. }, [api, feature, organization, projectId, queryClient]);
  234. return {
  235. isLoading: prompt.isPending,
  236. isError: prompt.isError,
  237. isPromptDismissed,
  238. dismissPrompt,
  239. snoozePrompt,
  240. showPrompt,
  241. };
  242. }
  243. /**
  244. * Get the status of many prompts
  245. */
  246. export async function batchedPromptsCheck<T extends readonly string[]>(
  247. api: Client,
  248. features: T,
  249. params: {
  250. organization: OrganizationSummary;
  251. projectId?: string;
  252. }
  253. ): Promise<{[key in T[number]]: PromptData}> {
  254. const query = {
  255. feature: features,
  256. organization_id: params.organization.id,
  257. ...(params.projectId === undefined ? {} : {project_id: params.projectId}),
  258. };
  259. const url = `/organizations/${params.organization.slug}/prompts-activity/`;
  260. const response: PromptResponse = await api.requestPromise(url, {
  261. query,
  262. });
  263. const responseFeatures = response?.features;
  264. const result: {[key in T[number]]?: PromptData} = {};
  265. if (!responseFeatures) {
  266. return result as {[key in T[number]]: PromptData};
  267. }
  268. for (const featureName of features) {
  269. const item = responseFeatures[featureName];
  270. if (item) {
  271. result[featureName] = {
  272. dismissedTime: item.dismissed_ts,
  273. snoozedTime: item.snoozed_ts,
  274. };
  275. } else {
  276. result[featureName] = null;
  277. }
  278. }
  279. return result as {[key in T[number]]: PromptData};
  280. }