Files
openmonetis/lib/pagadores/utils.ts
Felipe Coutinho a70a83dd9d feat(pagadores): adicionar widget no dashboard e atualizar avatares
- Novo widget de pagadores no dashboard com resumo de transações
- Substituir avatares SVG por PNG com melhor qualidade
- Melhorar seção de pagador no diálogo de lançamentos
- Adicionar ação para buscar pagadores por nome
- Atualizar componentes de seleção (cartões, categorias, contas)
- Melhorias no layout de ajustes e relatórios

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 01:44:50 +00:00

71 lines
1.7 KiB
TypeScript

import { DEFAULT_PAGADOR_AVATAR } from "./constants";
/**
* Normaliza o caminho do avatar extraindo apenas o nome do arquivo.
* Remove qualquer caminho anterior e retorna null se não houver avatar.
* Preserva URLs completas (http/https/data).
*/
export const normalizeAvatarPath = (
avatar: string | null | undefined,
): string | null => {
if (!avatar) return null;
// Preservar URLs completas (Google, etc)
if (
avatar.startsWith("http://") ||
avatar.startsWith("https://") ||
avatar.startsWith("data:")
) {
return avatar;
}
const file = avatar.split("/").filter(Boolean).pop();
return file ?? avatar;
};
/**
* Retorna o caminho completo para o avatar, com fallback para o avatar padrão.
* Se o avatar for uma URL completa (http/https/data), retorna diretamente.
*/
export const getAvatarSrc = (avatar: string | null | undefined): string => {
if (!avatar) {
return `/avatares/${DEFAULT_PAGADOR_AVATAR}`;
}
// Se for uma URL completa (Google, etc), retorna diretamente
if (
avatar.startsWith("http://") ||
avatar.startsWith("https://") ||
avatar.startsWith("data:")
) {
return avatar;
}
// Se for um caminho local, normaliza e adiciona o prefixo
const normalized = normalizeAvatarPath(avatar);
return `/avatares/${normalized ?? DEFAULT_PAGADOR_AVATAR}`;
};
/**
* Normaliza nome a partir de email
*/
export const normalizeNameFromEmail = (
email: string | null | undefined,
): string => {
if (!email) {
return "Novo pagador";
}
const [local] = email.split("@");
if (!local) {
return "Novo pagador";
}
return local
.split(".")
.map((segment) =>
segment.length > 0
? segment[0]?.toUpperCase().concat(segment.slice(1))
: segment,
)
.join(" ");
};