"use client" import type React from "react" import { useEffect, useRef, useState } from "react" import { Flag, Mail, ArrowLeft, CheckCircle2 } from "lucide-react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" const RESEND_COOLDOWN = 30 // seconds function isValidEmail(value: string) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value.trim()) } export function LoginForm() { const [email, setEmail] = useState("") const [touched, setTouched] = useState(false) const [sent, setSent] = useState(false) const [sending, setSending] = useState(false) const [cooldown, setCooldown] = useState(0) const emailValid = isValidEmail(email) const showError = touched && email.length > 0 && !emailValid // Countdown timer for the resend cooldown. useEffect(() => { if (cooldown <= 0) return const id = setInterval(() => { setCooldown((c) => (c <= 1 ? 0 : c - 1)) }, 1000) return () => clearInterval(id) }, [cooldown]) async function sendLink() { // Mock only — pretend to dispatch a magic link. setSending(true) await new Promise((r) => setTimeout(r, 700)) setSending(false) setSent(true) setCooldown(RESEND_COOLDOWN) } function handleSubmit(e: React.FormEvent) { e.preventDefault() setTouched(true) if (!emailValid || sending) return void sendLink() } function handleResend() { if (cooldown > 0 || sending) return void sendLink() } function handleReset() { setSent(false) setTouched(false) } return (
Vi har sendt en innloggingslenke til{" "} {email}. Åpne den på denne enheten for å logge inn.
Fikk du ingen e-post? Send på nytt om{" "} {cooldown}s
) : ( )}