Skip to content

Commit 34261fe

Browse files
committed
refactor: use-mb-chat to context
1 parent 96e08c4 commit 34261fe

File tree

17 files changed

+476
-376
lines changed

17 files changed

+476
-376
lines changed

apps/masterbots.ai/app/actions/ai-main-call.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export async function processWithAiObject(
179179
throw new Error('Failed to get response object')
180180
}
181181

182-
console.log('result::processwithAiObject -->', responseObject)
182+
console.log('result::processWithAiObject -->', responseObject)
183183
return responseObject
184184
} catch (error) {
185185
console.error('Error in processWithAIObject: ', error)
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import FooterCT from '@/components/layout/footer/footer-ct'
22
import { ResponsiveSidebar } from '@/components/layout/sidebar/sidebar-responsive'
33
import { ChatLayoutSection } from '@/components/routes/chat/chat-layout-section'
4+
import { MBChatProvider } from '@/lib/hooks/use-mb-chat'
45
import NextTopLoader from 'nextjs-toploader'
56

67
interface ChatLayoutProps {
@@ -9,11 +10,13 @@ interface ChatLayoutProps {
910

1011
export default async function ChatLayout({ children }: ChatLayoutProps) {
1112
return (
12-
<main className="relative flex flex-col h-[calc(100vh-4rem)] overflow-hidden">
13-
<NextTopLoader color="#1ED761" initialPosition={0.2} />
14-
<ResponsiveSidebar />
15-
<ChatLayoutSection>{children}</ChatLayoutSection>
16-
<FooterCT />
17-
</main>
13+
<MBChatProvider>
14+
<main className="relative flex flex-col h-[calc(100vh-4rem)] overflow-hidden">
15+
<NextTopLoader color="#1ED761" initialPosition={0.2} />
16+
<ResponsiveSidebar />
17+
<ChatLayoutSection>{children}</ChatLayoutSection>
18+
<FooterCT />
19+
</main>
20+
</MBChatProvider>
1821
)
1922
}

apps/masterbots.ai/components/routes/chat/chat-list.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { ChatMessage } from '@/components/routes/chat/chat-message'
44
import { SharedAccordion } from '@/components/shared/shared-accordion'
55
import { ShortMessage } from '@/components/shared/short-message'
6-
import { useMBChat } from '@/lib/hooks/use-mb-chat'
76
import { useScroll } from '@/lib/hooks/use-scroll'
87
import { useThread } from '@/lib/hooks/use-thread'
98
import { cn, createMessagePairs } from '@/lib/utils'
@@ -47,7 +46,8 @@ export function ChatList({
4746
const { isNewResponse, activeThread } = useThread()
4847
const localContainerRef = useRef<HTMLDivElement>(null)
4948
const effectiveContainerRef = containerRef || localContainerRef
50-
const chatMessages = (messages || activeThread?.messages || []).sort((a, b) => a.createdAt - b.createdAt)
49+
const chatMessages = (messages || activeThread?.messages || [])
50+
// .sort((a, b) => a.createdAt - b.createdAt)
5151

5252
useScroll({
5353
containerRef: effectiveContainerRef,
@@ -118,7 +118,7 @@ function MessagePairs({
118118
<>
119119
{pairs.map((pair: MessagePair, key: number) => (
120120
<SharedAccordion
121-
key={`${pair.userMessage.id}-${pair.chatGptMessage[0]?.id ?? 'pending'}`}
121+
key={`${pair.userMessage.createdAt}-${pair.chatGptMessage[0]?.id ?? 'pending'}`}
122122
defaultState={key === 0 || (key === pairs.length - 1 && isNewResponse)}
123123
className={cn({ 'relative': isThread })}
124124
triggerClass={cn(
@@ -188,10 +188,17 @@ function MessagePairs({
188188
}
189189

190190
export function ChatLoadingState() {
191-
const { activeTool } = useThread()
192-
const [{ isLoadingMessages }] = useMBChat()
191+
const { activeTool, loadingState } = useThread()
193192

194-
if (!isLoadingMessages && !activeTool?.toolName) return null
193+
if (!loadingState || !activeTool?.toolName) return null
194+
195+
if (loadingState?.match(/^(processing|digesting|polishing)/)) {
196+
return (
197+
<div className="flex items-center justify-center w-full h-20">
198+
<div className="w-8 h-8 border-4 border-t-4 border-gray-200 rounded-full animate-ping" />
199+
</div>
200+
)
201+
}
195202

196203
switch (activeTool?.toolName) {
197204
case 'webSearch':
@@ -219,7 +226,9 @@ export function ChatLoadingState() {
219226
)
220227
default:
221228
return (
222-
<div className="transition-all size-6 border-2 border-t-[2px] rounded-full border-x-gray-300 animate-spin" />
229+
<div className="flex items-center justify-center w-full h-20">
230+
<div className="w-8 h-8 border-4 border-t-4 border-gray-200 rounded-full animate-ping" />
231+
</div>
223232
)
224233
}
225234
}

apps/masterbots.ai/components/routes/chat/chat-message.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export function ChatMessage({
2727
<div className="pt-4 mt-4 border-t border-gray-200">
2828
<h3 className="mb-2 text-lg font-semibold">References</h3>
2929
<div className="space-y-4">
30-
{references.map((ref, index) => (
31-
<div key={index} className="flex gap-4">
30+
{references.map((ref) => (
31+
<div key={ref.profile.name.toLowerCase().replace(/\s/g, '-')} className="flex gap-4">
3232
{ref.thumbnail?.src && (
3333
// eslint-disable-next-line @next/next/no-img-element
3434
<img

apps/masterbots.ai/components/routes/chat/chat-options.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,11 @@ export function ChatOptions({ threadId, thread, isBrowse }: ChatOptionsProps) {
109109
)}
110110

111111
<DropdownMenu>
112-
<DropdownMenuTrigger asChild>
113-
<Button
114-
variant="ghost"
115-
size="sm"
116-
className="w-6 h-6 p-0 sm:h-8 sm:w-8"
117-
>
118-
<MoreVertical className="w-3.5 h-3.5 sm:w-4 sm:h-4" />
119-
</Button>
112+
<DropdownMenuTrigger asChild className={cn(buttonVariants({
113+
variant: 'ghost',
114+
size: 'sm'
115+
}), 'size-6 p-0 sm:size-8')}>
116+
<MoreVertical className="w-3.5 h-3.5 sm:w-4 sm:h-4" />
120117
</DropdownMenuTrigger>
121118
<DropdownMenuContent
122119
sideOffset={8}

apps/masterbots.ai/components/routes/chat/chat-panel-header.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { IconRefresh, IconShare, IconStop } from '@/components/ui/icons'
88
import { Label } from '@/components/ui/label'
99
import { Separator } from '@/components/ui/separator'
1010
import { Switch } from '@/components/ui/switch'
11-
import { useMBChat } from '@/lib/hooks/use-mb-chat'
1211
import { usePowerUp } from '@/lib/hooks/use-power-up'
1312
import { useThread } from '@/lib/hooks/use-thread'
1413
import { GlobeIcon } from 'lucide-react'
@@ -42,8 +41,7 @@ export function ChatPanelHeader({
4241
// TODO: Attach Share func to user chats chat pop-up
4342
const [shareDialogOpen, setShareDialogOpen] = React.useState(false)
4443
const webSearchRef = React.useRef(null)
45-
const { loadingState } = useThread()
46-
const [{ webSearch }, { toggleWebSearch }] = useMBChat()
44+
const { loadingState, webSearch, setWebSearch } = useThread()
4745
const { isPowerUp, togglePowerUp } = usePowerUp()
4846

4947
return (
@@ -73,7 +71,7 @@ export function ChatPanelHeader({
7371
id="webSearch"
7472
value={webSearch ? 'checked' : 'unchecked'}
7573
ref={webSearchRef}
76-
onClick={toggleWebSearch}
74+
onClick={value => setWebSearch(!value)}
7775
className="transition-all delay-100 h-auto w-auto inline-flex items-center gap-2 border-muted p-0.5 data-[state=checked]:border-accent/50 data-[state=checked]:bg-accent/25 rounded-full"
7876
checkboxconfig={{
7977
check: (

apps/masterbots.ai/components/routes/chat/chat.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import { ChatList } from '@/components/routes/chat/chat-list'
3636
import { ChatPanel } from '@/components/routes/chat/chat-panel'
3737
import { ChatScrollAnchor } from '@/components/routes/chat/chat-scroll-anchor'
38-
import { botNames } from '@/lib/constants/bots-names'
3938
import { useAtBottom } from '@/lib/hooks/use-at-bottom'
4039
import { useMBChat } from '@/lib/hooks/use-mb-chat'
4140
import { useSidebar } from '@/lib/hooks/use-sidebar'
@@ -48,7 +47,6 @@ import { useScroll } from 'framer-motion'
4847
import type { Chatbot } from 'mb-genql'
4948
import { useParams, usePathname } from 'next/navigation'
5049
import React, { useEffect } from 'react'
51-
import { useAsync } from 'react-use'
5250

5351
export function Chat({
5452
chatbot: chatbotProps,
@@ -76,10 +74,7 @@ export function Chat({
7674
const [
7775
{ newChatThreadId: threadId, input, isLoading, allMessages, isNewChat },
7876
{ appendWithMbContextPrompts, appendAsContinuousThread, reload, setInput },
79-
] = useMBChat({
80-
chatbot,
81-
})
82-
const chatbotNames = useAsync(async () => (await botNames).get(params.chatbot), [])
77+
] = useMBChat()
8378
const pathname = usePathname()
8479
const prevPathname = React.useRef(pathname)
8580

@@ -92,7 +87,7 @@ export function Chat({
9287
scrollY,
9388
})
9489

95-
// ? saffer way to debounce scroll to bottom
90+
// ? safer way to debounce scroll to bottom
9691
let timeoutId: any
9792
const debounceScrollToBottom = (element: HTMLElement | undefined) => {
9893
clearTimeout(timeoutId)

apps/masterbots.ai/components/routes/thread/thread-list.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export default function ThreadList({
5959
return (
6060
<>
6161
{loading
62-
? Array.from({ length: 5 }).map((_, key) => (
63-
<ThreadItemSkeleton key={`thread-skeleton-${key}`} />
62+
? [1, 2, 3, 4, 5].map((pos) => (
63+
<ThreadItemSkeleton key={`thread-skeleton-${pos}`} />
6464
))
6565
: filteredThreads?.map((thread, key) => (
6666
<ThreadComponent

apps/masterbots.ai/components/routes/thread/thread-popup.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ import { useEffect, useRef, useState } from 'react'
2020
export function ThreadPopup({ className }: { className?: string }) {
2121
const { activeChatbot } = useSidebar()
2222
const { isOpenPopup, activeThread } = useThread()
23-
const [{ allMessages, isLoading }, { sendMessageFromResponse }] = useMBChat({
24-
threadId: activeThread?.threadId,
25-
chatbot: activeThread?.chatbot || (activeChatbot as Chatbot)
26-
})
23+
const [{ allMessages, isLoading }, { sendMessageFromResponse }] = useMBChat()
2724
const [browseMessages, setBrowseMessages] = useState<Message[]>([])
2825
const popupContentRef = useRef<HTMLDivElement>()
2926

apps/masterbots.ai/lib/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { PromptProps } from '@/types/types'
22
import type { Message } from 'ai'
3+
import { nanoid } from 'nanoid'
34

45
export function formatSystemPrompts(prompts: PromptProps[]): Message[] {
56
return (
67
prompts?.map(({ prompt }) => ({
7-
id: prompt.promptId.toString(),
8+
id: 'expertise-' + nanoid(10),
89
role: 'system',
910
// role: 'user',
1011
content: prompt.content,

0 commit comments

Comments
 (0)