Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
import SqliteClient from '@aliasvault/client/database/SqliteClient';
import { getBuiltinLogoSvg, ItemTypeIconSvgs } from '@aliasvault/models/icons';
import {
FieldKey,
LogoKinds,
ItemTypes,
type Item,
} from '@aliasvault/models/vault';
import React from 'react';
import type { ItemTypeIconKey } from '@aliasvault/models/icons';
/**
* Corner rounding for raster logos, proportional to the icon size so every size looks the same.
*/
const LOGO_ROUNDING = 'rounded-[17%]';
type ItemIconProps = {
item: Item;
className?: string;
};
/**
* Renders an SVG string as a React component using dangerouslySetInnerHTML.
* The SVG is wrapped in a div to apply className for sizing.
*/
const SvgIcon: React.FC<{ svg: string; className?: string }> = ({ svg, className = 'w-8 h-8' }) => (
<div
className={`${className} flex-shrink-0`}
dangerouslySetInnerHTML={{ __html: svg }}
/>
);
/**
* Detect credit card brand from card number using BIN prefixes.
*/
const detectCardBrand = (cardNumber: string | undefined): ItemTypeIconKey => {
if (!cardNumber) {
return 'credit-card';
}
const cleaned = cardNumber.replace(/[\s-]/g, '');
if (!/^\d{4,}/.test(cleaned)) {
return 'credit-card';
}
if (/^4/.test(cleaned)) {
return 'visa';
}
if (/^5[1-5]/.test(cleaned) || /^2[2-7]/.test(cleaned)) {
return 'mastercard';
}
if (/^3[47]/.test(cleaned)) {
return 'amex';
}
if (/^6(?:011|22|4[4-9]|5)/.test(cleaned)) {
return 'discover';
}
return 'credit-card';
};
/**
* Get the appropriate SVG icon for a credit card brand.
*/
const getCardIconSvg = (cardNumber: string | undefined): string => {
return ItemTypeIconSvgs[detectCardBrand(cardNumber)];
};
/**
* ItemIcon component - displays contextually appropriate icons based on item type
*
* An icon the user picked (built-in or uploaded) wins for any item type.
* Otherwise:
* For Login/Alias: Uses the Logo field if available, falls back to placeholder
* For CreditCard: Shows card brand icons (Visa, Mastercard, Amex, Discover) based on card number
* For Note: Shows a document/note icon
*/
const ItemIcon: React.FC<ItemIconProps> = ({ item, className = 'w-8 h-8' }) => {
/*
* An icon the user picked themselves outranks the item type's own icon: they asked for this
* drawing on this item. Automatic favicons do not, so a card keeps showing a card.
*/
const chosen = item.LogoInfo && item.LogoInfo.Kind !== LogoKinds.Favicon ? item.LogoInfo : null;
if (chosen?.Kind === LogoKinds.Builtin) {
const builtinSvg = getBuiltinLogoSvg(chosen.Source);
if (builtinSvg) {
return <SvgIcon svg={builtinSvg} className={className} />;
}
}
if (chosen?.Kind === LogoKinds.Custom) {
const uploadedSrc = item.Logo ? SqliteClient.imgSrcFromBytes(item.Logo) : null;
if (uploadedSrc) {
return <img src={uploadedSrc} alt={item.Name ?? 'Item'} className={`${className} flex-shrink-0 ${LOGO_ROUNDING}`} />;
}
}
// For Note type, always show note icon
if (item.ItemType === ItemTypes.Note) {
return <SvgIcon svg={ItemTypeIconSvgs.note} className={className} />;
}
// For CreditCard type, detect card brand and show appropriate icon
if (item.ItemType === ItemTypes.CreditCard) {
const cardNumberField = item.Fields?.find(f => f.FieldKey === FieldKey.CardNumber);
const cardNumber = cardNumberField?.Value
? (Array.isArray(cardNumberField.Value) ? cardNumberField.Value[0] : cardNumberField.Value)
: undefined;
return <SvgIcon svg={getCardIconSvg(cardNumber)} className={className} />;
}
/*
* For Login/Alias types, use Logo if available, otherwise placeholder.
*/
const logoSrc = item.Logo ? SqliteClient.imgSrcFromBytes(item.Logo) : null;
if (logoSrc) {
return (
<img
src={logoSrc}
alt={item.Name || 'Item'}
className={`${className} flex-shrink-0 ${LOGO_ROUNDING}`}
onError={(e) => {
// On error, replace with placeholder icon
const target = e.target as HTMLImageElement;
target.style.display = 'none';
const parent = target.parentElement;
if (parent) {
const placeholder = document.createElement('div');
placeholder.className = className;
placeholder.innerHTML = ItemTypeIconSvgs.placeholder;
parent.insertBefore(placeholder, target);
}
}}
/>
);
}
// Default placeholder for Login/Alias without logo
return <SvgIcon svg={ItemTypeIconSvgs.placeholder} className={className} />;
};
export default ItemIcon;
// Export the SvgIcon component and icon utilities for direct use if needed
export { SvgIcon, getCardIconSvg };