Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot] 2026-04-17 20:00:56 +00:00
parent 75397543e8
commit bebe095d6e
4 changed files with 188 additions and 2 deletions

View file

@ -0,0 +1,50 @@
"use client";
import { supabase } from "@/integrations/supabase/client";
interface InvokeResult<T> {
data: T | null;
error: { message: string; status?: number } | null;
}
/**
* Wrapper para supabase.functions.invoke que normaliza erros de função
* (FunctionsHttpError vem com Response no .context que precisa ser lido).
*/
export async function invokeFunction<T = unknown>(
name: string,
body?: Record<string, unknown>,
): Promise<InvokeResult<T>> {
try {
const { data, error } = await supabase.functions.invoke<T>(name, {
body: body ?? {},
});
if (error) {
// Tenta extrair mensagem do response real
let msg = error.message ?? "Erro inesperado";
let status: number | undefined;
// deno-lint-ignore no-explicit-any
const ctx = (error as any).context as Response | undefined;
if (ctx && typeof ctx.json === "function") {
try {
status = ctx.status;
const parsed = await ctx.json();
if (parsed?.error) msg = parsed.error;
} catch {
// ignora
}
}
return { data: null, error: { message: msg, status } };
}
return { data: (data ?? null) as T | null, error: null };
} catch (err) {
return {
data: null,
error: {
message: err instanceof Error ? err.message : "Erro inesperado",
},
};
}
}

View file

@ -0,0 +1,25 @@
"use client";
/**
* Sanitiza um primeiro nome para uso em sugestões de username do Telegram.
* - remove acentos via NFD
* - remove caracteres não-alfanuméricos
* - lowercase
*/
export function sanitizeForUsername(name: string): string {
return (name || "")
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/[^a-zA-Z0-9]/g, "")
.toLowerCase();
}
export function suggestBotName(fullName: string | null | undefined): string {
const first = (fullName || "").trim().split(/\s+/)[0] || "Você";
return `Mika de ${first}`;
}
export function suggestBotUsername(fullName: string | null | undefined): string {
const first = sanitizeForUsername((fullName || "").trim().split(/\s+/)[0] || "voce");
return `mika_${first || "voce"}_bot`;
}