|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | +import type { BatchRequest, BatchResponse } from '@/lib/api/batch'; |
| 3 | + |
| 4 | +export const runtime = 'edge'; |
| 5 | + |
| 6 | +export interface HelpArticle { |
| 7 | + id: string; |
| 8 | + title: string; |
| 9 | + content: string; |
| 10 | + category: string; |
| 11 | + tags: string[]; |
| 12 | +} |
| 13 | + |
| 14 | +/** Static help content keyed by article id */ |
| 15 | +const HELP_ARTICLES: Record<string, HelpArticle> = { |
| 16 | + 'getting-started': { |
| 17 | + id: 'getting-started', |
| 18 | + title: 'Getting Started with TeachLink', |
| 19 | + content: |
| 20 | + 'Welcome to TeachLink! Connect your Starknet wallet to begin exploring courses, earning reputation, and tipping creators.', |
| 21 | + category: 'Onboarding', |
| 22 | + tags: ['wallet', 'starknet', 'beginner'], |
| 23 | + }, |
| 24 | + 'wallet-connect': { |
| 25 | + id: 'wallet-connect', |
| 26 | + title: 'Connecting Your Wallet', |
| 27 | + content: |
| 28 | + 'TeachLink supports Argent X and Braavos wallets. Click the "Connect Wallet" button in the top navigation to get started.', |
| 29 | + category: 'Web3', |
| 30 | + tags: ['wallet', 'argent', 'braavos'], |
| 31 | + }, |
| 32 | + tipping: { |
| 33 | + id: 'tipping', |
| 34 | + title: 'How Tipping Works', |
| 35 | + content: |
| 36 | + 'Send on-chain tips to course creators using STRK tokens. Tips are processed via smart contracts on Starknet.', |
| 37 | + category: 'Web3', |
| 38 | + tags: ['tips', 'strk', 'creators'], |
| 39 | + }, |
| 40 | + courses: { |
| 41 | + id: 'courses', |
| 42 | + title: 'Browsing and Enrolling in Courses', |
| 43 | + content: |
| 44 | + 'Browse courses by topic, filter by skill level, and enroll with a single click. Progress is tracked on-chain.', |
| 45 | + category: 'Learning', |
| 46 | + tags: ['courses', 'enroll', 'progress'], |
| 47 | + }, |
| 48 | + reputation: { |
| 49 | + id: 'reputation', |
| 50 | + title: 'Building Your Reputation', |
| 51 | + content: |
| 52 | + 'Earn reputation points by completing courses, contributing to discussions, and receiving tips from peers.', |
| 53 | + category: 'Gamification', |
| 54 | + tags: ['reputation', 'points', 'achievements'], |
| 55 | + }, |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * POST /api/help |
| 60 | + * |
| 61 | + * Accepts a batch of help article requests and returns all matching articles |
| 62 | + * in a single response, reducing round-trips for the HelpDocumentation component. |
| 63 | + * |
| 64 | + * Body: { requests: BatchRequest[] } |
| 65 | + * Response: { responses: BatchResponse<HelpArticle>[] } |
| 66 | + */ |
| 67 | +export async function POST(request: Request) { |
| 68 | + let body: { requests: BatchRequest[] }; |
| 69 | + |
| 70 | + try { |
| 71 | + body = await request.json(); |
| 72 | + } catch { |
| 73 | + return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 }); |
| 74 | + } |
| 75 | + |
| 76 | + if (!Array.isArray(body?.requests)) { |
| 77 | + return NextResponse.json({ error: 'requests must be an array' }, { status: 400 }); |
| 78 | + } |
| 79 | + |
| 80 | + const responses: BatchResponse<HelpArticle>[] = body.requests.map((req) => { |
| 81 | + const article = HELP_ARTICLES[req.path]; |
| 82 | + if (!article) { |
| 83 | + return { id: req.id, error: `Article not found: ${req.path}` }; |
| 84 | + } |
| 85 | + return { id: req.id, data: article }; |
| 86 | + }); |
| 87 | + |
| 88 | + return NextResponse.json({ responses }); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * GET /api/help?ids=id1,id2 |
| 93 | + * |
| 94 | + * Convenience endpoint for fetching multiple articles by comma-separated ids. |
| 95 | + */ |
| 96 | +export async function GET(request: Request) { |
| 97 | + const { searchParams } = new URL(request.url); |
| 98 | + const ids = searchParams.get('ids')?.split(',').filter(Boolean) ?? []; |
| 99 | + |
| 100 | + if (ids.length === 0) { |
| 101 | + const all = Object.values(HELP_ARTICLES); |
| 102 | + return NextResponse.json({ articles: all }); |
| 103 | + } |
| 104 | + |
| 105 | + const articles = ids.map((id) => HELP_ARTICLES[id.trim()]).filter(Boolean); |
| 106 | + return NextResponse.json({ articles }); |
| 107 | +} |
0 commit comments