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,7 +6,13 @@ import { BusterLogo } from '@/assets/svg/BusterLogo';
import { Button } from '@/components/ui/buttons';
import { Title } from '@/components/ui/typography';
import { useSupabaseContext } from '@/context/Supabase';
import { BusterRoutes, createBusterRoute } from '@/routes';
import {
BusterRoutes,
createBusterRoute,
createPathnameToBusterRoute,
extractPathParamsFromRoute,
getEmbedAssetToRegularAsset
} from '@/routes';
export const AppNoPageAccess: React.FC<{
assetId: string;
@ -14,20 +20,23 @@ export const AppNoPageAccess: React.FC<{
const isAnonymousUser = useSupabaseContext((x) => x.isAnonymousUser);
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;
if (shouldShowLogin) {
const currentUrl = typeof window !== 'undefined'
? `${window.location.pathname}${window.location.search}`
: '';
const currentUrl =
typeof window !== 'undefined' ? `${window.location.pathname}${window.location.search}` : '';
const linkUrl = getEmbedAssetToRegularAsset(currentUrl);
return {
buttonText: 'Login',
linkUrl: createBusterRoute({
route: BusterRoutes.AUTH_LOGIN,
next: encodeURIComponent(currentUrl)
buttonText: 'Login to view asset',
linkUrl:
linkUrl ||
createBusterRoute({
route: BusterRoutes.AUTH_LOGIN
})
};
}

View File

@ -1,6 +1,6 @@
import { NextRequest } from 'next/server';
import { describe, expect, it, vi } from 'vitest';
import { getEmbedAssetRedirect } from './assetPageChecks';
import { getEmbedAssetRedirect, getEmbedAssetToRegularAsset } from './assetPageChecks';
vi.mock('next/server', () => ({
NextRequest: vi.fn().mockImplementation((url) => ({
@ -66,3 +66,33 @@ describe('getEmbedAssetRedirect', () => {
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
};
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[] = [
BusterRoutes.APP_METRIC_ID_CHART,
BusterRoutes.APP_DASHBOARD_ID,
@ -87,3 +93,13 @@ export const getEmbedAssetRedirect = (request: NextRequest): string | 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 {
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 = {
@ -12,4 +13,8 @@ export type BusterEmbedRoutesWithArgs = {
route: BusterEmbedRoutes.EMBED_DASHBOARD_ID;
dashboardId: string;
};
[BusterEmbedRoutes.EMBED_COLLECTION_ID]: {
route: BusterEmbedRoutes.EMBED_COLLECTION_ID;
collectionId: string;
};
};