📦 EqualifyEverything / equalify-hub

📄 updates.tsx · 384 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
384import 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 = `
/* Update cards */
.update-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;
}
.update-card:hover {
    border-color: #C8102E;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
.update-card h3 {
    margin: 0 0 8px 0;
    font-size: 16px;
    color: #C8102E;
}
.update-card p {
    margin: 0;
    font-size: 14px;
    color: var(--color-text-secondary);
}
.update-card .update-date {
    font-size: 12px;
    color: var(--color-text-secondary);
    margin-top: 8px;
    opacity: 0.8;
}

/* Single update view */
.update-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--color-border);
}
.update-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;
}
.update-meta {
    font-size: 14px;
    color: var(--color-text-secondary);
    margin-bottom: 24px;
}
.update-content {
    color: var(--color-text);
    line-height: 1.7;
}
.update-content h1 { font-size: 24px; margin: 24px 0 16px; color: var(--color-text); }
.update-content h2 { font-size: 20px; margin: 24px 0 12px; color: var(--color-text); border-bottom: 1px solid var(--color-border); padding-bottom: 8px; }
.update-content h3 { font-size: 16px; margin: 20px 0 10px; color: var(--color-text); }
.update-content p { margin: 0 0 16px; }
.update-content ul, .update-content ol { margin: 0 0 16px; padding-left: 24px; }
.update-content li { margin: 4px 0; }
.update-content code { background: var(--color-bg-secondary); padding: 2px 6px; border-radius: 4px; font-size: 13px; }
.update-content pre { background: #1f2937; color: #e5e7eb; padding: 16px; border-radius: 6px; overflow-x: auto; margin: 0 0 16px; }
.update-content pre code { background: none; padding: 0; color: inherit; }
.update-content blockquote { border-left: 4px solid #C8102E; margin: 0 0 16px; padding: 12px 16px; background: var(--color-bg-secondary); color: var(--color-text-secondary); }
.update-content table { width: 100%; border-collapse: collapse; margin: 0 0 16px; }
.update-content th, .update-content td { border: 1px solid var(--color-border); padding: 8px 12px; text-align: left; }
.update-content th { background: var(--color-bg-secondary); font-weight: 600; }
.update-content a { color: #C8102E; }
.update-content img { max-width: 100%; border-radius: 6px; }
.update-content strong { color: var(--color-text); }
`;

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

interface UpdateFile {
    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());
}

// List view - shows all updates as clickable cards
export const UpdatesListPage: FC<{ updates: UpdateListItem[] }> = ({ updates }) => {
    const user = getCurrentUser();
    
    return (
        <Layout title="Updates - 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;">📢 Updates</h1>
                <p style="color:var(--color-text-secondary);margin:0 0 32px 0;font-size:16px;">
                    The latest news, announcements, and FAQs about Equalify.
                </p>
                
                {updates.length > 0 ? (
                    updates.map(update => (
                        <a href={`/updates/${update.slug}`} class="update-card">
                            <h3>{update.title}</h3>
                            {update.description && (
                                <p>{update.description}</p>
                            )}
                            {update.dateFormatted && (
                                <div class="update-date">{update.dateFormatted}</div>
                            )}
                        </a>
                    ))
                ) : (
                    <div style="background:#fef3c7;border:1px solid #fcd34d;border-radius:8px;padding:16px;">
                        <p style="margin:0;color:#92400e;">No updates available yet. Check back soon!</p>
                    </div>
                )}
            </div>
        </Layout>
    );
};

// Single update view - shows one update's content
export const UpdatesDocPage: FC<{ update: UpdateFile }> = ({ update }) => {
    const user = getCurrentUser();
    
    return (
        <Layout title={`${update.title} - Updates - Equalify Hub`} styles={styles} user={user}>
            <div style="max-width:900px;margin:0 auto;padding:32px 48px 64px;">
                <a href="/updates" 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 Updates
                </a>
                
                <div class="update-header">
                    <h1>{update.title}</h1>
                    <a href={update.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>
                
                {(update.dateFormatted || update.author) && (
                    <div class="update-meta">
                        {update.dateFormatted && <>Published {update.dateFormatted}</>}
                        {update.dateFormatted && update.author && <> · </>}
                        {update.author && <>By {update.author}</>}
                    </div>
                )}
                
                <div class="update-content" dangerouslySetInnerHTML={{ __html: update.content }} />
            </div>
        </Layout>
    );
};

// Handler for list view
export async function updatesHandler(c: Context) {
    const token = getGitHubToken();
    
    let updates: UpdateListItem[] = [];
    try {
        const contents = await fetchGitHub(
            'https://api.github.com/repos/EqualifyEverything/equalify-docs/contents/updates',
            token
        );
        
        if (Array.isArray(contents)) {
            const mdFiles = contents.filter((f: any) => f.name.endsWith('.md'));
            
            // Fetch content for each file to get frontmatter
            const updatePromises = mdFiles.map(async (file: any) => {
                try {
                    const fileData = await fetchGitHub(
                        `https://api.github.com/repos/EqualifyEverything/equalify-docs/contents/updates/${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);
                }
                
                // Fallback if content fetch fails
                const fallbackTitle = filenameToTitle(file.name);
                return {
                    name: file.name,
                    slug: file.name.replace('.md', ''),
                    title: fallbackTitle,
                    description: '',
                    author: '',
                    date: '',
                    dateFormatted: ''
                };
            });
            
            updates = await Promise.all(updatePromises);
            
            // Sort by date descending (newest first), files without dates go last
            updates.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);
            });
        }
    } catch (error) {
        console.error('Error fetching updates:', error);
    }
    
    return c.html(<UpdatesListPage updates={updates} />);
}

// Handler for single update view
export async function updatesDocHandler(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/updates/${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 update: UpdateFile = {
                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(<UpdatesDocPage update={update} />);
        }
    } catch (error) {
        console.error('Error fetching update:', error);
    }
    
    // Update not found - redirect to list
    return c.redirect('/updates');
}