persona-command.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/persona-command.tsx).
  3. * Copyright 2023 Vercel, Inc. Licensed under Apache-2.0.
  4. * Modified by ZCode: local integration, formatting and adaptations.
  5. * See THIRD-PARTY-NOTICES.md in the repository root for license and provenance.
  6. */
  7. "use client";
  8. import type { PersonaState } from "@/components/ai-elements/persona";
  9. import { Persona } from "@/components/ai-elements/persona";
  10. import { Button } from "@/components/ui/button";
  11. import { ButtonGroup } from "@/components/ui/button-group";
  12. import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
  13. import type { LucideIcon } from "lucide-react";
  14. import { BrainIcon, CircleIcon, EyeClosedIcon, MegaphoneIcon, MicIcon } from "lucide-react";
  15. import { memo, useCallback, useState } from "react";
  16. const states: {
  17. state: PersonaState;
  18. icon: LucideIcon;
  19. label: string;
  20. }[] = [
  21. {
  22. icon: CircleIcon,
  23. label: "Idle",
  24. state: "idle",
  25. },
  26. {
  27. icon: MicIcon,
  28. label: "Listening",
  29. state: "listening",
  30. },
  31. {
  32. icon: BrainIcon,
  33. label: "Thinking",
  34. state: "thinking",
  35. },
  36. {
  37. icon: MegaphoneIcon,
  38. label: "Speaking",
  39. state: "speaking",
  40. },
  41. {
  42. icon: EyeClosedIcon,
  43. label: "Asleep",
  44. state: "asleep",
  45. },
  46. ];
  47. interface StateButtonProps {
  48. state: (typeof states)[0];
  49. currentState: PersonaState;
  50. onStateChange: (state: PersonaState) => void;
  51. }
  52. const StateButton = memo(({ state, currentState, onStateChange }: StateButtonProps) => {
  53. const handleClick = useCallback(() => onStateChange(state.state), [onStateChange, state.state]);
  54. return (
  55. <Tooltip key={state.state}>
  56. <TooltipTrigger asChild>
  57. <Button
  58. onClick={handleClick}
  59. size="icon-sm"
  60. variant={currentState === state.state ? "default" : "outline"}
  61. >
  62. <state.icon className="size-4" />
  63. </Button>
  64. </TooltipTrigger>
  65. <TooltipContent>{state.label}</TooltipContent>
  66. </Tooltip>
  67. );
  68. });
  69. StateButton.displayName = "StateButton";
  70. const Example = () => {
  71. const [currentState, setCurrentState] = useState<PersonaState>("idle");
  72. const handleStateChange = useCallback((state: PersonaState) => {
  73. setCurrentState(state);
  74. }, []);
  75. return (
  76. <div className="flex size-full flex-col items-center justify-center gap-4">
  77. <Persona className="size-32" state={currentState} variant="command" />
  78. <ButtonGroup orientation="horizontal">
  79. {states.map((state) => (
  80. <StateButton
  81. currentState={currentState}
  82. key={state.state}
  83. onStateChange={handleStateChange}
  84. state={state}
  85. />
  86. ))}
  87. </ButtonGroup>
  88. </div>
  89. );
  90. };
  91. export default Example;