Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

3.4 KB
Raw Blame
import { downloadBytes } from '@aliasvault/client/utilities/FileDownload';
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';

import Icon from '@/entrypoints/popup/components/Icons/Icon';
import { useDb } from '@/entrypoints/popup/context/DbContext';

import { logFailure } from '@/utils/Diagnostics';

import type { Attachment } from '@aliasvault/models/vault';

type AttachmentBlockProps = {
  itemId: string;
  manifestId: string;
}

/**
 * This component shows attachments for an item.
 */
const AttachmentBlock: React.FC<AttachmentBlockProps> = ({ itemId, manifestId }) => {
  const { t } = useTranslation();
  const [attachments, setAttachments] = useState<Attachment[]>([]);
  const [loading, setLoading] = useState(true);
  const dbContext = useDb();

  /**
   * Downloads an attachment file.
   */
  const downloadAttachment = (attachment: Attachment): void => {
    try {
      downloadBytes(attachment.Filename, attachment.Blob ?? []);
    } catch (error) {
      logFailure('Error downloading attachment', error);
    }
  };

  useEffect(() => {
    /**
     * Loads the attachments for the item.
     */
    const loadAttachments = async (): Promise<void> => {
      if (!dbContext?.sqliteClient || !itemId) {
        return;
      }

      try {
        const attachmentList = dbContext.sqliteClient.items.getAttachmentsForItem({ Id: itemId, ManifestId: manifestId });
        setAttachments(attachmentList);
      } catch (error) {
        logFailure('Error loading attachments', error);
      } finally {
        setLoading(false);
      }
    };

    loadAttachments();
  }, [itemId, manifestId, dbContext?.sqliteClient]);

  if (loading) {
    return (
      <div className="text-gray-500 dark:text-gray-400 mb-4">
        <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">{t('common.attachments')}</h2>
        {t('common.loadingAttachments')}
      </div>
    );
  }

  if (attachments.length === 0) {
    return null;
  }

  return (
    <div className="mb-4">
      <div className="space-y-2">
        <h2 className="text-lg font-semibold text-gray-900 dark:text-white">{t('common.attachments')}</h2>
        <div className="grid grid-cols-1 gap-2">
          {attachments.map(attachment => (
            <button
              key={attachment.Id}
              className="w-full text-left p-2 ps-3 pe-3 rounded bg-white dark:bg-gray-800 shadow hover:shadow-md transition-all border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700"
              onClick={() => downloadAttachment(attachment)}
              aria-label={`Download ${attachment.Filename}`}
            >
              <div className="flex justify-between items-center gap-2">
                <div className="flex items-center flex-1">
                  <div className="flex flex-col">
                    <h4 className="text-sm font-medium text-gray-900 dark:text-white">{attachment.Filename}</h4>
                    <span className="text-xs text-gray-500 dark:text-gray-400">
                      {new Date(attachment.CreatedAt).toLocaleDateString()}
                    </span>
                  </div>
                </div>
                <div className="flex items-center">
                  <Icon name="download" className="w-4 h-4 text-gray-500 dark:text-gray-400" />
                </div>
              </div>
            </button>
          ))}
        </div>
      </div>
    </div>
  );
};

export default AttachmentBlock;