embedded route check

This commit is contained in:
Nate Kelley 2025-10-01 15:02:58 -06:00
parent 88e3e1e6c5
commit 68b8fc4b4b
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
4 changed files with 56 additions and 7 deletions

View File

@ -1,9 +1,11 @@
import { useMatch, useMatches } from '@tanstack/react-router'; import { useMatch, useMatches, useMatchRoute } from '@tanstack/react-router';
import { Route as EmbedRoute } from '@/routes/embed';
export const useIsEmbed = () => { export const useIsEmbed = () => {
const match = useMatch({ const matchRoute = useMatchRoute();
from: '/embed', const matches = matchRoute({
to: EmbedRoute.id,
fuzzy: true,
}); });
console.log(match); return !!matches;
return !!match?.id;
}; };

View File

@ -8,9 +8,12 @@ import {
import findLast from 'lodash/findLast'; import findLast from 'lodash/findLast';
export const useSelectedAssetType = (): NonNullable<StaticDataRouteOption['assetType']> => { export const useSelectedAssetType = (): NonNullable<StaticDataRouteOption['assetType']> => {
const matches = useMatches(); const lastMatch = useMatches({
select: (matches) => {
return findLast(matches, (match) => match.staticData?.assetType);
},
});
const lastMatch = findLast(matches, (match) => match.staticData?.assetType);
if (typeof lastMatch === 'number') { if (typeof lastMatch === 'number') {
return 'chat'; return 'chat';
} }

View File

@ -1,6 +1,7 @@
import React, { useCallback, useMemo } from 'react'; import React, { useCallback, useMemo } from 'react';
import type { BusterChatMessage, BusterChatResponseMessage_file } from '@/api/asset_interfaces'; import type { BusterChatMessage, BusterChatResponseMessage_file } from '@/api/asset_interfaces';
import { useGetChatMessage } from '@/api/buster_rest/chats'; import { useGetChatMessage } from '@/api/buster_rest/chats';
import { useIsEmbed } from '@/context/BusterAssets/useIsEmbed';
import { createChatAssetRoute } from '@/lib/routes/createSimpleAssetRoute'; import { createChatAssetRoute } from '@/lib/routes/createSimpleAssetRoute';
import type { ILinkProps } from '@/types/routes'; import type { ILinkProps } from '@/types/routes';
import type { ChatResponseMessageProps } from '../ChatResponseMessageSelector'; import type { ChatResponseMessageProps } from '../ChatResponseMessageSelector';
@ -21,12 +22,14 @@ export const ChatResponseMessage_File: React.FC<ChatResponseMessageProps> = Reac
const { file_type } = responseMessage; const { file_type } = responseMessage;
const { isSelectedFile } = useGetIsSelectedFile({ responseMessage }); const { isSelectedFile } = useGetIsSelectedFile({ responseMessage });
const isEmbed = useIsEmbed();
const linkParams = createChatAssetRoute({ const linkParams = createChatAssetRoute({
asset_type: file_type, asset_type: file_type,
id: responseMessage.id, id: responseMessage.id,
chatId, chatId,
versionNumber: responseMessage.version_number, versionNumber: responseMessage.version_number,
isEmbed,
}) as unknown as ILinkProps; }) as unknown as ILinkProps;
const SelectedComponent = useMemo(() => { const SelectedComponent = useMemo(() => {

View File

@ -59,8 +59,16 @@ export const createChatAssetRoute = (asset: {
id: string | undefined; id: string | undefined;
chatId: string; chatId: string;
versionNumber?: number; versionNumber?: number;
isEmbed: boolean;
}) => { }) => {
if (asset.asset_type === 'chat' || !asset.asset_type || !asset.id) { if (asset.asset_type === 'chat' || !asset.asset_type || !asset.id) {
if (asset.isEmbed) {
return defineLink({
to: '/embed/chats/$chatId',
params: { chatId: asset.chatId },
});
}
return defineLink({ return defineLink({
to: '/app/chats/$chatId', to: '/app/chats/$chatId',
params: { chatId: asset.chatId }, params: { chatId: asset.chatId },
@ -68,6 +76,13 @@ export const createChatAssetRoute = (asset: {
} }
if (asset.asset_type === 'metric_file') { if (asset.asset_type === 'metric_file') {
if (asset.isEmbed) {
return defineLink({
to: '/embed/chats/$chatId/metrics/$metricId',
params: { metricId: asset.id || '', chatId: asset.chatId },
search: { metric_version_number: asset.versionNumber },
});
}
return defineLink({ return defineLink({
to: '/app/chats/$chatId/metrics/$metricId', to: '/app/chats/$chatId/metrics/$metricId',
params: { metricId: asset.id || '', chatId: asset.chatId }, params: { metricId: asset.id || '', chatId: asset.chatId },
@ -76,6 +91,13 @@ export const createChatAssetRoute = (asset: {
} }
if (asset.asset_type === 'dashboard_file') { if (asset.asset_type === 'dashboard_file') {
if (asset.isEmbed) {
return defineLink({
to: '/embed/chats/$chatId/dashboards/$dashboardId',
params: { dashboardId: asset.id || '', chatId: asset.chatId },
search: { dashboard_version_number: asset.versionNumber },
});
}
return defineLink({ return defineLink({
to: '/app/chats/$chatId/dashboards/$dashboardId', to: '/app/chats/$chatId/dashboards/$dashboardId',
params: { dashboardId: asset.id || '', chatId: asset.chatId }, params: { dashboardId: asset.id || '', chatId: asset.chatId },
@ -84,6 +106,13 @@ export const createChatAssetRoute = (asset: {
} }
if (asset.asset_type === 'report_file') { if (asset.asset_type === 'report_file') {
if (asset.isEmbed) {
return defineLink({
to: '/embed/chats/$chatId/reports/$reportId',
params: { reportId: asset.id || '', chatId: asset.chatId },
search: { report_version_number: asset.versionNumber },
});
}
return defineLink({ return defineLink({
to: '/app/chats/$chatId/reports/$reportId', to: '/app/chats/$chatId/reports/$reportId',
params: { reportId: asset.id || '', chatId: asset.chatId }, params: { reportId: asset.id || '', chatId: asset.chatId },
@ -92,6 +121,12 @@ export const createChatAssetRoute = (asset: {
} }
if (asset.asset_type === 'reasoning') { if (asset.asset_type === 'reasoning') {
if (asset.isEmbed) {
return defineLink({
to: '/embed/chats/$chatId/reasoning/$messageId',
params: { messageId: asset.id || '', chatId: asset.chatId },
});
}
return defineLink({ return defineLink({
to: '/app/chats/$chatId/reasoning/$messageId', to: '/app/chats/$chatId/reasoning/$messageId',
params: { chatId: asset.chatId, messageId: asset.id || '' }, params: { chatId: asset.chatId, messageId: asset.id || '' },
@ -99,6 +134,12 @@ export const createChatAssetRoute = (asset: {
} }
if (asset.asset_type === 'collection') { if (asset.asset_type === 'collection') {
if (asset.isEmbed) {
console.warn('collection is actually not supported for embeds...', asset.id);
return defineLink({
to: '/auth/login',
});
}
return defineLink({ return defineLink({
to: '/app/collections/$collectionId', to: '/app/collections/$collectionId',
params: { collectionId: asset.id || '' }, params: { collectionId: asset.id || '' },