'use client';

import { useRouter } from 'next/navigation';
import { useState, useTransition } from 'react';

export function MemberLogoutButton() {
  const router = useRouter();
  const [pending, startTransition] = useTransition();
  return (
    <button
      type="button"
      disabled={pending}
      onClick={() =>
        startTransition(async () => {
          await fetch('/api/my/logout', { method: 'POST' });
          router.replace('/');
        })
      }
      className="rounded border border-gray-300 px-3 py-1.5 text-sm hover:bg-gray-50 disabled:opacity-50"
    >
      {pending ? 'Signing out…' : 'Sign out'}
    </button>
  );
}
