refactor: extrair data fetching da page de pagadores para data.ts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Felipe Coutinho
2026-02-27 15:40:52 +00:00
parent 842919bce5
commit e644d67022
3 changed files with 113 additions and 88 deletions

30
lib/avatar/options.ts Normal file
View File

@@ -0,0 +1,30 @@
import { readdir } from "node:fs/promises";
import path from "node:path";
import { DEFAULT_PAGADOR_AVATAR } from "@/lib/pagadores/constants";
const AVATAR_DIRECTORY = path.join(process.cwd(), "public", "avatares");
const AVATAR_EXTENSIONS = new Set([".png", ".jpg", ".jpeg", ".svg", ".webp"]);
/**
* Loads available avatar files from the public/avatares directory
* @returns Array of unique avatar filenames sorted alphabetically
*/
export async function loadAvatarOptions() {
try {
const files = await readdir(AVATAR_DIRECTORY, { withFileTypes: true });
const items = files
.filter((file) => file.isFile())
.map((file) => file.name)
.filter((file) => AVATAR_EXTENSIONS.has(path.extname(file).toLowerCase()))
.sort((a, b) => a.localeCompare(b, "pt-BR", { sensitivity: "base" }));
if (items.length === 0) {
items.push(DEFAULT_PAGADOR_AVATAR);
}
return Array.from(new Set(items));
} catch {
return [DEFAULT_PAGADOR_AVATAR];
}
}