Skip to content

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

10.6 KB
Raw Blame
import { scopedKey } from '@aliasvault/client/database/ItemRef';
import { buildFolderTree, flattenFolderTree, getFolderIdPath, isSharedFolder } from '@aliasvault/client/items/FolderUtils';
import React, { useState, useCallback, useMemo, useEffect } from 'react';
import { useTranslation } from 'react-i18next';

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

import type { Folder, FolderRef } from '@aliasvault/client/database/repositories/FolderRepository';

/** Indent per folder level. */
const FOLDER_INDENT_PX = 20;

type ItemNameInputProps = {
  inputRef?: React.RefObject<HTMLInputElement | null>;
  value: string;
  onChange: (value: string) => void;
  folders: Folder[];
  selectedFolder: FolderRef | null | undefined;
  onFolderChange: (folder: FolderRef | null) => void;
  // Optional control rendered directly left of the input (the item logo).
  logoSlot?: React.ReactNode;
  suggestions?: string[];
};

/**
 * Item name input field with an integrated folder selection button and modal.
 * The folder button appears inside the input when folders are available.
 */
const ItemNameInput: React.FC<ItemNameInputProps> = ({
  inputRef,
  value,
  onChange,
  folders,
  selectedFolder: selectedFolderRef,
  onFolderChange,
  logoSlot,
  suggestions = []
}) => {
  const { t } = useTranslation();
  const dbContext = useDb();
  const [showFolderModal, setShowFolderModal] = useState(false);
  const [expandedFolders, setExpandedFolders] = useState<Set<string>>(new Set());

  const selectedFolderId = selectedFolderRef?.Id ?? null;
  const selectedManifestId = selectedFolderRef?.ManifestId ?? null;
  const selectedFolder = folders.find(f => f.Id === selectedFolderId && f.ManifestId === selectedManifestId);
  const personalManifestId = dbContext?.sqliteClient?.getPersonalManifestId() ?? null;

  const folderTree = useMemo(() => buildFolderTree(folders), [folders]);
  const visibleFolders = useMemo(() => flattenFolderTree(folderTree, expandedFolders), [folderTree, expandedFolders]);

  /**
   * Automatically expand parent folders when a folder is pre-selected.
   * This ensures the selected folder is visible in the tree when the modal opens.
   */
  useEffect(() => {
    if (selectedFolderId && selectedManifestId && folders.length > 0) {
      const fullPath = getFolderIdPath({ Id: selectedFolderId, ManifestId: selectedManifestId }, folders);
      if (fullPath.length > 0) {
        // Expand all folders in the path including the selected folder if it has children.
        setExpandedFolders(new Set(fullPath.map(id => scopedKey(selectedManifestId, id))));
      }
    } else {
      setExpandedFolders(new Set());
    }
  }, [selectedFolderId, selectedManifestId, folders]);

  /**
   * Toggle folder expansion in tree view.
   */
  const toggleFolder = useCallback((folderKey: string): void => {
    setExpandedFolders(prev => {
      const next = new Set(prev);
      if (next.has(folderKey)) {
        next.delete(folderKey);
      } else {
        next.add(folderKey);
      }
      return next;
    });
  }, []);

  /**
   * Handle folder selection and close the modal.
   */
  const handleSelectFolder = useCallback((folder: FolderRef | null): void => {
    onFolderChange(folder);
    setShowFolderModal(false);
  }, [onFolderChange]);

  /**
   * Handle name input change.
   */
  const handleNameChange = useCallback((e: React.ChangeEvent<HTMLInputElement>): void => {
    onChange(e.target.value);
  }, [onChange]);

  /**
   * Handle opening folder modal.
   */
  const handleOpenFolderModal = useCallback((): void => {
    setShowFolderModal(true);
  }, []);

  /**
   * Handle closing folder modal.
   */
  const handleCloseFolderModal = useCallback((): void => {
    setShowFolderModal(false);
  }, []);

  return (
    <>
      <div>
        {/* mb-1.5 rather than mb-1: the suggestion pills fill the whole line box, so they sit closer to the input than bare label text would */}
        <div className="flex items-center gap-2 mb-1.5">
          <label htmlFor="itemName" className="shrink-0 block text-sm font-medium text-gray-700 dark:text-gray-300">
            {t('items.itemName')} <span className="text-red-500">*</span>
          </label>
          {/* Name suggestions, inline with the label so they don't add an extra row */}
          {suggestions.length > 0 && (
            <div className="flex-1 min-w-0 flex items-center gap-1 overflow-x-auto no-scrollbar" aria-label={t('items.suggestions')}>
              {suggestions.map(name => (
                <button
                  key={name}
                  type="button"
                  onClick={() => onChange(name)}
                  title={`${t('items.suggestions')}: ${name}`}
                  className="shrink-0 max-w-[7rem] truncate px-2 py-0.5 text-xs rounded-full bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-300 hover:bg-primary-100 dark:hover:bg-primary-900/40 hover:text-primary-700 dark:hover:text-primary-300 transition-colors"
                >
                  {name}
                </button>
              ))}
            </div>
          )}
        </div>
        <div className="flex items-stretch gap-2">
          {logoSlot}
          <div className="relative flex items-center flex-1 min-w-0">
            <input
              ref={inputRef}
              id="itemName"
              type="text"
              value={value}
              onChange={handleNameChange}
              className={`w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:text-white ${selectedFolderId ? 'pr-28' : 'pr-10'}`}
              required
            />
            {/* Folder Button inside input - always visible, width adjusts based on whether a folder is selected */}
            <button
              type="button"
              onClick={handleOpenFolderModal}
              className={`absolute right-1 z-10 flex items-center gap-1 px-2 py-1 rounded transition-colors text-xs ${
                selectedFolderId
                  ? 'bg-primary-100 dark:bg-primary-900/30 text-primary-600 dark:text-primary-400 hover:bg-primary-200 dark:hover:bg-primary-900/50'
                  : 'text-gray-500 dark:text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-600'
              }`}
              title={selectedFolder?.Name || '-'}
            >
              <FolderIcon
                variant="outline"
                isShared={selectedFolder !== undefined && isSharedFolder(selectedFolder, personalManifestId)}
                className="w-4 h-4"
                badgeClassName="bg-white dark:bg-gray-700 ring-gray-200 dark:ring-gray-600"
              />
              {selectedFolderId && (
                <span className="max-w-16 truncate">
                  {selectedFolder?.Name}
                </span>
              )}
            </button>
          </div>
        </div>
      </div>

      {/* Folder Selection Modal */}
      <ModalWrapper isOpen={showFolderModal} onClose={handleCloseFolderModal} title={t('items.folders.folder')} maxWidth="max-w-sm">
        {/* Folder Options - Tree View */}
        <div className="space-y-1 max-h-64 overflow-y-auto">
          {/* No Folder Option */}
          <button
            type="button"
            onClick={() => handleSelectFolder(null)}
            className={`w-full px-3 py-2 text-left rounded-md flex items-center gap-3 transition-colors ${
              !selectedFolderId
                ? 'bg-primary-50 dark:bg-primary-900/20 text-primary-700 dark:text-primary-300'
                : 'text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700'
            }`}
          >
            <Icon name="archive" className={`w-5 h-5 ${!selectedFolderId ? 'text-primary-500' : 'text-gray-400'}`} />
            <span className="font-medium">&mdash;</span>
            {!selectedFolderId && (
              <Icon name="check" className="w-5 h-5 ml-auto text-primary-600 dark:text-primary-400" />
            )}
          </button>

          {/* Folder Tree, one row per visible folder; each level indents by one chevron slot */}
          {visibleFolders.map(node => {
            const nodeKey = scopedKey(node.ManifestId, node.Id);
            const isExpanded = expandedFolders.has(nodeKey);
            const hasChildren = node.children.length > 0;
            const isSelected = selectedFolderId === node.Id && selectedManifestId === node.ManifestId;
            return (
              <div
                key={nodeKey}
                className={`flex items-center rounded-md transition-colors ${isSelected ? 'bg-primary-50 dark:bg-primary-900/20 text-primary-700 dark:text-primary-300' : 'text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700'}`}
                style={{ paddingLeft: `${node.depth * FOLDER_INDENT_PX + 4}px` }}
              >
                {hasChildren ? (
                  <button
                    type="button"
                    onClick={() => toggleFolder(nodeKey)}
                    aria-expanded={isExpanded}
                    className="flex-shrink-0 w-6 h-6 flex items-center justify-center rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors"
                  >
                    <Icon name="chevron-right" className={`w-3 h-3 transition-transform ${isExpanded ? 'rotate-90' : ''}`} />
                  </button>
                ) : (
                  <span className="flex-shrink-0 w-6" />
                )}
                <button
                  type="button"
                  onClick={() => handleSelectFolder({ Id: node.Id, ManifestId: node.ManifestId })}
                  className="flex-1 min-w-0 pl-1 pr-3 py-2 text-left flex items-center gap-2"
                >
                  <FolderIcon
                    variant="outline"
                    isShared={isSharedFolder(node, personalManifestId)}
                    className={`w-5 h-5 flex-shrink-0 ${isSelected ? 'text-primary-500' : 'text-gray-400'}`}
                    badgeClassName="bg-white dark:bg-gray-800 ring-gray-200 dark:ring-gray-600"
                  />
                  <span className="font-medium flex-1 truncate">{node.Name}</span>
                  {isSelected && (
                    <Icon name="check" className="w-5 h-5 flex-shrink-0 text-primary-600 dark:text-primary-400" />
                  )}
                </button>
              </div>
            );
          })}
        </div>
      </ModalWrapper>
    </>
  );
};

export default ItemNameInput;