-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension] Use dropdown menu for conversation switching (#8913)
* Use dropdown menu for conversation switching * show selected conversation * add chevron
- Loading branch information
Showing
6 changed files
with
213 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 122 additions & 10 deletions
132
extension/app/src/components/conversation/ConversationsListButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,131 @@ | ||
import { Button, ChatBubbleLeftRightIcon } from "@dust-tt/sparkle"; | ||
import { useNavigate } from "react-router-dom"; | ||
import type { ConversationWithoutContentPublicType } from "@dust-tt/client"; | ||
import { | ||
Button, | ||
ChatBubbleLeftRightIcon, | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuLabel, | ||
DropdownMenuTrigger, | ||
ScrollArea, | ||
Spinner, | ||
} from "@dust-tt/sparkle"; | ||
import { useConversations } from "@extension/components/conversation/useConversations"; | ||
import moment from "moment"; | ||
import { useNavigate, useParams } from "react-router-dom"; | ||
|
||
type GroupLabel = | ||
| "Today" | ||
| "Yesterday" | ||
| "Last Week" | ||
| "Last Month" | ||
| "Last 12 Months" | ||
| "Older"; | ||
|
||
const Content = () => { | ||
const { conversations, isConversationsLoading } = useConversations(); | ||
const { conversationId } = useParams(); | ||
|
||
const navigate = useNavigate(); | ||
|
||
const groupConversationsByDate = ( | ||
conversations: ConversationWithoutContentPublicType[] | ||
) => { | ||
const today = moment().startOf("day"); | ||
const yesterday = moment().subtract(1, "days").startOf("day"); | ||
const lastWeek = moment().subtract(1, "weeks").startOf("day"); | ||
const lastMonth = moment().subtract(1, "months").startOf("day"); | ||
const lastYear = moment().subtract(1, "years").startOf("day"); | ||
|
||
type GroupLabel = | ||
| "Today" | ||
| "Yesterday" | ||
| "Last Week" | ||
| "Last Month" | ||
| "Last 12 Months" | ||
| "Older"; | ||
|
||
const groups: Record<GroupLabel, ConversationWithoutContentPublicType[]> = { | ||
Today: [], | ||
Yesterday: [], | ||
"Last Week": [], | ||
"Last Month": [], | ||
"Last 12 Months": [], | ||
Older: [], | ||
}; | ||
|
||
conversations.forEach((conversation) => { | ||
const createdDate = moment(conversation.created); | ||
if (createdDate.isSameOrAfter(today)) { | ||
groups["Today"].push(conversation); | ||
} else if (createdDate.isSameOrAfter(yesterday)) { | ||
groups["Yesterday"].push(conversation); | ||
} else if (createdDate.isSameOrAfter(lastWeek)) { | ||
groups["Last Week"].push(conversation); | ||
} else if (createdDate.isSameOrAfter(lastMonth)) { | ||
groups["Last Month"].push(conversation); | ||
} else if (createdDate.isSameOrAfter(lastYear)) { | ||
groups["Last 12 Months"].push(conversation); | ||
} else { | ||
groups["Older"].push(conversation); | ||
} | ||
}); | ||
|
||
return groups; | ||
}; | ||
|
||
const conversationsByDate = conversations.length | ||
? groupConversationsByDate(conversations) | ||
: ({} as Record<GroupLabel, ConversationWithoutContentPublicType[]>); | ||
|
||
return isConversationsLoading ? ( | ||
<div className="flex items-center justify-center m-4"> | ||
<Spinner variant="dark" size="xs" /> | ||
</div> | ||
) : ( | ||
<ScrollArea className="h-[80vh]"> | ||
{Object.keys(conversationsByDate).map((dateLabel) => ( | ||
<> | ||
<DropdownMenuLabel label={dateLabel} /> | ||
{conversationsByDate[dateLabel as GroupLabel].map((conversation) => ( | ||
<DropdownMenuItem | ||
key={conversation.sId} | ||
label={conversation.title || "Untitled Conversation"} | ||
className={ | ||
conversationId === conversation.sId | ||
? "bg-primary-50" | ||
: undefined | ||
} | ||
onClick={() => { | ||
navigate(`/conversations/${conversation.sId}`); | ||
}} | ||
/> | ||
))} | ||
</> | ||
))} | ||
</ScrollArea> | ||
); | ||
}; | ||
|
||
export const ConversationsListButton = ({ | ||
size = "sm", | ||
}: { | ||
size?: "sm" | "md"; | ||
}) => { | ||
const navigate = useNavigate(); | ||
return ( | ||
<Button | ||
tooltip="View conversations" | ||
icon={ChatBubbleLeftRightIcon} | ||
variant="ghost" | ||
onClick={() => navigate("/conversations")} | ||
size={size} | ||
/> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button | ||
tooltip="View conversations" | ||
icon={ChatBubbleLeftRightIcon} | ||
isSelect | ||
variant="ghost" | ||
size={size} | ||
/> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent className="w-[80vw]"> | ||
<Content /> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.