persona-opal.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/persona-opal.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. interface StateButtonProps {
  17. state: { state: PersonaState; icon: LucideIcon; label: string };
  18. currentState: PersonaState;
  19. onStateChange: (state: PersonaState) => void;
  20. }
  21. const StateButton = memo(({ state, currentState, onStateChange }: StateButtonProps) => {
  22. const handleClick = useCallback(() => onStateChange(state.state), [onStateChange, state.state]);
  23. return (
  24. <Tooltip key={state.state}>
  25. <TooltipTrigger asChild>
  26. <Button
  27. onClick={handleClick}
  28. size="icon-sm"
  29. variant={currentState === state.state ? "default" : "outline"}
  30. >
  31. <state.icon className="size-4" />
  32. </Button>
  33. </TooltipTrigger>
  34. <TooltipContent>{state.label}</TooltipContent>
  35. </Tooltip>
  36. );
  37. });
  38. StateButton.displayName = "StateButton";
  39. const states: {
  40. state: PersonaState;
  41. icon: LucideIcon;
  42. label: string;
  43. }[] = [
  44. {
  45. icon: CircleIcon,
  46. label: "Idle",
  47. state: "idle",
  48. },
  49. {
  50. icon: MicIcon,
  51. label: "Listening",
  52. state: "listening",
  53. },
  54. {
  55. icon: BrainIcon,
  56. label: "Thinking",
  57. state: "thinking",
  58. },
  59. {
  60. icon: MegaphoneIcon,
  61. label: "Speaking",
  62. state: "speaking",
  63. },
  64. {
  65. icon: EyeClosedIcon,
  66. label: "Asleep",
  67. state: "asleep",
  68. },
  69. ];
  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="opal" />
  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;