Skip to content

Commit

Permalink
refactor(webui): delete bundle reloading due to authorization issues …
Browse files Browse the repository at this point in the history
…with Metro (#56)
  • Loading branch information
byCedric authored May 18, 2024
1 parent dc16bbe commit 67e785a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 68 deletions.
33 changes: 0 additions & 33 deletions webui/src/app/--/bundles/[bundle]/reload+api.ts

This file was deleted.

45 changes: 10 additions & 35 deletions webui/src/providers/bundle.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useQuery } from '@tanstack/react-query';
import { useLocalSearchParams } from 'expo-router';
import {
type PropsWithChildren,
createContext,
useContext,
useMemo,
useEffect,
useCallback,
} from 'react';
import { type PropsWithChildren, createContext, useContext, useMemo, useEffect } from 'react';

import { type BundleDeltaResponse } from '~/app/--/bundles/[bundle]/delta+api';
import { StateInfo } from '~/components/StateInfo';
import { Button } from '~/ui/Button';
import { Spinner } from '~/ui/Spinner';
import { ToastAction, type ToasterToast, useToast } from '~/ui/Toast';
import { type ToasterToast, useToast } from '~/ui/Toast';
import { fetchApi, handleApiError } from '~/utils/api';
import { type PartialAtlasBundle } from '~core/data/types';

Expand Down Expand Up @@ -94,34 +86,24 @@ export function BundleDeltaToast({
bundle: Pick<PartialAtlasBundle, 'id'>;
modulePath?: string;
}) {
const client = useQueryClient();
const toaster = useToast();

const deltaResponse = useBundleDeltaData(bundle.id);
const bundleDelta = deltaResponse.data?.delta;

const refetchBundleData = useCallback(
() =>
fetchApi(`/bundles/${bundle.id}/reload`)
.then(handleApiError)
.then(() => client.refetchQueries({ queryKey: ['bundles', bundle.id], type: 'active' })),
[bundle.id]
);

useEffect(() => {
if (!bundleDelta) return;

if (modulePath) {
if (bundleDelta.deletedPaths.includes(modulePath)) {
toaster.toast(toastModuleDeleted(bundle.id));
} else if (bundleDelta.modifiedPaths.includes(modulePath)) {
refetchBundleData().then(() => toaster.toast(toastModuleModified(bundle.id)));
toaster.toast(toastModuleModified(bundle.id));
}
return;
}

toaster.toast(toastBundleUpdate(bundle.id, refetchBundleData));
}, [bundle.id, bundleDelta, refetchBundleData, modulePath]);
toaster.toast(toastBundleUpdate(bundle.id));
}, [bundle.id, bundleDelta, modulePath]);

return null;
}
Expand All @@ -130,30 +112,23 @@ function toastModuleModified(bundleId: string): ToasterToast {
return {
id: `bundle-delta-${bundleId}`,
title: 'Module modified',
description: 'This module is updated to reflect the latest changes.',
description: 'This file has changed, open your app before reloading this page.',
};
}

function toastModuleDeleted(bundleId: string): ToasterToast {
return {
id: `bundle-delta-${bundleId}`,
title: 'Module deleted',
description: 'This file is deleted since latest build, and is no longer available.',
description: 'This file is deleted since last build, and is no longer available.',
};
}

function toastBundleUpdate(bundleId: string, refetchBundleData: () => any): ToasterToast {
function toastBundleUpdate(bundleId: string): ToasterToast {
return {
id: `bundle-delta-${bundleId}`,
title: 'Bundle outdated',
description: 'The code was changed since last build.',
action: (
<ToastAction altText="Reload bundle">
<Button variant="secondary" size="xs" onClick={refetchBundleData}>
Reload bundle
</Button>
</ToastAction>
),
description: 'Code was changed since last build, open your app before reloading this page.',
};
}

Expand Down

0 comments on commit 67e785a

Please sign in to comment.