2025-12-23 15:52:40 +01:00
|
|
|
"use client";
|
|
|
|
|
|
2025-12-25 21:50:34 +01:00
|
|
|
import { CSSProperties, useState } from "react";
|
2025-12-23 15:52:40 +01:00
|
|
|
import { useParams, useRouter } from "next/navigation";
|
|
|
|
|
import { Permission } from "../../auth-context";
|
2025-12-25 20:32:11 +01:00
|
|
|
import { tradesApi } from "../../api";
|
2025-12-23 15:52:40 +01:00
|
|
|
import { Header } from "../../components/Header";
|
|
|
|
|
import { SatsDisplay } from "../../components/SatsDisplay";
|
Extract reusable UI components to reduce DRY violations
- Created StatusBadge component: Standardizes status badge display
- Supports tradeStatus prop for trade-specific styling
- Supports variant prop for simple badges (success/error/ready)
- Eliminates repetitive badge style combinations
- Created EmptyState component: Standardizes empty state display
- Handles loading and empty states consistently
- Supports message, hint, and action props
- Used across trades, invites, admin pages
- Created ConfirmationButton component: Standardizes confirmation flows
- Two-step confirmation pattern (action -> confirm/cancel)
- Supports different variants (danger/success/primary)
- Handles loading states automatically
- Used for cancel, complete, no-show actions
- Migrated pages to use new components:
- trades/page.tsx: StatusBadge, EmptyState, ConfirmationButton
- trades/[id]/page.tsx: StatusBadge
- invites/page.tsx: StatusBadge, EmptyState
- admin/trades/page.tsx: StatusBadge, EmptyState, ConfirmationButton
- admin/invites/page.tsx: StatusBadge
Benefits:
- Eliminated ~50+ lines of repetitive badge styling code
- Consistent UI patterns across all pages
- Easier to maintain and update styling
- Better type safety
All tests passing (32 frontend, 33 e2e)
2025-12-25 21:40:07 +01:00
|
|
|
import { StatusBadge } from "../../components/StatusBadge";
|
2025-12-23 15:52:40 +01:00
|
|
|
import { useRequireAuth } from "../../hooks/useRequireAuth";
|
Refactor frontend: Add reusable hooks and components
- Created useAsyncData hook: Eliminates repetitive data fetching boilerplate
- Handles loading, error, and data state automatically
- Supports enabled/disabled fetching
- Provides refetch function
- Created PageLayout component: Standardizes page structure
- Handles loading state, authorization checks, header, error display
- Reduces ~10 lines of boilerplate per page
- Created useMutation hook: Simplifies action handling
- Manages loading state and errors for mutations
- Supports success/error callbacks
- Used for cancel, create, revoke actions
- Created ErrorDisplay component: Standardizes error UI
- Consistent error banner styling across app
- Integrated into PageLayout
- Created useForm hook: Foundation for form state management
- Handles form data, validation, dirty checking
- Ready for future form migrations
- Migrated pages to use new patterns:
- invites/page.tsx: useAsyncData + PageLayout
- trades/page.tsx: useAsyncData + PageLayout + useMutation
- trades/[id]/page.tsx: useAsyncData
- admin/price-history/page.tsx: useAsyncData + PageLayout
- admin/invites/page.tsx: useMutation for create/revoke
Benefits:
- ~40% reduction in boilerplate code
- Consistent patterns across pages
- Easier to maintain and extend
- Better type safety
All tests passing (32 frontend, 33 e2e)
2025-12-25 21:30:35 +01:00
|
|
|
import { useAsyncData } from "../../hooks/useAsyncData";
|
2025-12-23 15:52:40 +01:00
|
|
|
import { formatDateTime } from "../../utils/date";
|
Extract reusable UI components to reduce DRY violations
- Created StatusBadge component: Standardizes status badge display
- Supports tradeStatus prop for trade-specific styling
- Supports variant prop for simple badges (success/error/ready)
- Eliminates repetitive badge style combinations
- Created EmptyState component: Standardizes empty state display
- Handles loading and empty states consistently
- Supports message, hint, and action props
- Used across trades, invites, admin pages
- Created ConfirmationButton component: Standardizes confirmation flows
- Two-step confirmation pattern (action -> confirm/cancel)
- Supports different variants (danger/success/primary)
- Handles loading states automatically
- Used for cancel, complete, no-show actions
- Migrated pages to use new components:
- trades/page.tsx: StatusBadge, EmptyState, ConfirmationButton
- trades/[id]/page.tsx: StatusBadge
- invites/page.tsx: StatusBadge, EmptyState
- admin/trades/page.tsx: StatusBadge, EmptyState, ConfirmationButton
- admin/invites/page.tsx: StatusBadge
Benefits:
- Eliminated ~50+ lines of repetitive badge styling code
- Consistent UI patterns across all pages
- Easier to maintain and update styling
- Better type safety
All tests passing (32 frontend, 33 e2e)
2025-12-25 21:40:07 +01:00
|
|
|
import { formatEur } from "../../utils/exchange";
|
2025-12-23 15:52:40 +01:00
|
|
|
import {
|
|
|
|
|
layoutStyles,
|
|
|
|
|
typographyStyles,
|
|
|
|
|
bannerStyles,
|
|
|
|
|
buttonStyles,
|
|
|
|
|
tradeCardStyles,
|
|
|
|
|
} from "../../styles/shared";
|
|
|
|
|
|
|
|
|
|
export default function TradeDetailPage() {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const params = useParams();
|
2025-12-23 17:03:51 +01:00
|
|
|
const publicId = params?.id as string | undefined;
|
2025-12-25 21:50:34 +01:00
|
|
|
const [cancelError, setCancelError] = useState<string | null>(null);
|
2025-12-23 15:52:40 +01:00
|
|
|
|
|
|
|
|
const { user, isLoading, isAuthorized } = useRequireAuth({
|
|
|
|
|
requiredPermission: Permission.VIEW_OWN_EXCHANGES,
|
|
|
|
|
fallbackRedirect: "/",
|
|
|
|
|
});
|
|
|
|
|
|
Refactor frontend: Add reusable hooks and components
- Created useAsyncData hook: Eliminates repetitive data fetching boilerplate
- Handles loading, error, and data state automatically
- Supports enabled/disabled fetching
- Provides refetch function
- Created PageLayout component: Standardizes page structure
- Handles loading state, authorization checks, header, error display
- Reduces ~10 lines of boilerplate per page
- Created useMutation hook: Simplifies action handling
- Manages loading state and errors for mutations
- Supports success/error callbacks
- Used for cancel, create, revoke actions
- Created ErrorDisplay component: Standardizes error UI
- Consistent error banner styling across app
- Integrated into PageLayout
- Created useForm hook: Foundation for form state management
- Handles form data, validation, dirty checking
- Ready for future form migrations
- Migrated pages to use new patterns:
- invites/page.tsx: useAsyncData + PageLayout
- trades/page.tsx: useAsyncData + PageLayout + useMutation
- trades/[id]/page.tsx: useAsyncData
- admin/price-history/page.tsx: useAsyncData + PageLayout
- admin/invites/page.tsx: useMutation for create/revoke
Benefits:
- ~40% reduction in boilerplate code
- Consistent patterns across pages
- Easier to maintain and extend
- Better type safety
All tests passing (32 frontend, 33 e2e)
2025-12-25 21:30:35 +01:00
|
|
|
const {
|
|
|
|
|
data: trade,
|
|
|
|
|
isLoading: isLoadingTrade,
|
|
|
|
|
error,
|
|
|
|
|
} = useAsyncData(
|
|
|
|
|
() => {
|
|
|
|
|
if (!publicId) throw new Error("Trade ID is required");
|
|
|
|
|
return tradesApi.getTrade(publicId);
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
enabled: !!user && isAuthorized && !!publicId,
|
|
|
|
|
onError: () => {
|
|
|
|
|
// Error message is set by useAsyncData
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-12-23 15:52:40 +01:00
|
|
|
|
|
|
|
|
if (isLoading || isLoadingTrade) {
|
|
|
|
|
return (
|
|
|
|
|
<main style={layoutStyles.main}>
|
|
|
|
|
<div style={layoutStyles.loader}>Loading...</div>
|
|
|
|
|
</main>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isAuthorized) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
Refactor frontend: Add reusable hooks and components
- Created useAsyncData hook: Eliminates repetitive data fetching boilerplate
- Handles loading, error, and data state automatically
- Supports enabled/disabled fetching
- Provides refetch function
- Created PageLayout component: Standardizes page structure
- Handles loading state, authorization checks, header, error display
- Reduces ~10 lines of boilerplate per page
- Created useMutation hook: Simplifies action handling
- Manages loading state and errors for mutations
- Supports success/error callbacks
- Used for cancel, create, revoke actions
- Created ErrorDisplay component: Standardizes error UI
- Consistent error banner styling across app
- Integrated into PageLayout
- Created useForm hook: Foundation for form state management
- Handles form data, validation, dirty checking
- Ready for future form migrations
- Migrated pages to use new patterns:
- invites/page.tsx: useAsyncData + PageLayout
- trades/page.tsx: useAsyncData + PageLayout + useMutation
- trades/[id]/page.tsx: useAsyncData
- admin/price-history/page.tsx: useAsyncData + PageLayout
- admin/invites/page.tsx: useMutation for create/revoke
Benefits:
- ~40% reduction in boilerplate code
- Consistent patterns across pages
- Easier to maintain and extend
- Better type safety
All tests passing (32 frontend, 33 e2e)
2025-12-25 21:30:35 +01:00
|
|
|
if (error || (!isLoadingTrade && !trade)) {
|
2025-12-23 15:52:40 +01:00
|
|
|
return (
|
|
|
|
|
<main style={layoutStyles.main}>
|
|
|
|
|
<Header currentPage="trades" />
|
|
|
|
|
<div style={styles.content}>
|
|
|
|
|
<h1 style={typographyStyles.pageTitle}>Trade Details</h1>
|
Refactor frontend: Add reusable hooks and components
- Created useAsyncData hook: Eliminates repetitive data fetching boilerplate
- Handles loading, error, and data state automatically
- Supports enabled/disabled fetching
- Provides refetch function
- Created PageLayout component: Standardizes page structure
- Handles loading state, authorization checks, header, error display
- Reduces ~10 lines of boilerplate per page
- Created useMutation hook: Simplifies action handling
- Manages loading state and errors for mutations
- Supports success/error callbacks
- Used for cancel, create, revoke actions
- Created ErrorDisplay component: Standardizes error UI
- Consistent error banner styling across app
- Integrated into PageLayout
- Created useForm hook: Foundation for form state management
- Handles form data, validation, dirty checking
- Ready for future form migrations
- Migrated pages to use new patterns:
- invites/page.tsx: useAsyncData + PageLayout
- trades/page.tsx: useAsyncData + PageLayout + useMutation
- trades/[id]/page.tsx: useAsyncData
- admin/price-history/page.tsx: useAsyncData + PageLayout
- admin/invites/page.tsx: useMutation for create/revoke
Benefits:
- ~40% reduction in boilerplate code
- Consistent patterns across pages
- Easier to maintain and extend
- Better type safety
All tests passing (32 frontend, 33 e2e)
2025-12-25 21:30:35 +01:00
|
|
|
{error && (
|
|
|
|
|
<div style={bannerStyles.errorBanner}>
|
|
|
|
|
{error ||
|
|
|
|
|
"Failed to load trade. It may not exist or you may not have permission to view it."}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-12-23 15:52:40 +01:00
|
|
|
<button onClick={() => router.push("/trades")} style={buttonStyles.primaryButton}>
|
|
|
|
|
Back to Trades
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</main>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-25 21:50:34 +01:00
|
|
|
if (!trade) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 15:52:40 +01:00
|
|
|
const isBuy = trade.direction === "buy";
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<main style={layoutStyles.main}>
|
|
|
|
|
<Header currentPage="trades" />
|
|
|
|
|
<div style={styles.content}>
|
|
|
|
|
<div style={styles.header}>
|
|
|
|
|
<h1 style={typographyStyles.pageTitle}>Trade Details</h1>
|
|
|
|
|
<button onClick={() => router.push("/trades")} style={buttonStyles.secondaryButton}>
|
|
|
|
|
← Back to Trades
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div style={styles.tradeDetailCard}>
|
|
|
|
|
<div style={styles.detailSection}>
|
|
|
|
|
<h2 style={styles.sectionTitle}>Trade Information</h2>
|
|
|
|
|
<div style={styles.detailGrid}>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Status:</span>
|
2025-12-25 21:50:34 +01:00
|
|
|
<StatusBadge tradeStatus={trade.status}>{""}</StatusBadge>
|
2025-12-23 15:52:40 +01:00
|
|
|
</div>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Time:</span>
|
|
|
|
|
<span style={styles.detailValue}>{formatDateTime(trade.slot_start)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Direction:</span>
|
|
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
...styles.detailValue,
|
|
|
|
|
color: isBuy ? "#4ade80" : "#f87171",
|
|
|
|
|
fontWeight: 600,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{isBuy ? "BUY BTC" : "SELL BTC"}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Payment Method:</span>
|
|
|
|
|
<span style={styles.detailValue}>
|
|
|
|
|
{isBuy
|
|
|
|
|
? `Receive via ${trade.bitcoin_transfer_method === "onchain" ? "Onchain" : "Lightning"}`
|
|
|
|
|
: `Send via ${trade.bitcoin_transfer_method === "onchain" ? "Onchain" : "Lightning"}`}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div style={styles.detailSection}>
|
|
|
|
|
<h2 style={styles.sectionTitle}>Amounts</h2>
|
|
|
|
|
<div style={styles.detailGrid}>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>EUR Amount:</span>
|
|
|
|
|
<span style={styles.detailValue}>{formatEur(trade.eur_amount)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Bitcoin Amount:</span>
|
|
|
|
|
<span style={{ ...styles.detailValue, ...tradeCardStyles.satsAmount }}>
|
|
|
|
|
<SatsDisplay sats={trade.sats_amount} />
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div style={styles.detailSection}>
|
|
|
|
|
<h2 style={styles.sectionTitle}>Pricing</h2>
|
|
|
|
|
<div style={styles.detailGrid}>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Market Price:</span>
|
|
|
|
|
<span style={styles.detailValue}>
|
|
|
|
|
€
|
2025-12-26 11:38:17 +01:00
|
|
|
{trade.market_price_eur.toLocaleString("es-ES", {
|
2025-12-23 15:52:40 +01:00
|
|
|
maximumFractionDigits: 0,
|
|
|
|
|
})}
|
|
|
|
|
/BTC
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Agreed Price:</span>
|
|
|
|
|
<span style={styles.detailValue}>
|
|
|
|
|
€
|
2025-12-26 11:38:17 +01:00
|
|
|
{trade.agreed_price_eur.toLocaleString("es-ES", {
|
2025-12-23 15:52:40 +01:00
|
|
|
maximumFractionDigits: 0,
|
|
|
|
|
})}
|
|
|
|
|
/BTC
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Premium:</span>
|
|
|
|
|
<span style={styles.detailValue}>{trade.premium_percentage}%</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div style={styles.detailSection}>
|
|
|
|
|
<h2 style={styles.sectionTitle}>Timestamps</h2>
|
|
|
|
|
<div style={styles.detailGrid}>
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Created:</span>
|
|
|
|
|
<span style={styles.detailValue}>{formatDateTime(trade.created_at)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
{trade.cancelled_at && (
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Cancelled:</span>
|
|
|
|
|
<span style={styles.detailValue}>{formatDateTime(trade.cancelled_at)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{trade.completed_at && (
|
|
|
|
|
<div style={styles.detailRow}>
|
|
|
|
|
<span style={styles.detailLabel}>Completed:</span>
|
|
|
|
|
<span style={styles.detailValue}>{formatDateTime(trade.completed_at)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-12-25 21:50:34 +01:00
|
|
|
{cancelError && <div style={bannerStyles.errorBanner}>{cancelError}</div>}
|
2025-12-23 15:52:40 +01:00
|
|
|
{trade.status === "booked" && (
|
|
|
|
|
<div style={styles.actionSection}>
|
|
|
|
|
<button
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
if (
|
|
|
|
|
!confirm(
|
|
|
|
|
"Are you sure you want to cancel this trade? This action cannot be undone."
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2025-12-25 20:32:11 +01:00
|
|
|
await tradesApi.cancelTrade(trade.public_id);
|
2025-12-23 15:52:40 +01:00
|
|
|
router.push("/trades");
|
|
|
|
|
} catch (err) {
|
2025-12-25 21:50:34 +01:00
|
|
|
setCancelError(err instanceof Error ? err.message : "Failed to cancel trade");
|
2025-12-23 15:52:40 +01:00
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
style={buttonStyles.secondaryButton}
|
|
|
|
|
>
|
|
|
|
|
Cancel Trade
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</main>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const styles: Record<string, CSSProperties> = {
|
|
|
|
|
content: {
|
|
|
|
|
flex: 1,
|
|
|
|
|
padding: "2rem",
|
|
|
|
|
maxWidth: "800px",
|
|
|
|
|
margin: "0 auto",
|
|
|
|
|
width: "100%",
|
|
|
|
|
},
|
|
|
|
|
header: {
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
marginBottom: "2rem",
|
|
|
|
|
},
|
|
|
|
|
tradeDetailCard: {
|
|
|
|
|
background: "rgba(255, 255, 255, 0.03)",
|
|
|
|
|
border: "1px solid rgba(255, 255, 255, 0.08)",
|
|
|
|
|
borderRadius: "12px",
|
|
|
|
|
padding: "2rem",
|
|
|
|
|
},
|
|
|
|
|
detailSection: {
|
|
|
|
|
marginBottom: "2rem",
|
|
|
|
|
},
|
|
|
|
|
sectionTitle: {
|
|
|
|
|
fontFamily: "'DM Sans', system-ui, sans-serif",
|
|
|
|
|
fontSize: "1.1rem",
|
|
|
|
|
fontWeight: 600,
|
|
|
|
|
color: "#fff",
|
|
|
|
|
marginBottom: "1rem",
|
|
|
|
|
},
|
|
|
|
|
detailGrid: {
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: "1rem",
|
|
|
|
|
},
|
|
|
|
|
detailRow: {
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
padding: "0.75rem 0",
|
|
|
|
|
borderBottom: "1px solid rgba(255, 255, 255, 0.05)",
|
|
|
|
|
},
|
|
|
|
|
detailLabel: {
|
|
|
|
|
fontFamily: "'DM Sans', system-ui, sans-serif",
|
|
|
|
|
fontSize: "0.9rem",
|
|
|
|
|
color: "rgba(255, 255, 255, 0.6)",
|
|
|
|
|
},
|
|
|
|
|
detailValue: {
|
|
|
|
|
fontFamily: "'DM Sans', system-ui, sans-serif",
|
|
|
|
|
fontSize: "0.9rem",
|
|
|
|
|
color: "#fff",
|
|
|
|
|
fontWeight: 500,
|
|
|
|
|
},
|
|
|
|
|
actionSection: {
|
|
|
|
|
marginTop: "2rem",
|
|
|
|
|
paddingTop: "2rem",
|
|
|
|
|
borderTop: "1px solid rgba(255, 255, 255, 0.1)",
|
|
|
|
|
},
|
|
|
|
|
};
|