plan.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use client';
  2. import { Button } from '@/components/ui/button';
  3. import {
  4. Card,
  5. CardAction,
  6. CardContent,
  7. CardDescription,
  8. CardFooter,
  9. CardHeader,
  10. CardTitle,
  11. } from '@/components/ui/card';
  12. import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible';
  13. import { cn } from '@/lib/utils';
  14. import { ChevronsUpDownIcon } from 'lucide-react';
  15. import type { ComponentProps } from 'react';
  16. import { createContext, useContext } from 'react';
  17. import { Shimmer } from './shimmer';
  18. type PlanContextValue = {
  19. isStreaming: boolean;
  20. };
  21. const PlanContext = createContext<PlanContextValue | null>(null);
  22. const usePlan = () => {
  23. const context = useContext(PlanContext);
  24. if (!context) {
  25. throw new Error('Plan components must be used within Plan');
  26. }
  27. return context;
  28. };
  29. export type PlanProps = ComponentProps<typeof Collapsible> & {
  30. isStreaming?: boolean;
  31. };
  32. export const Plan = ({ className, isStreaming = false, children, ...props }: PlanProps) => (
  33. <PlanContext.Provider value={{ isStreaming }}>
  34. <Collapsible asChild data-slot="plan" {...props}>
  35. <Card className={cn('shadow-none', className)}>{children}</Card>
  36. </Collapsible>
  37. </PlanContext.Provider>
  38. );
  39. export type PlanHeaderProps = ComponentProps<typeof CardHeader>;
  40. export const PlanHeader = ({ className, ...props }: PlanHeaderProps) => (
  41. <CardHeader
  42. className={cn('flex items-start justify-between', className)}
  43. data-slot="plan-header"
  44. {...props}
  45. />
  46. );
  47. export type PlanTitleProps = Omit<ComponentProps<typeof CardTitle>, 'children'> & {
  48. children: string;
  49. };
  50. export const PlanTitle = ({ children, ...props }: PlanTitleProps) => {
  51. const { isStreaming } = usePlan();
  52. return (
  53. <CardTitle data-slot="plan-title" {...props}>
  54. {isStreaming ? <Shimmer>{children}</Shimmer> : children}
  55. </CardTitle>
  56. );
  57. };
  58. export type PlanDescriptionProps = Omit<ComponentProps<typeof CardDescription>, 'children'> & {
  59. children: string;
  60. };
  61. export const PlanDescription = ({ className, children, ...props }: PlanDescriptionProps) => {
  62. const { isStreaming } = usePlan();
  63. return (
  64. <CardDescription
  65. className={cn('text-balance', className)}
  66. data-slot="plan-description"
  67. {...props}
  68. >
  69. {isStreaming ? <Shimmer>{children}</Shimmer> : children}
  70. </CardDescription>
  71. );
  72. };
  73. export type PlanActionProps = ComponentProps<typeof CardAction>;
  74. export const PlanAction = (props: PlanActionProps) => (
  75. <CardAction data-slot="plan-action" {...props} />
  76. );
  77. export type PlanContentProps = ComponentProps<typeof CardContent>;
  78. export const PlanContent = (props: PlanContentProps) => (
  79. <CollapsibleContent asChild>
  80. <CardContent data-slot="plan-content" {...props} />
  81. </CollapsibleContent>
  82. );
  83. export type PlanFooterProps = ComponentProps<'div'>;
  84. export const PlanFooter = (props: PlanFooterProps) => (
  85. <CardFooter data-slot="plan-footer" {...props} />
  86. );
  87. export type PlanTriggerProps = ComponentProps<typeof CollapsibleTrigger>;
  88. export const PlanTrigger = ({ className, ...props }: PlanTriggerProps) => (
  89. <CollapsibleTrigger asChild>
  90. <Button
  91. className={cn('size-8', className)}
  92. data-slot="plan-trigger"
  93. size="icon"
  94. variant="ghost"
  95. {...props}
  96. >
  97. <ChevronsUpDownIcon className="size-4" />
  98. <span className="sr-only">Toggle plan</span>
  99. </Button>
  100. </CollapsibleTrigger>
  101. );