Files
CatoandClaude Opus 5 376b94f89d refactor: one rate limiter, one auth shell, one create flow, one icon frame (#198)
* refactor(rate-limit): one limiter from limitkit, keyed on the proxy's hop

Two hand-rolled limiters lived side by side — lib/rate-limit.ts and
lib/middleware/rate-limit.ts — each with its own window maths, its own
header names and its own idea of who the caller is. Both are gone; the
decision now comes from limitkit (fleet/SHARED.md), and what stays here
is the one table of app budgets in lib/rate-limit.ts. A route names a
bucket; it does not get to invent a number.

The reason this matters beyond tidiness: both copies keyed the limit on
the FIRST hop of X-Forwarded-For. That hop is written by the client, so
any caller could mint a fresh budget per request by adding a header —
the limiter looked like it worked and enforced nothing. limitkit counts
from the right, i.e. the hop our own proxy wrote. A test now pins it:
a forged first hop must not mint a fresh bucket.

Also removed the Supabase check_rate_limit RPC path, which failed OPEN
on every error — the limiter switched itself off whenever the database
hiccupped. And 429s now carry Retry-After and X-RateLimit-* headers plus
a retryAfter field, which is what the auth forms count down.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KAn51JHFcTrPeCjZDGXM8

* refactor(icons): each icon body written once

Icons.tsx repeated the same 24-box outline SVG wrapper for every icon —
a 121-line self-clone. One `Outline` frame and one `icon(paths, ...)`
builder replace it; an icon is now its path data and nothing else.

Every export and every path string is byte-identical to before, so the
rendered output does not move.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KAn51JHFcTrPeCjZDGXM8

* refactor(create): one QuickCreateFlow behind /create and /personal

The two pages held the same three-step flow twice — 192 cloned lines of
state machine, header, progress indicator and step switch. They differ
only in the copy around the template picker, so that is what the pages
now pass in: /personal supplies its title block and its link out to the
professionals, /create supplies neither.

Both renders are unchanged, including that /personal's intro and outro
only appear on the picker step.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KAn51JHFcTrPeCjZDGXM8

* refactor(auth): one shell, one field, one submit button for the auth screens

signup, signin, forgot-password and verify-email each carried their own
copy of the page frame, the error line, the labelled input and the
submit button — six clone pairs with signup in every one. They now share
AuthShell / AuthAlert / AuthField / AuthSubmitButton, and the per-form
error-plus-countdown state lives once in useAuthError.

One real fix falls out of it: isRateLimitError compared the error code
against 'RATE_LIMITED', but the API has always sent 'RATE_LIMIT'. The
rate-limit countdown therefore never showed. It does now.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KAn51JHFcTrPeCjZDGXM8

* chore(lint): console.info is not logging, and every disable says why

`no-console` allowed `info`, so debug lines shipped as operator output:
the blog route narrated every render to the server log at info level.
Those lines now go through lib/logger (the single logger) at the level
they actually are, and the allowance is gone — warn and error only. The
21 remaining console.log calls are all in scripts/test-db-connection.ts,
where printing to the terminal is the point.

The eslint-disable count drops 16 -> 13. Comments.tsx no longer needs one
(useSyncExternalStore is the supported way to ask "are we on the client"),
and lib/supabase.ts no longer needs its `any` — the SSG mock casts once at
the return instead. The 13 that remain each name their reason: external
avatar URLs that next/image would need a host allowlist for, and effects
that seed state from an external store.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KAn51JHFcTrPeCjZDGXM8

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 01:24:12 -07:00

185 lines
6.1 KiB
TypeScript

'use client';
import { useState, type ReactNode } from 'react';
import Link from 'next/link';
import { TemplatePicker } from './TemplatePicker';
import { NameStep } from './NameStep';
import { QuickChat } from './QuickChat';
import { type BotTemplate, getTemplateById } from '@/lib/bot-templates';
type Step = 'pick' | 'name' | 'chat';
interface CreatedBot {
template: BotTemplate;
name: string;
systemPrompt: string;
}
interface QuickCreateFlowProps {
/** Shown above the progress bar while the reader is still choosing a template. */
intro?: ReactNode;
/** Shown under the template picker while the reader is still choosing. */
outro?: ReactNode;
}
/**
* The three-step "pick a template → name it → chat" flow.
*
* One component behind both /create and /personal — they differ only in the
* copy around the picker, which the pages pass in.
*/
export function QuickCreateFlow({ intro, outro }: QuickCreateFlowProps) {
const [step, setStep] = useState<Step>('pick');
const [selectedTemplate, setSelectedTemplate] = useState<BotTemplate | null>(null);
const [createdBot, setCreatedBot] = useState<CreatedBot | null>(null);
const handleTemplateSelect = (templateId: string) => {
const template = getTemplateById(templateId);
if (template) {
setSelectedTemplate(template);
setStep('name');
}
};
const handleNameSubmit = (name: string, additionalContext?: string) => {
if (!selectedTemplate) return;
// Build the system prompt from template
const systemPrompt = selectedTemplate.systemPromptTemplate
.replace('{{name}}', name)
.replace('{{additional_context}}', additionalContext || '');
setCreatedBot({
template: selectedTemplate,
name,
systemPrompt,
});
setStep('chat');
};
const handleBack = () => {
if (step === 'name') {
setStep('pick');
setSelectedTemplate(null);
} else if (step === 'chat') {
setStep('name');
}
};
const handleReset = () => {
setStep('pick');
setSelectedTemplate(null);
setCreatedBot(null);
};
return (
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-white to-action-tint">
{/* Background Effects */}
<div className="absolute inset-0 overflow-hidden pointer-events-none">
<div className="absolute -top-40 -right-40 w-80 h-80 bg-gradient-to-br from-action to-action-hover rounded-full opacity-10 blur-3xl" />
<div className="absolute -bottom-40 -left-40 w-96 h-96 bg-gradient-to-tr from-action to-action-hover rounded-full opacity-10 blur-3xl" />
</div>
<main className="relative max-w-screen-xl mx-auto px-6 py-12">
{/* Header */}
<div className="flex items-center justify-between mb-8">
<Link
href="/"
className="text-gray-500 hover:text-gray-700 transition-colors flex items-center gap-2"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M10 19l-7-7m0 0l7-7m-7 7h18"
/>
</svg>
Back to Home
</Link>
{step !== 'pick' && (
<button
onClick={handleBack}
className="text-gray-500 hover:text-gray-700 transition-colors flex items-center gap-2"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M15 19l-7-7 7-7"
/>
</svg>
Back
</button>
)}
</div>
{step === 'pick' && intro}
{/* Progress Indicator */}
<div className="flex items-center justify-center gap-4 mb-12">
<div
className={`flex items-center gap-2 ${step === 'pick' ? 'text-action' : 'text-gray-400'}`}
>
<div
className={`w-8 h-8 rounded-full flex items-center justify-center text-sm font-semibold ${
step === 'pick' ? 'bg-action text-white' : 'bg-gray-200 text-gray-500'
}`}
>
1
</div>
<span className="hidden sm:inline font-medium">Choose</span>
</div>
<div className={`w-12 h-0.5 ${step !== 'pick' ? 'bg-action' : 'bg-gray-200'}`} />
<div
className={`flex items-center gap-2 ${step === 'name' ? 'text-action' : 'text-gray-400'}`}
>
<div
className={`w-8 h-8 rounded-full flex items-center justify-center text-sm font-semibold ${
step === 'name'
? 'bg-action text-white'
: step === 'chat'
? 'bg-action text-white'
: 'bg-gray-200 text-gray-500'
}`}
>
2
</div>
<span className="hidden sm:inline font-medium">Name</span>
</div>
<div className={`w-12 h-0.5 ${step === 'chat' ? 'bg-action' : 'bg-gray-200'}`} />
<div
className={`flex items-center gap-2 ${step === 'chat' ? 'text-action' : 'text-gray-400'}`}
>
<div
className={`w-8 h-8 rounded-full flex items-center justify-center text-sm font-semibold ${
step === 'chat' ? 'bg-action text-white' : 'bg-gray-200 text-gray-500'
}`}
>
3
</div>
<span className="hidden sm:inline font-medium">Chat</span>
</div>
</div>
{/* Step Content */}
{step === 'pick' && <TemplatePicker onSelect={handleTemplateSelect} />}
{step === 'name' && selectedTemplate && (
<NameStep template={selectedTemplate} onSubmit={handleNameSubmit} onBack={handleBack} />
)}
{step === 'chat' && createdBot && <QuickChat bot={createdBot} onReset={handleReset} />}
{step === 'pick' && outro}
</main>
</div>
);
}