Components
🧩 Frontend Key Components Specification Last Updated: June 29, 2025
This document describes the roles and structures of key React
components, Contexts
, and custom Hooks
in the comfy-surfai-frontend-next
project.
1. Global Contexts & Hooks​
These are core elements that manage application-wide state and logic.
a. AuthContext.tsx
​
- Location:
src/contexts/AuthContext.tsx
- Role: An "Authentication State Manager" that globally manages the user's authentication status (login status, user information) and provides related functions (login, logout).
- Key Provided Values (
value
):user: User | null
: User information object for the currently logged-in user.null
if not logged in.isLoading: boolean
: Becomestrue
while checking authentication status during app startup or login/logout.login(credentials)
: Function to handle standard email/password login.logout()
: Function to log out the user and delete related cookies.fetchUserProfile()
: Function to request profile information from the server and update theuser
state.
b. useComfyWebSocket.ts
​
- Location:
src/hooks/useComfyWebSocket.ts
- Role: A "Real-time Communication Manager" that communicates in real-time with the backend's
WebSocket
server, receives all events occurring during AI generation, and manages related states. - Key Return Values:
isWsConnected: boolean
:WebSocket
connection status.executionStatus: string | null
: Current task status text, such as "Generating...", "Final result received!".progressValue: { value, max } | null
: Progress of nodes likeKSampler
.sessionOutputs: HistoryItemData[]
: List of generated results during the current session on theGenerate
page. (Maintains up to 20 items)
c. apiClient.ts
​
- Location:
src/lib/apiClient.ts
- Role: A "Central Communication Gateway" responsible for all
HTTP
communication with the backend API. - Key Features:
- Automatic Token Re-issuance: If an Access Token expires and a
401
error is received during an API request, it automatically reissues a new token using the Refresh Token and retries the original failed request. - Automatic CSRF Header Addition: When making state-changing requests (e.g.,
POST
,PUT
,DELETE
), it reads theXSRF-TOKEN
cookie value and automatically adds theX-XSRF-TOKEN
header to defend against CSRF attacks. - Automatic Cookie Sending: The
credentials: 'include'
option ensures that the browser automatically includes authentication-relatedHttpOnly
cookies with all requests.
- Automatic Token Re-issuance: If an Access Token expires and a
2. Page Components​
Located within the src/app/
directory, these are "smart" components that serve as entry points for each route.
a. generate/page.tsx
​
- Role: Main page for AI image/video generation.
- Key Logic:
- Manages real-time status using the
useComfyWebSocket
hook. - Fetches workflow template list via API and passes it to
TemplateForm
. - Calls the generation API (
POST /api/generate
) with parameters received fromTemplateForm
. - Displays generated results for the current session via
SessionGallery
. - Manages the open/closed state of
ImageLightbox
.
- Manages real-time status using the
b. history/page.tsx
​
- Role: "My Album" page that permanently displays all generated results by the user.
- Key Logic:
- Fetches a list of user's generation records by calling the backend's
/my-outputs
API. - Implements pagination (infinite scroll) functionality via a "Load More" button.
- Reuses
HistoryGallery
andGeneratedItem
components to display results. - Manages the open/closed state of
ImageLightbox
.
- Fetches a list of user's generation records by calling the backend's
c. admin/workflows/new/page.tsx
and admin/workflows/[id]/edit/page.tsx
​
- Role: Admin page for creating and modifying workflow templates.
- Key Logic:
- Includes access control logic that checks the user's
role
viauseAuth
upon page access and redirects immediately if notadmin
. - Reuses the
ParameterMappingForm
component to render the parameter settings UI. - Creation Page: Manages a two-step flow (basic information input -> parameter mapping) and calls appropriate APIs (
POST /workflow-templates
,PUT /.../parameter-map
) for each step. - Edit Page: Upon page load, fetches existing template data to populate all forms, and calls
PATCH /workflow-templates/:id
API to update all changes at once when 'Save' is clicked.
- Includes access control logic that checks the user's
3. Reusable Components​
Located in the src/components/
directory, these are "dumb" components responsible for specific UI fragments or functionalities.
a. ParameterMappingForm.tsx
​
- Location:
src/components/admin/ParameterMappingForm.tsx
- Role: A "Parameter Mapping Specialist Component" responsible for the entire complex and dynamic form UI used to create and modify the
parameter_map
of workflow templates. - Reusability:
- Creation Page (
new/page.tsx
): Used in the second step. ReceivesonSave
andonBack
functions to perform its own save/cancel logic via internal buttons. - Edit Page (
edit/page.tsx
): Included as part of the page. Renders withoutonSave
andonBack
, only responsible for displaying and modifying data. Final saving is handled by the main 'Save' button on the edit page.
- Creation Page (
- Key Features:
- Category-based Logic: Calls API based on the
category
prop to fetch relevant parameter preset lists. - Automatic Required Parameter Addition: In
new
page mode, automatically adds required parameters corresponding to the category to the list. - Intelligent UI:
- Displays detailed information (
inputs
,class_type
) of a node when selected. - Allows auto-completion of
input_name
with a single click. - Parameters added from presets have their
key
andtype
locked to prevent errors. - Required parameters are disabled to prevent deletion.
- Already added presets are disabled in the 'Add Parameter' dropdown.
- Displays detailed information (
- State Management: Directly receives
parameterMap
state andsetParameterMap
function as props, directly controlling the parent component's (page's) state.
- Category-based Logic: Calls API based on the
b. GeneratedItem.tsx
​
- Role: Renders a single generated output card displayed in the gallery. Reused in both
SessionGallery
andHistoryGallery
. - Key Features:
- Draws UI based on
item
data (HistoryItemData
) received as a prop. - Conditionally renders an image (
<img>
) or video (<video>
) based onitem.mimeType
. - Analyzes
item.usedParameters
to calculate and display video playback duration. - Determines file expiration based on
item.createdAt
and displays an alternative UI if expired. - Includes zoom/download/delete buttons, and calls handler functions (
onImageClick
,onDelete
) received from the parent when clicked.
- Draws UI based on
c. ImageLightbox.tsx
​
- Role: A modal that covers the entire screen, displaying a zoomed-in image and detailed metadata when a user clicks an image in the gallery.
- Key Features:
- Renders based on the
item
object (HistoryItemData | null
) received as a prop. Hidden ifitem
isnull
. - Displays media (image/video) largely on the left using
item.viewUrl
. - Displays detailed metadata such as
item.usedParameters
,item.createdAt
in a scrollable area on the right. - Calls the
onClose
handler when the outer background or 'X' button is clicked to close it.
- Renders based on the