attachments.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/attachments.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. AttachmentPreview,
  11. AttachmentRemove,
  12. Attachments,
  13. } from "@/components/ai-elements/attachments";
  14. import { nanoid } from "nanoid";
  15. import { memo, useCallback, useState } from "react";
  16. const initialAttachments = [
  17. {
  18. filename: "mountain-landscape.jpg",
  19. id: nanoid(),
  20. mediaType: "image/jpeg",
  21. type: "file" as const,
  22. url: "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop",
  23. },
  24. {
  25. filename: "ocean-sunset.jpg",
  26. id: nanoid(),
  27. mediaType: "image/jpeg",
  28. type: "file" as const,
  29. url: "https://images.unsplash.com/photo-1682687220742-aba13b6e50ba?w=400&h=400&fit=crop",
  30. },
  31. {
  32. filename: "document.pdf",
  33. id: nanoid(),
  34. mediaType: "application/pdf",
  35. type: "file" as const,
  36. url: "",
  37. },
  38. {
  39. filename: "video.mp4",
  40. id: nanoid(),
  41. mediaType: "video/mp4",
  42. type: "file" as const,
  43. url: "",
  44. },
  45. ];
  46. interface AttachmentItemProps {
  47. attachment: (typeof initialAttachments)[0];
  48. onRemove: (id: string) => void;
  49. }
  50. const AttachmentItem = memo(({ attachment, onRemove }: AttachmentItemProps) => {
  51. const handleRemove = useCallback(() => onRemove(attachment.id), [onRemove, attachment.id]);
  52. return (
  53. <Attachment data={attachment} onRemove={handleRemove}>
  54. <AttachmentPreview />
  55. <AttachmentRemove />
  56. </Attachment>
  57. );
  58. });
  59. AttachmentItem.displayName = "AttachmentItem";
  60. const Example = () => {
  61. const [attachments, setAttachments] = useState(initialAttachments);
  62. const handleRemove = useCallback((id: string) => {
  63. setAttachments((prev) => prev.filter((a) => a.id !== id));
  64. }, []);
  65. return (
  66. <div className="flex items-center justify-center p-8">
  67. <Attachments variant="grid">
  68. {attachments.map((attachment) => (
  69. <AttachmentItem attachment={attachment} key={attachment.id} onRemove={handleRemove} />
  70. ))}
  71. </Attachments>
  72. </div>
  73. );
  74. };
  75. export default Example;