NextJs open and close side drawer with app router

I have created a NextJs app router to achieve the routes as follows:

  • /chat/conversation_id -> /chat/new or /chat = Open in full main layout
  • /chat/conversation_id/history -> /chat/new/history or /chat/id/history = Open in the – side drawer along with chat
  • /chat/conversation_id/files -> /chat/new/files or /chat/id/files = Open in the side drawer along with chat
  • /chat/conversation_id/<etc> = Open such routes in the side drawer along with chat
  • /settings = Open in full main layout

In the app router added the chat section as a slot and history and files as routable through the children in layout. With this when navigated to /chat/conversation_id/history the side drawer is opening. On soft navigating back to /chat/conversation_id the side drawer still shows the content of history. but on the hard navigation history drawer closes.

The layout file code: chat/[conversationId]/layout.tsx

export default function ChatLayout({
  children,
  chatbot
}: Readonly<{
  children: React.ReactNode
  chatbot: React.ReactNode
}>) {
  // console.log("children", children)
  return (
    <div className="flex h-full flex-row">
      {children}
      <div className="flex-1">{chatbot}</div>
    </div>
  )
}

Even have tried to hide children as chat/[conversationId]/default.tsx return null

Questions:

  1. Can something like this be achievable in an app router, If achievable please share samples or do I have to do a side drawer in the chat page and toggle the side drawer with query params?
  2. Can new chat be routed to /chat instead of /chat/new. This I was doing because I can`t have empty before the history path /chat/<empty>/history?
  3. At the start of the chat I am trying to replace the path with /chat/id but if did so the page redirects or renders. Can we do a soft change of the URL without redirecting or re-rendering?

I was able to get this working.

Answers:

  1. I have updated the chat/[conversationId]/layout.tsx to check the pathname if history or files are not in the path name hiding the children.
    Code:
'use client'

import { usePathname } from 'next/navigation'

export default function ChatLayout({
  children,
  chatbot
}: Readonly<{
  children: React.ReactNode
  chatbot: React.ReactNode
}>) {
  const pathname = usePathname()

  const showDrawer = pathname.includes('history') || pathname.includes('files')
  return (
    <div className="flex h-full flex-row">
      {showDrawer && <div>{children}</div>}
      <div className="flex-1">{chatbot}</div>
    </div>
  )
}
  1. This can be done by having the same routes under /chat as well as /chat/[conversationId]. But I am skipping this, I do conditional rendering with /chat/new and /chat/id
  2. I am using browser native API window.history.pushState to do shallow routing. NextJS doc reference

you can use (history) in the directory because then the app will not understand your router
=>

--- chat
--------[convesationId]
------------ ...
------------ (history)
----------------page.tsx

….
enter link description here

New contributor

Beer Baby is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật