📦 EqualifyEverything / equalify-hub

📄 reports.tsx · 423 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423import type { FC } from 'hono/jsx';
import type { Context } from 'hono';
import { Layout } from '#src/components/Layout';
import { getCurrentUser, getGitHubToken, fetchGitHub } from '#src/utils/auth';
import { renderMarkdown } from '#src/utils/markdown';

const styles = `
/* Report cards */
.report-card {
    background: var(--color-bg-secondary);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 16px;
    transition: all 0.2s;
    text-decoration: none;
    display: block;
}
.report-card:hover {
    border-color: #C8102E;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
.report-card h3 {
    margin: 0 0 8px 0;
    font-size: 16px;
    color: #C8102E;
}
.report-card p {
    margin: 0;
    font-size: 14px;
    color: var(--color-text-secondary);
}
.report-card .report-date {
    font-size: 12px;
    color: #4b5563;
    margin-top: 8px;
}

/* Single report view */
.report-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--color-border);
}
.report-header h1 {
    margin: 0;
    font-size: 28px;
    color: var(--color-text);
}
.back-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--color-text-secondary);
    font-size: 14px;
    text-decoration: none;
    margin-bottom: 16px;
}
.back-link:hover {
    color: #C8102E;
}
.edit-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: 6px;
    color: var(--color-text-secondary);
    font-size: 14px;
    text-decoration: none;
    transition: all 0.2s;
}
.edit-btn:hover {
    border-color: #C8102E;
    color: #C8102E;
}
.report-meta {
    font-size: 14px;
    color: var(--color-text-secondary);
    margin-bottom: 24px;
}
.report-content {
    color: var(--color-text);
    line-height: 1.7;
}
.report-content h1 { font-size: 24px; margin: 24px 0 16px; color: var(--color-text); }
.report-content h2 { font-size: 20px; margin: 24px 0 12px; color: var(--color-text); border-bottom: 1px solid var(--color-border); padding-bottom: 8px; }
.report-content h3 { font-size: 16px; margin: 20px 0 10px; color: var(--color-text); }
.report-content p { margin: 0 0 16px; }
.report-content ul { margin: 0 0 16px; padding-left: 24px; list-style: disc; }
.report-content ol { margin: 0 0 16px; padding-left: 24px; list-style: decimal; }
.report-content li { margin: 4px 0; }
.report-content code { background: var(--color-bg-secondary); padding: 2px 6px; border-radius: 4px; font-size: 13px; }
.report-content pre { background: #1f2937; color: #e5e7eb; padding: 16px; border-radius: 6px; overflow-x: auto; margin: 0 0 16px; }
.report-content pre code { background: none; padding: 0; color: inherit; }
.report-content blockquote { border-left: 4px solid #C8102E; margin: 0 0 16px; padding: 12px 16px; background: var(--color-bg-secondary); color: var(--color-text-secondary); }
.report-content table { width: 100%; border-collapse: collapse; margin: 0 0 16px; }
.report-content th, .report-content td { border: 1px solid var(--color-border); padding: 8px 12px; text-align: left; }
.report-content th { background: var(--color-bg-secondary); font-weight: 600; }
.report-content a { color: #C8102E; }
.report-content img { max-width: 100%; border-radius: 6px; }
.report-content strong { color: var(--color-text); }
`;

interface ReportListItem {
    name: string;
    slug: string;
    title: string;
    description: string;
    author: string;
    date: string;
    dateFormatted: string;
}

interface ReportFile {
    name: string;
    title: string;
    author: string;
    date: string;
    dateFormatted: string;
    content: string;
    html_url: string;
}

interface Frontmatter {
    title: string;
    date: string;
    dateFormatted: string;
    description: string;
    author: string;
}

// Parse YAML frontmatter from markdown content
function parseFrontmatter(content: string, fallbackTitle: string): { frontmatter: Frontmatter; body: string } {
    const frontmatterMatch = content.match(/^---\s*\n([\s\S]*?)\n---\s*\n([\s\S]*)$/);

    let frontmatter: Frontmatter = {
        title: fallbackTitle,
        date: '',
        dateFormatted: '',
        description: '',
        author: ''
    };
    let body = content;

    if (frontmatterMatch) {
        const yaml = frontmatterMatch[1];
        body = frontmatterMatch[2];

        // Parse title
        const titleMatch = yaml.match(/^title:\s*["']?([^"'\n]+)["']?\s*$/m);
        if (titleMatch) {
            frontmatter.title = titleMatch[1].trim();
        }

        // Parse date (supports YYYY-MM-DD format)
        const dateMatch = yaml.match(/^date:\s*["']?(\d{4}-\d{2}-\d{2})["']?\s*$/m);
        if (dateMatch) {
            frontmatter.date = dateMatch[1];
            const dateObj = new Date(dateMatch[1] + 'T00:00:00');
            frontmatter.dateFormatted = dateObj.toLocaleDateString('en-US', {
                year: 'numeric',
                month: 'long',
                day: 'numeric'
            });
        }

        // Parse description
        const descMatch = yaml.match(/^description:\s*["']?([^"'\n]+)["']?\s*$/m);
        if (descMatch) {
            frontmatter.description = descMatch[1].trim();
        }

        // Parse author
        const authorMatch = yaml.match(/^author:\s*["']?([^"'\n]+)["']?\s*$/m);
        if (authorMatch) {
            frontmatter.author = authorMatch[1].trim();
        }
    } else {
        // No frontmatter - check for first H1 heading as title
        const h1Match = content.match(/^#\s+(.+)$/m);
        if (h1Match) {
            frontmatter.title = h1Match[1].trim();
        }
    }

    return { frontmatter, body };
}

// Convert filename to fallback title
function filenameToTitle(filename: string): string {
    return filename
        .replace('.md', '')
        .replace(/-/g, ' ')
        .replace(/\b\w/g, (c: string) => c.toUpperCase());
}

// Attribute reports/updates to Equalify Dashboard in their titles
// Examples:
//   "April 2026 Development Report" -> "April 2026 Equalify Dashboard Development Report"
//   "New Features & Bug Fixes" -> "Equalify Dashboard: New Features & Bug Fixes"
function attributeTitle(title: string): string {
    if (!title || title.toLowerCase().includes('equalify dashboard')) return title;
    // Insert "Equalify Dashboard" before "Development Report"
    if (/development report/i.test(title)) {
        return title.replace(/development report/i, 'Equalify Dashboard Development Report');
    }
    // Otherwise prefix with "Equalify Dashboard: "
    return `Equalify Dashboard: ${title}`;
}

// List view - shows reports + updates
export const ReportsListPage: FC<{ reports: ReportListItem[]; updates: ReportListItem[] }> = ({ reports, updates }) => {
    const user = getCurrentUser();

    return (
        <Layout title="Reports - Equalify Hub" styles={styles} user={user}>
            <div style="max-width:900px;margin:0 auto;padding:32px 48px 64px;">
                <h1 style="font-size:32px;font-weight:700;color:var(--color-text);margin:0 0 8px 0;">Reports</h1>
                <p style="color:var(--color-text-secondary);margin:0 0 32px 0;font-size:16px;">
                    Monthly development reports, performance metrics, and project updates for Equalify.
                </p>

                <h2 style="font-size:20px;font-weight:600;color:var(--color-text);margin:0 0 16px 0;">Monthly Development Reports</h2>

                {reports.length > 0 ? (
                    reports.map(report => (
                        <a href={`/reports/${report.slug}`} class="report-card">
                            <h3>{attributeTitle(report.title)}</h3>
                            {report.description && (
                                <p>{report.description}</p>
                            )}
                            {report.dateFormatted && (
                                <div class="report-date">{report.dateFormatted}</div>
                            )}
                        </a>
                    ))
                ) : (
                    <div style="background:#fef3c7;border:1px solid #fcd34d;border-radius:8px;padding:16px;">
                        <p style="margin:0;color:#713f12;">No reports available yet. Check back soon!</p>
                    </div>
                )}

                {updates.length > 0 && (
                    <>
                        <h2 style="font-size:20px;font-weight:600;color:var(--color-text);margin:40px 0 16px 0;">Updates</h2>
                        {updates.map(update => (
                            <a href={`/updates/${update.slug}`} class="report-card">
                                <h3>{attributeTitle(update.title)}</h3>
                                {update.description && (
                                    <p>{update.description}</p>
                                )}
                                {update.dateFormatted && (
                                    <div class="report-date">{update.dateFormatted}</div>
                                )}
                            </a>
                        ))}
                    </>
                )}
            </div>
        </Layout>
    );
};

// Single report view - shows one report's content
export const ReportsDocPage: FC<{ report: ReportFile }> = ({ report }) => {
    const user = getCurrentUser();
    const displayTitle = attributeTitle(report.title);

    return (
        <Layout title={`${displayTitle} - Reports - Equalify Hub`} styles={styles} user={user}>
            <div style="max-width:900px;margin:0 auto;padding:32px 48px 64px;">
                <a href="/reports" class="back-link">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M19 12H5M12 19l-7-7 7-7"/>
                    </svg>
                    Back to Reports
                </a>

                <div class="report-header">
                    <h1>{displayTitle}</h1>
                    <a href={report.html_url} class="edit-btn" rel="noopener" target="_blank">
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
                            <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
                        </svg>
                        Edit on GitHub
                    </a>
                </div>

                {(report.dateFormatted || report.author) && (
                    <div class="report-meta">
                        {report.dateFormatted && <>Published {report.dateFormatted}</>}
                        {report.dateFormatted && report.author && <> · </>}
                        {report.author && <>By {report.author}</>}
                    </div>
                )}

                <div class="report-content" dangerouslySetInnerHTML={{ __html: report.content }} />
            </div>
        </Layout>
    );
};

// Fetch a folder of markdown docs from equalify-docs repo
async function fetchDocFolder(folder: string, token: string): Promise<ReportListItem[]> {
    try {
        const contents = await fetchGitHub(
            `https://api.github.com/repos/EqualifyEverything/equalify-docs/contents/${folder}`,
            token
        );

        if (!Array.isArray(contents)) return [];

        const mdFiles = contents.filter((f: any) => f.name.endsWith('.md'));

        const items = await Promise.all(mdFiles.map(async (file: any) => {
            try {
                const fileData = await fetchGitHub(
                    `https://api.github.com/repos/EqualifyEverything/equalify-docs/contents/${folder}/${file.name}`,
                    token
                );

                if (fileData && fileData.content) {
                    const content = Buffer.from(fileData.content, 'base64').toString('utf-8');
                    const fallbackTitle = filenameToTitle(file.name);
                    const { frontmatter } = parseFrontmatter(content, fallbackTitle);

                    return {
                        name: file.name,
                        slug: file.name.replace('.md', ''),
                        title: frontmatter.title,
                        description: frontmatter.description,
                        author: frontmatter.author,
                        date: frontmatter.date,
                        dateFormatted: frontmatter.dateFormatted
                    };
                }
            } catch (error) {
                console.error(`Error fetching ${file.name}:`, error);
            }

            const fallbackTitle = filenameToTitle(file.name);
            return {
                name: file.name,
                slug: file.name.replace('.md', ''),
                title: fallbackTitle,
                description: '',
                author: '',
                date: '',
                dateFormatted: ''
            };
        }));

        items.sort((a, b) => {
            if (!a.date && !b.date) return a.title.localeCompare(b.title);
            if (!a.date) return 1;
            if (!b.date) return -1;
            return b.date.localeCompare(a.date);
        });

        return items;
    } catch (error) {
        console.error(`Error fetching ${folder}:`, error);
        return [];
    }
}

// Handler for list view
export async function reportsHandler(c: Context) {
    const token = getGitHubToken();

    const [reports, updates] = await Promise.all([
        fetchDocFolder('reports', token),
        fetchDocFolder('updates', token),
    ]);

    return c.html(<ReportsListPage reports={reports} updates={updates} />);
}

// Handler for single report view
export async function reportsDocHandler(c: Context) {
    const slug = c.req.param('slug');
    const token = getGitHubToken();
    const filename = `${slug}.md`;

    try {
        const fileData = await fetchGitHub(
            `https://api.github.com/repos/EqualifyEverything/equalify-docs/contents/reports/${filename}`,
            token
        );

        if (fileData && fileData.content) {
            const content = Buffer.from(fileData.content, 'base64').toString('utf-8');
            const fallbackTitle = filenameToTitle(filename);
            const { frontmatter, body } = parseFrontmatter(content, fallbackTitle);

            const report: ReportFile = {
                name: filename,
                title: frontmatter.title,
                author: frontmatter.author,
                date: frontmatter.date,
                dateFormatted: frontmatter.dateFormatted,
                content: renderMarkdown(body),
                html_url: fileData.html_url
            };

            return c.html(<ReportsDocPage report={report} />);
        }
    } catch (error) {
        console.error('Error fetching report:', error);
    }

    // Report not found - redirect to list
    return c.redirect('/reports');
}