2025-09-21 12:03:29 +01:00
|
|
|
'use client';
|
2024-12-03 17:49:38 +03:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
import { motion } from 'framer-motion';
|
|
|
|
|
import { memo } from 'react';
|
|
|
|
|
import type { UseChatHelpers } from '@ai-sdk/react';
|
|
|
|
|
import type { VisibilityType } from './visibility-selector';
|
|
|
|
|
import type { ChatMessage } from '@/lib/types';
|
|
|
|
|
import { Suggestion } from './elements/suggestion';
|
2024-12-03 17:49:38 +03:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
interface SuggestedActionsProps {
|
2024-12-03 17:49:38 +03:00
|
|
|
chatId: string;
|
2025-09-21 12:03:29 +01:00
|
|
|
sendMessage: UseChatHelpers<ChatMessage>['sendMessage'];
|
2025-05-01 17:47:48 -07:00
|
|
|
selectedVisibilityType: VisibilityType;
|
2025-09-21 12:03:29 +01:00
|
|
|
}
|
2024-12-03 17:49:38 +03:00
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
function PureSuggestedActions({
|
|
|
|
|
chatId,
|
|
|
|
|
sendMessage,
|
|
|
|
|
selectedVisibilityType,
|
|
|
|
|
}: SuggestedActionsProps) {
|
2024-12-03 17:49:38 +03:00
|
|
|
const suggestedActions = [
|
2025-09-21 12:03:29 +01:00
|
|
|
'What are the advantages of using Next.js?',
|
2025-09-09 15:44:07 -04:00
|
|
|
"Write code to demonstrate Dijkstra's algorithm",
|
2025-09-21 12:03:29 +01:00
|
|
|
'Help me write an essay about Silicon Valley',
|
|
|
|
|
'What is the weather in San Francisco?',
|
2024-12-03 17:49:38 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div
|
2025-09-20 12:47:10 -07:00
|
|
|
data-testid="suggested-actions"
|
2025-09-21 12:03:29 +01:00
|
|
|
className="grid w-full gap-2 sm:grid-cols-2"
|
2025-09-09 15:44:07 -04:00
|
|
|
>
|
|
|
|
|
{suggestedActions.map((suggestedAction, index) => (
|
|
|
|
|
<motion.div
|
2025-09-21 12:03:29 +01:00
|
|
|
initial={{ opacity: 0, y: 20 }}
|
2025-09-09 15:44:07 -04:00
|
|
|
animate={{ opacity: 1, y: 0 }}
|
|
|
|
|
exit={{ opacity: 0, y: 20 }}
|
2025-09-20 12:47:10 -07:00
|
|
|
transition={{ delay: 0.05 * index }}
|
2025-09-21 12:03:29 +01:00
|
|
|
key={suggestedAction}
|
2025-09-09 15:44:07 -04:00
|
|
|
>
|
|
|
|
|
<Suggestion
|
2025-09-21 12:03:29 +01:00
|
|
|
suggestion={suggestedAction}
|
2025-09-09 15:44:07 -04:00
|
|
|
onClick={(suggestion) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
window.history.replaceState({}, '', `/chat/${chatId}`);
|
2025-09-09 15:44:07 -04:00
|
|
|
sendMessage({
|
2025-09-21 12:03:29 +01:00
|
|
|
role: 'user',
|
|
|
|
|
parts: [{ type: 'text', text: suggestion }],
|
2025-09-09 15:44:07 -04:00
|
|
|
});
|
|
|
|
|
}}
|
2025-09-21 12:03:29 +01:00
|
|
|
className="h-auto w-full whitespace-normal p-3 text-left"
|
2024-12-03 17:49:38 +03:00
|
|
|
>
|
2025-09-09 15:44:07 -04:00
|
|
|
{suggestedAction}
|
|
|
|
|
</Suggestion>
|
|
|
|
|
</motion.div>
|
|
|
|
|
))}
|
2024-12-03 17:49:38 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 17:47:48 -07:00
|
|
|
export const SuggestedActions = memo(
|
|
|
|
|
PureSuggestedActions,
|
|
|
|
|
(prevProps, nextProps) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
if (prevProps.chatId !== nextProps.chatId) return false;
|
|
|
|
|
if (prevProps.selectedVisibilityType !== nextProps.selectedVisibilityType)
|
2025-05-01 17:47:48 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
2025-09-21 12:03:29 +01:00
|
|
|
},
|
2025-05-01 17:47:48 -07:00
|
|
|
);
|