import { forwardRef, useEffect, useRef, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import ComsatsLogo from '../ComsatsLogo'
import { MessageRoleSticker } from './MessageRoleSticker'
function AssistantAvatarIcon({ className = '' }) {
return (
)
}
function SendIcon({ className = '' }) {
return (
)
}
function DotsVerticalIcon({ className = '' }) {
return (
)
}
function CheckMiniIcon({ className = '' }) {
return (
)
}
function SparklesIcon({ className = '' }) {
return (
)
}
function MarkdownText({ content, tone = 'assistant' }) {
const linkClass =
tone === 'user'
? 'underline decoration-white/60 hover:decoration-white'
: 'font-medium text-comsats-blue underline decoration-comsats-blue/25 underline-offset-2 hover:text-comsats-purple'
const codeBg =
tone === 'user' ? 'bg-white/20 text-white' : 'bg-slate-100 text-slate-900 ring-1 ring-slate-200/80'
const codeBlockBg =
tone === 'user' ? 'bg-black/25 text-white' : 'bg-slate-50 text-slate-900 ring-1 ring-slate-200/90'
return (
(
{children}
),
ul: ({ children }) => ,
ol: ({ children }) => {children} ,
li: ({ children }) => {children} ,
a: ({ href, children }) => (
{children}
),
strong: ({ children }) => (
{children}
),
code: ({ inline, children }) =>
inline ? (
{children}
) : (
{children}
),
}}
>
{content ?? ''}
)
}
const EMPTY_THREAD_STARTERS = [
'What programs are offered at CUI Attock?',
'How do I apply for admissions?',
'Tell me about campus facilities and fees.',
]
const ChatMain = forwardRef(function ChatMain(
{
chatId,
chatTitle = 'Conversation',
turns,
loadingChat,
loadingOlder,
hasOlder,
onLoadOlder,
sending,
tailBump,
error,
question,
setQuestion,
onSend,
followUpSuggestions = [],
suggestionsEnabled = true,
onSuggestionsEnabledChange,
webSearchEnabled = false,
onWebSearchEnabledChange,
onPickFollowUp,
sidebarExpanded = true,
collapsedSidebarRail = false,
onOpenSidebar = () => {},
},
scrollRef,
) {
const [sourceMenuOpen, setSourceMenuOpen] = useState(false)
const sourceMenuWrapRef = useRef(null)
useEffect(() => {
if (!sourceMenuOpen) return
function handlePointerDown(e) {
const el = sourceMenuWrapRef.current
if (el && !el.contains(e.target)) setSourceMenuOpen(false)
}
function handleKey(e) {
if (e.key === 'Escape') setSourceMenuOpen(false)
}
document.addEventListener('mousedown', handlePointerDown)
window.addEventListener('keydown', handleKey)
return () => {
document.removeEventListener('mousedown', handlePointerDown)
window.removeEventListener('keydown', handleKey)
}
}, [sourceMenuOpen])
useEffect(() => {
if (!chatId || loadingChat) return
const el = scrollRef?.current
if (!el) return
const scrollToEnd = () => {
el.scrollTo({ top: el.scrollHeight, behavior: 'smooth' })
}
requestAnimationFrame(() => {
requestAnimationFrame(scrollToEnd)
})
}, [chatId, loadingChat, tailBump])
const showHeaderOpenSidebar =
!sidebarExpanded && !collapsedSidebarRail
const headerTools = (
Follow-ups
onSuggestionsEnabledChange?.(!suggestionsEnabled)}
className={`relative h-7 w-11 shrink-0 rounded-full transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-comsats-blue focus-visible:ring-offset-2 ${
suggestionsEnabled ? 'bg-comsats-purple' : 'bg-slate-300'
}`}
>
)
if (!chatId) {
return (
{showHeaderOpenSidebar ? (
onOpenSidebar()}
className="flex shrink-0 rounded-xl bg-white p-0.5 shadow-sm ring-1 ring-slate-200/80 transition-shadow hover:ring-comsats-purple/25"
aria-label="Open sidebar"
>
) : null}
CUI Connect
CUI Connect
Open the sidebar to pick a chat or start fresh — admissions, programs, and campus questions
welcome.
)
}
const showEmptyWelcome = !loadingChat && turns.length === 0 && !sending
return (
{showHeaderOpenSidebar ? (
onOpenSidebar()}
className="flex shrink-0 rounded-xl bg-white p-0.5 shadow-md shadow-slate-900/5 ring-1 ring-slate-200/90 transition-all hover:ring-comsats-purple/30 hover:shadow-comsats-purple/10"
aria-label="Open sidebar"
>
) : null}
{chatTitle}
CUI Connect
·
Knowledge assistant
{headerTools}
{loadingChat ? (
) : (
{hasOlder ? (
{loadingOlder ? 'Loading older…' : 'Load older messages'}
) : null}
{showEmptyWelcome ? (
What would you like to know?
I can help with programs, admissions, fees, and campus life. After you open a chat,
use the ⋯ menu next to the
message box to choose{' '}
RAG (knowledge base) or{' '}
Web search .
Try one of these
{EMPTY_THREAD_STARTERS.map((s) => (
onPickFollowUp?.(s)}
className="group rounded-2xl border border-slate-200/90 bg-white px-5 py-4 text-left text-[15px] font-medium text-slate-800 shadow-[0_2px_8px_rgba(0,0,0,0.04)] ring-1 ring-white transition-all hover:-translate-y-0.5 hover:border-comsats-purple/30 hover:shadow-[0_12px_28px_-12px_rgba(75,46,131,0.25)]"
dir="auto"
>
{s}
))}
) : null}
{turns.map((turn) => {
const key =
turn.clientKey != null ? turn.clientKey : `pair-${turn.pairIndex ?? 'x'}`
return (
Assistant:
{turn.pending ? (
Thinking…
) : (
)}
)
})}
)}
{error ? (
{error}
) : null}
)
})
export default ChatMain