@assistant-ui/react
Version:
TypeScript/React library for AI Chat
53 lines • 1.75 kB
JavaScript
"use client";
import { createActionButton, } from "../../utils/createActionButton.js";
import { useCallback } from "react";
import { useAssistantState, useAssistantApi } from "../../context/index.js";
/**
* Hook that provides reload functionality for action bar buttons.
*
* This hook returns a callback function that reloads/regenerates the current assistant message,
* or null if reloading is not available (e.g., thread is running, disabled, or message is not from assistant).
*
* @returns A reload callback function, or null if reloading is disabled
*
* @example
* ```tsx
* function CustomReloadButton() {
* const reload = useActionBarReload();
*
* return (
* <button onClick={reload} disabled={!reload}>
* {reload ? "Reload Message" : "Cannot Reload"}
* </button>
* );
* }
* ```
*/
const useActionBarReload = () => {
const api = useAssistantApi();
const disabled = useAssistantState((s) => s.thread.isRunning ||
s.thread.isDisabled ||
s.message.role !== "assistant");
const callback = useCallback(() => {
api.message().reload();
}, [api]);
if (disabled)
return null;
return callback;
};
/**
* A button component that reloads/regenerates the current assistant message.
*
* This component automatically handles reloading the current assistant message
* and is disabled when reloading is not available (e.g., thread is running,
* disabled, or message is not from assistant).
*
* @example
* ```tsx
* <ActionBarPrimitive.Reload>
* Reload Message
* </ActionBarPrimitive.Reload>
* ```
*/
export const ActionBarPrimitiveReload = createActionButton("ActionBarPrimitive.Reload", useActionBarReload);
//# sourceMappingURL=ActionBarReload.js.map