-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ThreadList / ThreadListItem UI (#1214)
- Loading branch information
Showing
10 changed files
with
314 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
feat: ThreadList / ThreadListItem UI |
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
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
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
packages/react/src/primitives/threadList/ThreadListNew.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use client"; | ||
|
||
import { | ||
ActionButtonElement, | ||
ActionButtonProps, | ||
} from "../../utils/createActionButton"; | ||
import { useAssistantRuntime, useThreadList } from "../../context"; | ||
import { forwardRef } from "react"; | ||
import { Primitive } from "@radix-ui/react-primitive"; | ||
import { composeEventHandlers } from "@radix-ui/primitive"; | ||
|
||
const useThreadListNew = () => { | ||
const runtime = useAssistantRuntime(); | ||
return () => { | ||
runtime.switchToNewThread(); | ||
}; | ||
}; | ||
|
||
export namespace ThreadListPrimitiveNew { | ||
export type Element = ActionButtonElement; | ||
export type Props = ActionButtonProps<typeof useThreadListNew>; | ||
} | ||
|
||
export const ThreadListPrimitiveNew = forwardRef< | ||
ThreadListPrimitiveNew.Element, | ||
ThreadListPrimitiveNew.Props | ||
>(({ onClick, disabled, ...props }, forwardedRef) => { | ||
const isMain = useThreadList((t) => t.newThread === t.mainThreadId); | ||
const callback = useThreadListNew(); | ||
|
||
return ( | ||
<Primitive.button | ||
type="button" | ||
{...(isMain ? { "data-active": "true" } : null)} | ||
{...props} | ||
ref={forwardedRef} | ||
disabled={disabled || !callback} | ||
onClick={composeEventHandlers(onClick, () => { | ||
callback?.(); | ||
})} | ||
/> | ||
); | ||
}); | ||
|
||
ThreadListPrimitiveNew.displayName = "ThreadListPrimitive.New"; |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
"use client"; | ||
|
||
import { ComponentPropsWithoutRef, forwardRef, type FC } from "react"; | ||
import { ArchiveIcon } from "lucide-react"; | ||
|
||
import { withDefaults } from "./utils/withDefaults"; | ||
import { TooltipIconButton } from "./base/tooltip-icon-button"; | ||
import { ThreadListItemPrimitive } from "../primitives"; | ||
import { useThreadConfig } from "./thread-config"; | ||
import classNames from "classnames"; | ||
|
||
const ThreadListItem: FC = () => { | ||
return ( | ||
<ThreadListItemRoot> | ||
<ThreadListItemTrigger> | ||
<ThreadListItemTitle /> | ||
</ThreadListItemTrigger> | ||
<ThreadListItemArchive /> | ||
</ThreadListItemRoot> | ||
); | ||
}; | ||
|
||
const ThreadListItemRoot = withDefaults(ThreadListItemPrimitive.Root, { | ||
className: "aui-thread-list-item", | ||
}); | ||
|
||
ThreadListItemRoot.displayName = "ThreadListItemRoot"; | ||
|
||
const ThreadListItemTrigger = withDefaults(ThreadListItemPrimitive.Trigger, { | ||
className: "aui-thread-list-item-trigger", | ||
}); | ||
|
||
namespace ThreadListItemPrimitiveTitle { | ||
export type Element = HTMLParagraphElement; | ||
export type Props = ComponentPropsWithoutRef<"p">; | ||
} | ||
|
||
const ThreadListItemTitle = forwardRef< | ||
ThreadListItemPrimitiveTitle.Element, | ||
ThreadListItemPrimitiveTitle.Props | ||
>(({ className, ...props }, ref) => { | ||
const { | ||
strings: { | ||
threadList: { item: { title: { fallback = "New Chat" } = {} } = {} } = {}, | ||
} = {}, | ||
} = useThreadConfig(); | ||
|
||
return ( | ||
<p | ||
ref={ref} | ||
className={classNames("aui-thread-list-item-title", className)} | ||
{...props} | ||
> | ||
<ThreadListItemPrimitive.Title fallback={fallback} /> | ||
</p> | ||
); | ||
}); | ||
|
||
ThreadListItemTitle.displayName = "ThreadListItemTitle"; | ||
|
||
const ThreadListItemArchive = withDefaults( | ||
forwardRef<HTMLButtonElement, TooltipIconButton.Props>( | ||
({ className, ...props }, ref) => { | ||
const { | ||
strings: { | ||
threadList: { | ||
item: { archive: { label = "Archive thread" } = {} } = {}, | ||
} = {}, | ||
} = {}, | ||
} = useThreadConfig(); | ||
|
||
return ( | ||
<ThreadListItemPrimitive.Archive asChild> | ||
<TooltipIconButton | ||
{...props} | ||
ref={ref} | ||
className={classNames("aui-thread-list-item-archive", className)} | ||
variant="ghost" | ||
tooltip={label} | ||
> | ||
<ArchiveIcon /> | ||
</TooltipIconButton> | ||
</ThreadListItemPrimitive.Archive> | ||
); | ||
}, | ||
), | ||
{}, | ||
); | ||
|
||
ThreadListItemArchive.displayName = "ThreadListItemArchive"; | ||
|
||
const exports = { | ||
Root: ThreadListItemRoot, | ||
Title: ThreadListItemTitle, | ||
Archive: ThreadListItemArchive, | ||
}; | ||
|
||
export default Object.assign(ThreadListItem, exports) as typeof ThreadListItem & | ||
typeof exports; |
Oops, something went wrong.