reroute to asset page check

This commit is contained in:
Nate Kelley 2025-07-22 11:34:43 -06:00
parent 4e3b0004e6
commit d3bfcbca1b
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
4 changed files with 77 additions and 17 deletions

View File

@ -6,32 +6,41 @@ import { BusterLogo } from '@/assets/svg/BusterLogo';
import { Button } from '@/components/ui/buttons'; import { Button } from '@/components/ui/buttons';
import { Title } from '@/components/ui/typography'; import { Title } from '@/components/ui/typography';
import { useSupabaseContext } from '@/context/Supabase'; import { useSupabaseContext } from '@/context/Supabase';
import { BusterRoutes, createBusterRoute } from '@/routes'; import {
BusterRoutes,
createBusterRoute,
createPathnameToBusterRoute,
extractPathParamsFromRoute,
getEmbedAssetToRegularAsset
} from '@/routes';
export const AppNoPageAccess: React.FC<{ export const AppNoPageAccess: React.FC<{
assetId: string; assetId: string;
}> = React.memo(({ assetId }) => { }> = React.memo(({ assetId }) => {
const isAnonymousUser = useSupabaseContext((x) => x.isAnonymousUser); const isAnonymousUser = useSupabaseContext((x) => x.isAnonymousUser);
const { buttonText, linkUrl } = useMemo(() => { const { buttonText, linkUrl } = useMemo(() => {
const isEmbedPage = typeof window !== 'undefined' && window.location.pathname.startsWith('/embed'); const isEmbedPage =
typeof window !== 'undefined' && window.location.pathname.startsWith('/embed');
const shouldShowLogin = isAnonymousUser || isEmbedPage; const shouldShowLogin = isAnonymousUser || isEmbedPage;
if (shouldShowLogin) { if (shouldShowLogin) {
const currentUrl = typeof window !== 'undefined' const currentUrl =
? `${window.location.pathname}${window.location.search}` typeof window !== 'undefined' ? `${window.location.pathname}${window.location.search}` : '';
: '';
const linkUrl = getEmbedAssetToRegularAsset(currentUrl);
return { return {
buttonText: 'Login', buttonText: 'Login to view asset',
linkUrl: createBusterRoute({ linkUrl:
route: BusterRoutes.AUTH_LOGIN, linkUrl ||
next: encodeURIComponent(currentUrl) createBusterRoute({
}) route: BusterRoutes.AUTH_LOGIN
})
}; };
} }
return { return {
buttonText: 'Go home', buttonText: 'Go home',
linkUrl: createBusterRoute({ linkUrl: createBusterRoute({

View File

@ -1,6 +1,6 @@
import { NextRequest } from 'next/server'; import { NextRequest } from 'next/server';
import { describe, expect, it, vi } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import { getEmbedAssetRedirect } from './assetPageChecks'; import { getEmbedAssetRedirect, getEmbedAssetToRegularAsset } from './assetPageChecks';
vi.mock('next/server', () => ({ vi.mock('next/server', () => ({
NextRequest: vi.fn().mockImplementation((url) => ({ NextRequest: vi.fn().mockImplementation((url) => ({
@ -66,3 +66,33 @@ describe('getEmbedAssetRedirect', () => {
expect(redirect).toBeUndefined(); expect(redirect).toBeUndefined();
}); });
}); });
describe('getEmbedAssetToRegularAsset', () => {
it('should convert embed metric URL to regular metric chart URL', () => {
// Test converting an embed metric URL to the corresponding regular app metric chart URL
const embedMetricUrl = '/embed/metrics/123';
const result = getEmbedAssetToRegularAsset(embedMetricUrl);
expect(result).toBe('/app/metrics/123/chart');
});
it('should convert embed dashboard URL to regular dashboard URL', () => {
// Test converting an embed dashboard URL to the corresponding regular app dashboard URL
const embedDashboardUrl = '/embed/dashboards/456';
const result = getEmbedAssetToRegularAsset(embedDashboardUrl);
expect(result).toBe('/app/dashboards/456');
});
it('should return undefined for regular app URLs (non-embed)', () => {
// Test that regular app URLs that are not embed URLs return undefined
const regularAppUrl = '/app/metrics/123/chart';
const result = getEmbedAssetToRegularAsset(regularAppUrl);
expect(result).toBeUndefined();
});
it('should return undefined for invalid or unknown URLs', () => {
// Test that completely unknown/invalid URLs return undefined
const invalidUrl = '/some/random/unknown/path';
const result = getEmbedAssetToRegularAsset(invalidUrl);
expect(result).toBeUndefined();
});
});

View File

@ -45,6 +45,12 @@ const assetRedirectRecord: Partial<Record<BusterRoutes, BusterRoutes>> = {
[BusterRoutes.APP_CHAT_ID_DASHBOARD_ID_METRIC_ID_SQL]: BusterRoutes.EMBED_METRIC_ID [BusterRoutes.APP_CHAT_ID_DASHBOARD_ID_METRIC_ID_SQL]: BusterRoutes.EMBED_METRIC_ID
}; };
const embedAssetToRegularAssetRecord: Record<BusterEmbedRoutes, BusterRoutes> = {
[BusterRoutes.EMBED_METRIC_ID]: BusterRoutes.APP_METRIC_ID_CHART,
[BusterRoutes.EMBED_DASHBOARD_ID]: BusterRoutes.APP_DASHBOARD_ID,
[BusterRoutes.EMBED_COLLECTION_ID]: BusterRoutes.APP_COLLECTIONS_ID
};
const publicPages: BusterRoutes[] = [ const publicPages: BusterRoutes[] = [
BusterRoutes.APP_METRIC_ID_CHART, BusterRoutes.APP_METRIC_ID_CHART,
BusterRoutes.APP_DASHBOARD_ID, BusterRoutes.APP_DASHBOARD_ID,
@ -87,3 +93,13 @@ export const getEmbedAssetRedirect = (request: NextRequest): string | undefined
return undefined; return undefined;
}; };
export const getEmbedAssetToRegularAsset = (pathnameAndQueryParams: string) => {
const route = createPathnameToBusterRoute(pathnameAndQueryParams);
const matched = embedAssetToRegularAssetRecord[route as BusterEmbedRoutes];
if (matched) {
const params = extractPathParamsFromRoute(pathnameAndQueryParams);
return createBusterRoute({ route: matched, ...(params as any) });
}
};

View File

@ -1,6 +1,7 @@
export enum BusterEmbedRoutes { export enum BusterEmbedRoutes {
EMBED_METRIC_ID = '/embed/metrics/:metricId', EMBED_METRIC_ID = '/embed/metrics/:metricId',
EMBED_DASHBOARD_ID = '/embed/dashboards/:dashboardId' EMBED_DASHBOARD_ID = '/embed/dashboards/:dashboardId',
EMBED_COLLECTION_ID = '/embed/collections/:collectionId'
} }
export type BusterEmbedRoutesWithArgs = { export type BusterEmbedRoutesWithArgs = {
@ -12,4 +13,8 @@ export type BusterEmbedRoutesWithArgs = {
route: BusterEmbedRoutes.EMBED_DASHBOARD_ID; route: BusterEmbedRoutes.EMBED_DASHBOARD_ID;
dashboardId: string; dashboardId: string;
}; };
[BusterEmbedRoutes.EMBED_COLLECTION_ID]: {
route: BusterEmbedRoutes.EMBED_COLLECTION_ID;
collectionId: string;
};
}; };