Browse Source

fix(pre-load-data): Do not load org/proj/teams data if preload data is false (#84065)

Priscila Oliveira 1 month ago
parent
commit
7b8d68b0d0

+ 2 - 2
static/app/views/acceptOrganizationInvite/index.spec.tsx

@@ -60,7 +60,7 @@ describe('AcceptOrganizationInvite', function () {
 
     await userEvent.click(joinButton!);
     expect(acceptMock).toHaveBeenCalled();
-    expect(router.replace).toHaveBeenCalledWith('/org-slug/');
+    expect(window.location.href).toBe('/org-slug/');
   });
 
   it('can accept invitation on customer-domains', async function () {
@@ -100,7 +100,7 @@ describe('AcceptOrganizationInvite', function () {
 
     await userEvent.click(joinButton!);
     expect(acceptMock).toHaveBeenCalled();
-    expect(router.replace).toHaveBeenCalledWith('/org-slug/');
+    expect(window.location.href).toBe('/org-slug/');
   });
 
   it('renders error message', function () {

+ 3 - 2
static/app/views/acceptOrganizationInvite/index.tsx

@@ -13,7 +13,6 @@ import {t, tct} from 'sentry/locale';
 import ConfigStore from 'sentry/stores/configStore';
 import {space} from 'sentry/styles/space';
 import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
-import {browserHistory} from 'sentry/utils/browserHistory';
 import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
 
 type InviteDetails = {
@@ -85,7 +84,9 @@ class AcceptOrganizationInvite extends DeprecatedAsyncComponent<Props, State> {
           method: 'POST',
         });
       }
-      browserHistory.replace(`/${this.state.inviteDetails.orgSlug}/`);
+      // This forces a hard refresh, needed for the app to refetch the initial config
+      // Please see https://github.com/getsentry/sentry/blob/5f1fef10806db1d4d048912702f5c12cb38c2c08/static/app/bootstrap/index.tsx#L20
+      window.location.href = `/${this.state.inviteDetails.orgSlug}/`;
     } catch {
       this.setState({acceptError: true});
     }

+ 27 - 3
static/app/views/app/index.tsx

@@ -141,6 +141,12 @@ function App({children, params}: Props) {
   useEffect(() => GuideStore.onURLChange(), [location]);
 
   useEffect(() => {
+    // Skip loading organization-related data before the user is logged in,
+    // because it triggers a 401 error in the UI.
+    if (!config.shouldPreloadData) {
+      return undefined;
+    }
+
     loadOrganizations();
     checkInternalHealth();
 
@@ -169,7 +175,13 @@ function App({children, params}: Props) {
 
     // When the app is unloaded clear the organizationst list
     return () => OrganizationsStore.load([]);
-  }, [loadOrganizations, checkInternalHealth, config.messages, user]);
+  }, [
+    loadOrganizations,
+    checkInternalHealth,
+    config.messages,
+    user,
+    config.shouldPreloadData,
+  ]);
 
   function clearUpgrade() {
     ConfigStore.set('needsUpgrade', false);
@@ -237,6 +249,18 @@ function App({children, params}: Props) {
     return children;
   }
 
+  const renderOrganizationContextProvider = useCallback(
+    (content: React.ReactNode) => {
+      // Skip loading organization-related data before the user is logged in,
+      // because it triggers a 401 error in the UI.
+      if (!config.shouldPreloadData) {
+        return content;
+      }
+      return <OrganizationContextProvider>{content}</OrganizationContextProvider>;
+    },
+    [config.shouldPreloadData]
+  );
+
   // Used to restore focus to the container after closing the modal
   const mainContainerRef = useRef<HTMLDivElement>(null);
   const handleModalClose = useCallback(() => mainContainerRef.current?.focus?.(), []);
@@ -245,7 +269,7 @@ function App({children, params}: Props) {
     <Profiler id="App" onRender={onRenderCallback}>
       <LastKnownRouteContextProvider>
         <RouteAnalyticsContextProvider>
-          <OrganizationContextProvider>
+          {renderOrganizationContextProvider(
             <AsyncSDKIntegrationContextProvider>
               <GlobalFeedbackForm>
                 <GlobalDrawer>
@@ -258,7 +282,7 @@ function App({children, params}: Props) {
                 </GlobalDrawer>
               </GlobalFeedbackForm>
             </AsyncSDKIntegrationContextProvider>
-          </OrganizationContextProvider>
+          )}
         </RouteAnalyticsContextProvider>
       </LastKnownRouteContextProvider>
     </Profiler>