Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

refactor: extract invitation action presentation

Josué207034a18fe0
diff --git a/profile-ui/app/invitations/[token]/InvitationActionPanel.tsx b/profile-ui/app/invitations/[token]/InvitationActionPanel.tsx
new file mode 100644
index 000000000..0f8fa9850
--- /dev/null
+++ b/profile-ui/app/invitations/[token]/InvitationActionPanel.tsx
@@ -0,0 +1,167 @@
+import type { KeyboardEvent, MouseEvent, PointerEvent } from 'react'
+import { Button } from '@components/Button'
+import { FormField } from '@components/FormField'
+import { TextInput } from '@components/TextInput'
+
+type InvitationAction = 'accept' | 'password'
+type SubmittingAction = InvitationAction | null
+
+type InvitationActionPanelProps =
+ | {
+ mode: 'session-check'
+ disabled: boolean
+ checking: boolean
+ onRetrySession: () => void
+ loginHref: string
+ }
+ | {
+ mode: 'outcome-unknown'
+ loginHref: string
+ forgotPasswordHref: string
+ }
+ | {
+ mode: 'password-setup'
+ isPasswordReset: boolean
+ busy: boolean
+ submitting: SubmittingAction
+ password: string
+ confirmPassword: string
+ onPasswordChange: (value: string) => void
+ onConfirmPasswordChange: (value: string) => void
+ onPasswordKeyDown: (event: KeyboardEvent<HTMLInputElement>) => void
+ onPasswordAction: (
+ event: MouseEvent<HTMLButtonElement> | PointerEvent<HTMLButtonElement>
+ ) => void
+ }
+ | {
+ mode: 'accepted'
+ downloadUrl: string
+ loginHref: string
+ }
+ | { mode: 'expired' }
+ | {
+ mode: 'accept'
+ busy: boolean
+ submitting: SubmittingAction
+ onAcceptAction: (
+ event: MouseEvent<HTMLButtonElement> | PointerEvent<HTMLButtonElement>
+ ) => void
+ }
+
+function buttonLabel(submitting: SubmittingAction, kind: InvitationAction, label: string) {
+ if (submitting !== kind) return label
+ return (
+ <span className="cu-btn__content">
+ <span className="cu-btn__spinner" aria-hidden="true" />
+ <span>{kind === 'accept' ? 'Accepting...' : 'Saving...'}</span>
+ </span>
+ )
+}
+
+export function InvitationActionPanel(props: InvitationActionPanelProps) {
+ switch (props.mode) {
+ case 'session-check':
+ return (
+ <div className="stack">
+ <Button type="button" disabled={props.disabled} onClick={props.onRetrySession}>
+ {props.checking ? 'Checking account session...' : 'Retry account session check'}
+ </Button>
+ <a className="cu-btn" href={props.loginHref}>
+ Sign in instead
+ </a>
+ </div>
+ )
+ case 'outcome-unknown':
+ return (
+ <div className="stack">
+ <a className="cu-btn cu-btn--primary" href={props.loginHref}>
+ Try signing in
+ </a>
+ <a className="cu-btn" href={props.forgotPasswordHref}>
+ Request a new recovery link
+ </a>
+ </div>
+ )
+ case 'password-setup':
+ return (
+ <div className="stack">
+ <div className="form-card">
+ <strong>{props.isPasswordReset ? 'Set a new password' : 'Set your password'}</strong>
+ <div className="muted">
+ Use at least 8 characters. This password stays with your Evenfire account.
+ </div>
+ <FormField label="Password">
+ <TextInput
+ name="password"
+ type="password"
+ placeholder="At least 8 characters"
+ value={props.password}
+ onChange={event => props.onPasswordChange(event.currentTarget.value)}
+ onKeyDown={props.onPasswordKeyDown}
+ disabled={props.busy}
+ minLength={8}
+ required
+ />
+ </FormField>
+ <FormField label="Confirm password">
+ <TextInput
+ name="confirmPassword"
+ type="password"
+ placeholder="Repeat your password"
+ value={props.confirmPassword}
+ onChange={event => props.onConfirmPasswordChange(event.currentTarget.value)}
+ onKeyDown={props.onPasswordKeyDown}
+ disabled={props.busy}
+ minLength={8}
+ required
+ />
+ </FormField>
+ </div>
+ <Button
+ type="button"
+ disabled={props.busy}
+ onClick={props.onPasswordAction}
+ onPointerDown={props.onPasswordAction}
+ >
+ {buttonLabel(
+ props.submitting,
+ 'password',
+ props.isPasswordReset ? 'Reset password' : 'Set password and continue'
+ )}
+ </Button>
+ </div>
+ )
+ case 'accepted':
+ return (
+ <div className="stack">
+ <a
+ className="cu-btn cu-btn--primary"
+ href={props.downloadUrl}
+ target="_blank"
+ rel="noreferrer"
+ >
+ Download Evenfire
+ </a>
+ <a className="cu-btn" href={props.loginHref}>
+ Manage Profile
+ </a>
+ </div>
+ )
+ case 'expired':
+ return <div className="message message--error-plain">This invitation has expired.</div>
+ case 'accept':
+ return (
+ <div>
+ <Button
+ type="button"
+ disabled={props.busy}
+ onClick={props.onAcceptAction}
+ onPointerDown={props.onAcceptAction}
+ >
+ {buttonLabel(props.submitting, 'accept', 'Accept invitation')}
+ </Button>
+ </div>
+ )
+ }
+ return null
+}
diff --git a/profile-ui/app/invitations/[token]/InvitationClient.tsx b/profile-ui/app/invitations/[token]/InvitationClient.tsx
index 8b565bb9c..93bb7c2cd 100644
--- a/profile-ui/app/invitations/[token]/InvitationClient.tsx
+++ b/profile-ui/app/invitations/[token]/InvitationClient.tsx
@@ -11,9 +11,6 @@ import {
} from 'react'
import { useRouter } from 'next/navigation'
import { useAuth } from '@components/AuthContext'
-import { Button } from '@components/Button'
-import { FormField } from '@components/FormField'
-import { TextInput } from '@components/TextInput'
import { PROFILE_ROUTES } from '@constants/routes'
import {
ProfileApiError,
@@ -29,6 +26,7 @@ import {
} from '@lib/invitations'
import { formatTeamRole } from '@lib/teamRoles'
import type { InvitationPreview } from '@/app/types/api'
+import { InvitationActionPanel } from './InvitationActionPanel'
import type { InvitationClientProps } from './types'
type RecoveryPhase =
@@ -387,16 +385,6 @@ export function InvitationClient({
void handlePasswordSubmit()
}
- function buttonLabel(kind: 'accept' | 'password', label: string) {
- if (submitting !== kind) return label
- return (
- <span className="cu-btn__content">
- <span className="cu-btn__spinner" aria-hidden="true" />
- <span>{kind === 'accept' ? 'Accepting...' : 'Saving...'}</span>
- </span>
- )
- }
-
return (
<main className="center-page">
<section className="page-card">
@@ -452,103 +440,48 @@ export function InvitationClient({
) : null}
{!invitation ? null : recoveryNeedsSessionCheck ? (
- <div className="stack">
- <Button
- type="button"
- disabled={busy || recoveryPhase.state === 'checking_session' || !invitation.userId}
- onClick={() => void retryRecoveredSession()}
- >
- {recoveryPhase.state === 'checking_session'
- ? 'Checking account session...'
- : 'Retry account session check'}
- </Button>
- <a className="cu-btn" href={profileLoginHref}>
- Sign in instead
- </a>
- </div>
+ <InvitationActionPanel
+ mode="session-check"
+ disabled={busy || recoveryPhase.state === 'checking_session' || !invitation.userId}
+ checking={recoveryPhase.state === 'checking_session'}
+ onRetrySession={() => void retryRecoveredSession()}
+ loginHref={profileLoginHref}
+ />
) : isPasswordReset && recoveryOutcomeUnknown ? (
- <div className="stack">
- <a className="cu-btn cu-btn--primary" href={profileLoginHref}>
- Try signing in
- </a>
- <a className="cu-btn" href={forgotPasswordHref}>
- Request a new recovery link
- </a>
- </div>
+ <InvitationActionPanel
+ mode="outcome-unknown"
+ loginHref={profileLoginHref}
+ forgotPasswordHref={forgotPasswordHref}
+ />
) : (isPasswordReset && invitation.status === 'pending') ||
(invitation.status === 'accepted' && invitation.passwordPending) ? (
- <div className="stack">
- <div className="form-card">
- <strong>{isPasswordReset ? 'Set a new password' : 'Set your password'}</strong>
- <div className="muted">
- Use at least 8 characters. This password stays with your Evenfire account.
- </div>
- <FormField label="Password">
- <TextInput
- name="password"
- type="password"
- placeholder="At least 8 characters"
- value={password}
- onChange={event => setPassword(event.currentTarget.value)}
- onKeyDown={handlePasswordKeyDown}
- disabled={busy}
- minLength={8}
- required
- />
- </FormField>
- <FormField label="Confirm password">
- <TextInput
- name="confirmPassword"
- type="password"
- placeholder="Repeat your password"
- value={confirmPassword}
- onChange={event => setConfirmPassword(event.currentTarget.value)}
- onKeyDown={handlePasswordKeyDown}
- disabled={busy}
- minLength={8}
- required
- />
- </FormField>
- </div>
- <Button
- type="button"
- disabled={busy}
- onClick={handlePasswordAction}
- onPointerDown={handlePasswordAction}
- >
- {buttonLabel(
- 'password',
- isPasswordReset ? 'Reset password' : 'Set password and continue'
- )}
- </Button>
- </div>
+ <InvitationActionPanel
+ mode="password-setup"
+ isPasswordReset={isPasswordReset}
+ busy={busy}
+ submitting={submitting}
+ password={password}
+ confirmPassword={confirmPassword}
+ onPasswordChange={setPassword}
+ onConfirmPasswordChange={setConfirmPassword}
+ onPasswordKeyDown={handlePasswordKeyDown}
+ onPasswordAction={handlePasswordAction}
+ />
) : invitation.status === 'accepted' ? (
- <div className="stack">
- <a
- className="cu-btn cu-btn--primary"
- href={downloadUrl}
- target="_blank"
- rel="noreferrer"
- >
- Download Evenfire
- </a>
- <a className="cu-btn" href={profileLoginHref}>
- Manage Profile
- </a>
- </div>
+ <InvitationActionPanel
+ mode="accepted"
+ downloadUrl={downloadUrl}
+ loginHref={profileLoginHref}
+ />
) : invitationExpired ? (
- <div className="message message--error-plain">This invitation has expired.</div>
+ <InvitationActionPanel mode="expired" />
) : (
- <div>
- <Button
- type="button"
- disabled={busy}
- onClick={handleAcceptAction}
- onPointerDown={handleAcceptAction}
- >
- {buttonLabel('accept', 'Accept invitation')}
- </Button>
- </div>
+ <InvitationActionPanel
+ mode="accept"
+ busy={busy}
+ submitting={submitting}
+ onAcceptAction={handleAcceptAction}
+ />
)}
</section>
</main>
diff --git a/profile-ui/package-lock.json b/profile-ui/package-lock.json
index 74f3b09a3..b78143e7d 100644
--- a/profile-ui/package-lock.json
+++ b/profile-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-profile-ui",
- "version": "0.1.180",
+ "version": "0.1.181",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-profile-ui",
- "version": "0.1.180",
+ "version": "0.1.181",
"dependencies": {
"@clerum/desktop-app-links": "file:../packages/desktop-app-links",
"@clerum/frontend-components": "file:../packages/frontend-components",
diff --git a/profile-ui/package.json b/profile-ui/package.json
index 65d68d176..6ae0a2d4d 100644
--- a/profile-ui/package.json
+++ b/profile-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-profile-ui",
- "version": "0.1.180",
+ "version": "0.1.181",
"private": true,
"scripts": {
"dev": "next dev",