puerto 0.31.0

Puerto: la CLI de un framework full-stack DDD en Rust. Genera proyectos con Clean Architecture, la inyección de dependencias cableada sola y soporte de SQLx, sin cablear nada a mano. Antes, puerto-framework.
import { useState } from "react";
import type { Esquemas } from "../../../api/cliente";
import { desenvolver } from "../../../api/http";
{% if protected -%}
import { useAuth } from "../../../auth/auth";
{% endif -%}
import { AlertaError, codigoError } from "../../../compartido/ui/alerta-error";
import { DialogoConfirmar } from "../../../compartido/ui/dialogo-confirmar";
import { useDependencias } from "../../../dependencias";
import { aProblema } from "../mensajes";

type {{ pascal }} = Esquemas["Dto{{ pascal }}"];

/**
 * «{{ label }}» de `{{ ident }}`, tras confirmarlo; si la API lo rechaza, el diálogo se queda
 * abierto y dice por qué. `alTerminar`, cuando se hizo.
 */
{%- if signature_inline %}
export function Boton{{ uc_pascal }}({ {{ ident }}, alTerminar }: { {{ ident }}: {{ pascal }}; alTerminar: () => void }) {
{%- else %}
export function Boton{{ uc_pascal }}({
  {{ ident }},
  alTerminar,
}: {
  {{ ident }}: {{ pascal }};
  alTerminar: () => void;
}) {
{%- endif %}
  const { api } = useDependencias();
{%- if owner_camel and roles %}
  const { puede, usuario } = useAuth();
{%- elif roles %}
  const { puede } = useAuth();
{%- elif protected %}
  const { usuario } = useAuth();
{%- endif %}
  const [confirmando, setConfirmando] = useState(false);
  const [fallo, setFallo] = useState<unknown>(null);

  const confirmar = async () => {
    setFallo(null);
    try {
{%- if post_inline %}
      await desenvolver(api.POST("/{{ plural }}/{id}/{{ verb }}", { params: { path: { id: {{ ident }}.id } } }));
{%- else %}
      await desenvolver(
        api.POST("/{{ plural }}/{id}/{{ verb }}", { params: { path: { id: {{ ident }}.id } } }),
      );
{%- endif %}
      setConfirmando(false);
      alTerminar();
    } catch (error) {
      setFallo(error);
    }
  };
  const volver = () => {
    setConfirmando(false);
    setFallo(null);
  };

  // 28.5: la regla del dominio (`puede_{{ verb }}`) dice si ahora se puede.
  if (!{{ ident }}.{{ puede_camel }}) return null;
{%- if owner_camel %}

  // Su propietario{% if roles %}, o quien tiene su rol{% endif %} (28.2).
  const propio = usuario?.id === {{ ident }}.{{ owner_camel }};
{%- if not roles %}
  if (!propio) return null;
{%- elif roles | length == 1 %}
  if (!(propio || puede("{{ roles.0 }}"))) return null;
{%- else %}
  if (!(propio || ({% for r in roles %}puede("{{ r }}"){% if not loop.last %} && {% endif %}{% endfor %}))) return null;
{%- endif %}
{%- elif roles %}

{%- if roles | length == 1 %}
  if (!puede("{{ roles.0 }}")) return null;
{%- else %}
  if (!({% for r in roles %}puede("{{ r }}"){% if not loop.last %} && {% endif %}{% endfor %})) return null;
{%- endif %}
{%- elif protected %}

  if (usuario === null) return null;
{%- endif %}

  return (
    <>
      <button type="button" className="btn btn-warning" onClick={() => setConfirmando(true)}>
        {{ label }}
      </button>
      <DialogoConfirmar
        abierto={confirmando}
        mensaje="¿{{ label }}?"
        textoConfirmar="{{ label }}"
        textoCancelar="Volver"
        alConfirmar={confirmar}
        alCancelar={volver}
      >
        {fallo !== null && (
          <AlertaError error={fallo} mensaje={aProblema(codigoError(fallo)).texto} />
        )}
      </DialogoConfirmar>
    </>
  );
}