attachments-inline.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/attachments-inline.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 {
  9. Attachment,
  10. AttachmentHoverCard,
  11. AttachmentHoverCardContent,
  12. AttachmentHoverCardTrigger,
  13. AttachmentInfo,
  14. AttachmentPreview,
  15. AttachmentRemove,
  16. Attachments,
  17. getAttachmentLabel,
  18. getMediaCategory,
  19. } from "@/components/ai-elements/attachments";
  20. import { nanoid } from "nanoid";
  21. import { memo, useCallback, useState } from "react";
  22. const initialAttachments = [
  23. {
  24. filename: "mountain-landscape.jpg",
  25. id: nanoid(),
  26. mediaType: "image/jpeg",
  27. type: "file" as const,
  28. url: "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop",
  29. },
  30. {
  31. filename: "quarterly-report.pdf",
  32. id: nanoid(),
  33. mediaType: "application/pdf",
  34. type: "file" as const,
  35. url: "",
  36. },
  37. {
  38. id: nanoid(),
  39. mediaType: "text/html",
  40. title: "React Documentation",
  41. type: "source-document" as const,
  42. url: "https://react.dev",
  43. },
  44. {
  45. filename: "podcast-episode.mp3",
  46. id: nanoid(),
  47. mediaType: "audio/mp3",
  48. type: "file" as const,
  49. url: "",
  50. },
  51. ];
  52. interface AttachmentItemProps {
  53. attachment: (typeof initialAttachments)[0];
  54. onRemove: (id: string) => void;
  55. }
  56. const AttachmentItem = memo(({ attachment, onRemove }: AttachmentItemProps) => {
  57. const handleRemove = useCallback(() => onRemove(attachment.id), [onRemove, attachment.id]);
  58. const mediaCategory = getMediaCategory(attachment);
  59. const label = getAttachmentLabel(attachment);
  60. return (
  61. <AttachmentHoverCard key={attachment.id}>
  62. <AttachmentHoverCardTrigger asChild>
  63. <Attachment data={attachment} onRemove={handleRemove}>
  64. <div className="relative size-5 shrink-0">
  65. <div className="absolute inset-0 transition-opacity group-hover:opacity-0">
  66. <AttachmentPreview />
  67. </div>
  68. <AttachmentRemove className="absolute inset-0" />
  69. </div>
  70. <AttachmentInfo />
  71. </Attachment>
  72. </AttachmentHoverCardTrigger>
  73. <AttachmentHoverCardContent>
  74. <div className="space-y-3">
  75. {mediaCategory === "image" && attachment.type === "file" && attachment.url && (
  76. <div className="flex max-h-96 w-80 items-center justify-center overflow-hidden rounded-md border">
  77. <img
  78. alt={label}
  79. className="max-h-full max-w-full object-contain"
  80. height={384}
  81. src={attachment.url}
  82. width={320}
  83. />
  84. </div>
  85. )}
  86. <div className="space-y-1 px-0.5">
  87. <h4 className="font-semibold text-sm leading-none">{label}</h4>
  88. {attachment.mediaType && (
  89. <p className="font-mono text-muted-foreground text-xs">{attachment.mediaType}</p>
  90. )}
  91. </div>
  92. </div>
  93. </AttachmentHoverCardContent>
  94. </AttachmentHoverCard>
  95. );
  96. });
  97. AttachmentItem.displayName = "AttachmentItem";
  98. const Example = () => {
  99. const [attachments, setAttachments] = useState(initialAttachments);
  100. const handleRemove = useCallback((id: string) => {
  101. setAttachments((prev) => prev.filter((a) => a.id !== id));
  102. }, []);
  103. return (
  104. <div className="flex items-center justify-center p-8">
  105. <Attachments variant="inline">
  106. {attachments.map((attachment) => (
  107. <AttachmentItem attachment={attachment} key={attachment.id} onRemove={handleRemove} />
  108. ))}
  109. </Attachments>
  110. </div>
  111. );
  112. };
  113. export default Example;