Skip to content

Commit

Permalink
feat(masterbots.ai): create social network navigation alike on browse…
Browse files Browse the repository at this point in the history
… threads on develop (#172)

* devops: force deploy

* devops: trigger automated build

* devops: trigger automated build

* devops: trigger automated build

* devops: trigger automated build

* devops: trigger automated build

* devops: trigger automated build

* devops: trigger automated build

* impr(masterbots.ai): terms route + metadata impr (#158)

* feat: update url when opening the thread on browse view

* fix: rm shallow (this is not supported with app router)

* fix: make browse view to the default page

* chore: use toSlug from lib

---------

Co-authored-by: Gabo Esquivel <[email protected]>
Co-authored-by: Roberto Lucas <[email protected]>
  • Loading branch information
3 people authored and sheriffjimoh committed Apr 17, 2024
1 parent 43686ab commit da153e3
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 15 deletions.
11 changes: 11 additions & 0 deletions apps/masterbots.ai/app/(browse)/[category]/[threadId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getThread } from '@/services/hasura'
import { BrowseThread } from '@/components/browse-thread'
import { ChatPageProps } from '@/app/chat/[chatbot]/[threadId]/page'

export default async function ChatPage({ params }: ChatPageProps) {
const thread = await getThread({
threadId: params.threadId,
jwt: ''
})
return <BrowseThread thread={thread} />
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getCategories } from '@/services/hasura'

export const revalidate = 3600 // revalidate the data at most every hour

export default async function BrowsePage() {
export default async function HomePage() {
const categories = await getCategories()

return (
Expand Down
2 changes: 1 addition & 1 deletion apps/masterbots.ai/app/b/[id]/[threadId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getThread } from '@/services/hasura'
import { BrowseThread } from '@/components/browse-thread'
import { ChatPageProps } from '@/app/(chat)/[chatbot]/[threadId]/page'
import { ChatPageProps } from '@/app/chat/[chatbot]/[threadId]/page'
import PageProps from 'next/types'

export default async function ChatPage({ params }: ChatPageProps) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default async function IndexPage() {
const session = await auth()

// NOTE: maybe we should use same expiration time
const jwt = session!.user?.hasuraJwt
const jwt = session?.user?.hasuraJwt
if (!jwt || isTokenExpired(jwt)) {
redirect(`/sign-in`)
}
Expand Down
5 changes: 3 additions & 2 deletions apps/masterbots.ai/components/browse-category-button.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toSlug } from '@/lib/utils'
import { motion } from 'framer-motion'
import { Category } from 'mb-genql'
import Link from 'next/link'
Expand All @@ -17,8 +18,8 @@ export function BrowseCategoryButton({
<Link
href={
category === 'all'
? '/browse'
: `/browse/${category.name.toLowerCase().replace(/\s+/g, '_').replace(/\&/g, 'n')}`
? '/'
: `/${toSlug(category.name)}`
}
shallow
id={id}
Expand Down
3 changes: 2 additions & 1 deletion apps/masterbots.ai/components/browse-category-tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useBrowse } from '@/lib/hooks/use-browse'
import { Category } from 'mb-genql'
import { BrowseCategoryButton } from './browse-category-button'
import { useEffect } from 'react'
import { toSlug } from '@/lib/utils'

export function BrowseCategoryTabs({
categories,
Expand Down Expand Up @@ -36,7 +37,7 @@ export function BrowseCategoryTabs({
setActiveTab(
categories.filter(
c =>
c.name.toLowerCase().replace(/\s+/g, '_').replace(/\&/g, 'n') ===
toSlug(c.name) ===
initialCategory
)[0]?.categoryId
)
Expand Down
22 changes: 21 additions & 1 deletion apps/masterbots.ai/components/browse-list-item.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Image from 'next/image'

import { cn, sleep } from '@/lib/utils'
import { cn, sleep, toSlug } from '@/lib/utils'
import { getMessages } from '@/services/hasura'
import { Message, Thread } from 'mb-genql'
import Link from 'next/link'
Expand All @@ -10,6 +10,8 @@ import { BrowseChatMessageList } from './browse-chat-message-list'
import { ChatAccordion } from './chat-accordion'
import { ShortMessage } from './short-message'
import { IconOpenAI, IconUser } from './ui/icons'
import { useBrowse } from '@/lib/hooks/use-browse'
let initialUrl: string | null = null

export default function BrowseListItem({
thread,
Expand All @@ -32,6 +34,17 @@ export default function BrowseListItem({
// ! Move to custom hook and add it to the context useThread + useProvider @bran18
const [isAccordionOpen, setIsAccordionOpen] = React.useState(false)

const { tab } = useBrowse()

React.useEffect(() => {
if (initialUrl) return
initialUrl = location.href
})

React.useEffect(() => {
initialUrl = location.href
}, [tab])

React.useEffect(() => {
if (!threadRef.current) return
const observer = new IntersectionObserver(([entry]) => {
Expand Down Expand Up @@ -59,8 +72,15 @@ export default function BrowseListItem({

const handleAccordionToggle = async (isOpen: boolean) => {
if (isOpen) {
window.history.pushState(
{},
'',
`/${toSlug(thread.chatbot.categories[0].category.name)}/${thread.threadId}`
)
setMessages(_prev => [])
await fetchMessages()
} else {
window.history.pushState({}, '', initialUrl)
}
// When toggling accordion, it should scroll
// Use optional chaining to ensure scrollIntoView is called only if current is not null
Expand Down
4 changes: 2 additions & 2 deletions apps/masterbots.ai/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export async function Header() {
<SidebarToggle />
<HeaderLink href="/" text="Masterbots" />
<IconSeparator className="size-6 text-muted-foreground/50" />
<HeaderLink href="/" text="Chat" />
<HeaderLink href="/browse" text="Browse" />
<HeaderLink href="/chat" text="Chat" />
<HeaderLink href="/" text="Browse" />
</div>
<div className="flex items-center justify-end space-x-2">
<React.Suspense fallback={<div className="flex-1 overflow-auto" />}>
Expand Down
2 changes: 1 addition & 1 deletion apps/masterbots.ai/components/login-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function LoginButton({
onClick={() => {
setIsLoading(true)
// next-auth signIn() function doesn't work yet at Edge Runtime due to usage of BroadcastChannel
signIn('google', { callbackUrl: '/browse' })
signIn('google', { callbackUrl: '/' })
}}
disabled={isLoading}
className={cn(className)}
Expand Down
2 changes: 1 addition & 1 deletion apps/masterbots.ai/components/new-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function NewChat({
})

router.push(`/${chatbot.name.trim().toLowerCase()}/${threadId}`, {
shallow: true,
// shallow: true,
scroll: false
})
router.refresh()
Expand Down
6 changes: 3 additions & 3 deletions apps/masterbots.ai/components/sidebar-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default function SidebarLink({ category }: { category: Category }) {
<Link
// TODO: Improve routing for user sharing
// href={`/${category.name.toLowerCase()}`}
href="/"
href="/chat"
className={cn(
'flex items-center pr-5 py-3 cursor-pointer relative origin-left transition-all ease-in-out duration-300',
isChatbotOfThisCategory ? 'text-xs opacity-50' : 'grow pl-5'
Expand Down Expand Up @@ -168,7 +168,7 @@ export default function SidebarLink({ category }: { category: Category }) {
{
<motion.div
className="overflow-hidden
ml-5 flex-col border-l-[1px] dark:border-mirage border-gray-300"
ml-5 flex-col border-l-DEFAULT dark:border-mirage border-gray-300"
initial={{ height: 0 }}
animate={{
height:
Expand Down Expand Up @@ -242,7 +242,7 @@ function ChatbotComponent({
return (
<Link
ref={chatbotRef}
href={`/${chatbot.name.toLowerCase()}`}
href={`/chat/${chatbot.name.toLowerCase()}`}
className={cn(
'flex items-center px-[20px] py-[12px] dark:hover:bg-mirage hover:bg-gray-300',
chatbot.chatbotId === activeChatbot?.chatbotId &&
Expand Down
9 changes: 9 additions & 0 deletions apps/masterbots.ai/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,12 @@ export const scrollToBottomOfElement = (element?: HTMLElement) => {
export async function sleep(time: number) {
return new Promise(resolve => setTimeout(resolve, time))
}


// Function to convert a name into a slug
export const toSlug = (name: string): string => {
return name
.toLowerCase()
.replace(/&/g, 'n')
.replace(/\s+/g, '_')
}
2 changes: 1 addition & 1 deletion apps/masterbots.ai/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export { auth as middleware } from './auth'

export const config = {
matcher: [
'/((?!api|browse|b|u|terms|images|_next/static|_next/image|favicon.ico).*)'
'/((?!|^\/$|api|b|u|terms|images|_next/static|_next/image|favicon.ico).*)'
]
}

0 comments on commit da153e3

Please sign in to comment.