buster/apps/web/src/components/features/popups/FollowUpWithAsset.tsx

87 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-07-24 06:18:16 +08:00
import React from 'react';
import { Popover, type PopoverProps } from '../../ui/popover';
import { InputCard } from '../../ui/card/InputCard';
import type { ShareAssetType } from '@buster/server-shared/share';
import { useMemoizedFn } from '../../../hooks';
import { useStartChatFromAsset } from '../../../api/buster_rest/chats';
2025-07-24 06:56:17 +08:00
import { AppTooltip } from '../../ui/tooltip';
import { useAppLayoutContextSelector } from '@/context/BusterAppLayout';
import { assetParamsToRoute } from '../../../lib/assets';
2025-07-24 06:18:16 +08:00
type FollowUpWithAssetProps = {
2025-07-24 06:29:00 +08:00
assetType: Exclude<ShareAssetType, 'chat' | 'collection'>;
2025-07-24 06:18:16 +08:00
assetId: string;
children: React.ReactNode;
side?: PopoverProps['side'];
align?: PopoverProps['align'];
placeholder?: string;
buttonText?: string;
};
2025-07-24 12:22:18 +08:00
export const FollowUpWithAssetContent: React.FC<{
assetType: Exclude<ShareAssetType, 'chat' | 'collection'>;
assetId: string;
placeholder?: string;
buttonText?: string;
}> = React.memo(
2025-07-24 06:18:16 +08:00
({
assetType,
assetId,
placeholder = 'Describe the filter you want to apply',
buttonText = 'Apply custom filter'
}) => {
const { mutateAsync: startChatFromAsset, isPending } = useStartChatFromAsset();
2025-07-24 06:56:17 +08:00
const onChangePage = useAppLayoutContextSelector((x) => x.onChangePage);
2025-07-24 06:35:14 +08:00
const onSubmit = useMemoizedFn(async (prompt: string) => {
if (!prompt || !assetId || !assetType || isPending) return;
2025-07-24 06:56:17 +08:00
const res = await startChatFromAsset({
2025-07-24 06:29:00 +08:00
asset_id: assetId,
asset_type: assetType,
2025-07-24 06:35:14 +08:00
prompt
2025-07-24 06:29:00 +08:00
});
2025-07-24 06:56:17 +08:00
const link = assetParamsToRoute({
assetId,
type: assetType,
chatId: res.id
});
onChangePage(link);
2025-07-24 06:18:16 +08:00
});
2025-07-24 12:22:18 +08:00
return (
<InputCard
placeholder={placeholder}
buttonText={buttonText}
onSubmit={onSubmit}
loading={isPending}
className="border-none"
/>
);
}
);
FollowUpWithAssetContent.displayName = 'FollowUpWithAssetContent';
export const FollowUpWithAssetPopup: React.FC<FollowUpWithAssetProps> = React.memo(
({ assetType, assetId, side = 'bottom', align = 'end', children, placeholder, buttonText }) => {
2025-07-24 06:18:16 +08:00
return (
<Popover
side={side}
align={align}
className="p-0"
content={
2025-07-24 12:22:18 +08:00
<FollowUpWithAssetContent
assetType={assetType}
assetId={assetId}
2025-07-24 06:18:16 +08:00
placeholder={placeholder}
buttonText={buttonText}
/>
}>
2025-07-24 06:56:17 +08:00
<AppTooltip title="Apply custom filter">{children}</AppTooltip>
2025-07-24 06:18:16 +08:00
</Popover>
);
}
);
FollowUpWithAssetPopup.displayName = 'FollowUpWithAssetPopup';