attachments-list.tsx 2.5 KB

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