Browse Source

ref(crons): Switch to useApiQuery on monitorCheckins (#49112)

David Wang 1 year ago
parent
commit
14a6b66427
1 changed files with 27 additions and 26 deletions
  1. 27 26
      static/app/views/monitors/components/monitorCheckIns.tsx

+ 27 - 26
static/app/views/monitors/components/monitorCheckIns.tsx

@@ -5,15 +5,17 @@ import {Button} from 'sentry/components/button';
 import {SectionHeading} from 'sentry/components/charts/styles';
 import {SectionHeading} from 'sentry/components/charts/styles';
 import DateTime from 'sentry/components/dateTime';
 import DateTime from 'sentry/components/dateTime';
 import Duration from 'sentry/components/duration';
 import Duration from 'sentry/components/duration';
+import LoadingError from 'sentry/components/loadingError';
+import LoadingIndicator from 'sentry/components/loadingIndicator';
 import Pagination from 'sentry/components/pagination';
 import Pagination from 'sentry/components/pagination';
 import {PanelTable} from 'sentry/components/panels';
 import {PanelTable} from 'sentry/components/panels';
 import StatusIndicator from 'sentry/components/statusIndicator';
 import StatusIndicator from 'sentry/components/statusIndicator';
 import Text from 'sentry/components/text';
 import Text from 'sentry/components/text';
 import {IconDownload} from 'sentry/icons';
 import {IconDownload} from 'sentry/icons';
 import {t, tct} from 'sentry/locale';
 import {t, tct} from 'sentry/locale';
-import {space} from 'sentry/styles/space';
 import {defined} from 'sentry/utils';
 import {defined} from 'sentry/utils';
-import useApiRequests from 'sentry/utils/useApiRequests';
+import {useApiQuery} from 'sentry/utils/queryClient';
+import {useLocation} from 'sentry/utils/useLocation';
 import {
 import {
   CheckIn,
   CheckIn,
   CheckInStatus,
   CheckInStatus,
@@ -27,10 +29,6 @@ type Props = {
   orgId: string;
   orgId: string;
 };
 };
 
 
-type State = {
-  checkInList: CheckIn[];
-};
-
 const checkStatusToIndicatorStatus: Record<
 const checkStatusToIndicatorStatus: Record<
   CheckInStatus,
   CheckInStatus,
   'success' | 'error' | 'muted' | 'warning'
   'success' | 'error' | 'muted' | 'warning'
@@ -51,23 +49,32 @@ const statusToText: Record<CheckInStatus, string> = {
 };
 };
 
 
 function MonitorCheckIns({monitor, monitorEnv, orgId}: Props) {
 function MonitorCheckIns({monitor, monitorEnv, orgId}: Props) {
-  const {data, hasError, renderComponent} = useApiRequests<State>({
-    endpoints: [
-      [
-        'checkInList',
-        `/organizations/${orgId}/monitors/${monitor.slug}/checkins/`,
-        {query: {per_page: '10', environment: monitorEnv.name}},
-        {paginate: true},
-      ],
-    ],
-  });
+  const location = useLocation();
+  const queryKey = [
+    `/organizations/${orgId}/monitors/${monitor.slug}/checkins/`,
+    {query: {per_page: '10', environment: monitorEnv.name, ...location.query}},
+  ] as const;
+
+  const {
+    data: checkInList,
+    getResponseHeader,
+    isLoading,
+    isError,
+  } = useApiQuery<CheckIn[]>(queryKey, {staleTime: 0});
+
+  if (isLoading) {
+    return <LoadingIndicator />;
+  }
+  if (isError) {
+    return <LoadingError />;
+  }
 
 
   const generateDownloadUrl = (checkin: CheckIn) =>
   const generateDownloadUrl = (checkin: CheckIn) =>
     `/api/0/organizations/${orgId}/monitors/${monitor.slug}/checkins/${checkin.id}/attachment/`;
     `/api/0/organizations/${orgId}/monitors/${monitor.slug}/checkins/${checkin.id}/attachment/`;
 
 
   const emptyCell = <Text>{'\u2014'}</Text>;
   const emptyCell = <Text>{'\u2014'}</Text>;
 
 
-  const renderedComponent = renderComponent(
+  return (
     <React.Fragment>
     <React.Fragment>
       <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
       <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
       <PanelTable
       <PanelTable
@@ -79,7 +86,7 @@ function MonitorCheckIns({monitor, monitorEnv, orgId}: Props) {
           t('Timestamp'),
           t('Timestamp'),
         ]}
         ]}
       >
       >
-        {data.checkInList?.map(checkIn => (
+        {checkInList.map(checkIn => (
           <React.Fragment key={checkIn.id}>
           <React.Fragment key={checkIn.id}>
             <Status>
             <Status>
               <StatusIndicator
               <StatusIndicator
@@ -106,7 +113,7 @@ function MonitorCheckIns({monitor, monitorEnv, orgId}: Props) {
                 icon={<IconDownload size="xs" />}
                 icon={<IconDownload size="xs" />}
                 href={generateDownloadUrl(checkIn)}
                 href={generateDownloadUrl(checkIn)}
               >
               >
-                Attachment
+                {t('Attachment')}
               </Button>
               </Button>
             ) : (
             ) : (
               emptyCell
               emptyCell
@@ -115,11 +122,9 @@ function MonitorCheckIns({monitor, monitorEnv, orgId}: Props) {
           </React.Fragment>
           </React.Fragment>
         ))}
         ))}
       </PanelTable>
       </PanelTable>
-      <Pagination pageLinks={data.checkInListPageLinks} />
+      <Pagination pageLinks={getResponseHeader?.('Link')} />
     </React.Fragment>
     </React.Fragment>
   );
   );
-
-  return hasError ? <ErrorWrapper>{renderedComponent}</ErrorWrapper> : renderedComponent;
 }
 }
 
 
 export default MonitorCheckIns;
 export default MonitorCheckIns;
@@ -132,7 +137,3 @@ const Status = styled('div')`
 const Timestamp = styled(DateTime)`
 const Timestamp = styled(DateTime)`
   color: ${p => p.theme.subText};
   color: ${p => p.theme.subText};
 `;
 `;
-
-const ErrorWrapper = styled('div')`
-  margin: ${space(3)} ${space(3)} 0;
-`;