📦 EqualifyEverything / equalify-hub

📄 issues.ts · 206 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
206import type { Context } from 'hono';
import { getGitHubToken } from '#src/utils/auth';
import { renderPage } from '#src/utils/legacyLayout';

async function fetchIssue(owner: string, repo: string, issueNumber: string, token?: string) {
    const headers: Record<string, string> = {
        'Accept': 'application/vnd.github.v3+json',
        'User-Agent': 'Equalify-Open-Source'
    };
    if (token) headers['Authorization'] = `token ${token}`;

    const response = await fetch(
        `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}`,
        { headers }
    );
    if (!response.ok) return null;
    return response.json();
}

async function fetchComments(owner: string, repo: string, issueNumber: string, token?: string) {
    const headers: Record<string, string> = {
        'Accept': 'application/vnd.github.v3+json',
        'User-Agent': 'Equalify-Open-Source'
    };
    if (token) headers['Authorization'] = `token ${token}`;

    const response = await fetch(
        `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}/comments`,
        { headers }
    );
    if (!response.ok) return [];
    return response.json();
}

function formatDate(dateStr: string): string {
    const date = new Date(dateStr);
    return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });
}

function escapeHtml(str: string): string {
    return str
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
}

export const issues = async (c: Context) => {
    const owner = c.req.param('owner');
    const repo = c.req.param('repo');
    const issueNumber = c.req.param('number');
    const token = getGitHubToken();

    const issue = await fetchIssue(owner, repo, issueNumber, token);
    if (!issue) {
        return c.html(renderPage('Issue Not Found', `
            <div class="container">
                <h1>Issue #${issueNumber} not found</h1>
                <p>This issue may not exist or you may not have permission to view it.</p>
                <p><a href="/${owner}/${repo}">Back to ${owner}/${repo}</a></p>
            </div>
        `), 404);
    }

    const comments = await fetchComments(owner, repo, issueNumber, token);
    const stateColor = issue.state === 'open' ? '#3fb950' : '#a371f7';
    const stateIcon = issue.state === 'open' 
        ? '<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z"></path><path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Z"></path></svg>'
        : '<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M11.28 6.78a.75.75 0 0 0-1.06-1.06L7.25 8.69 5.78 7.22a.75.75 0 0 0-1.06 1.06l2 2a.75.75 0 0 0 1.06 0l3.5-3.5Z"></path><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0Zm-1.5 0a6.5 6.5 0 1 0-13 0 6.5 6.5 0 0 0 13 0Z"></path></svg>';

    const labelsHtml = issue.labels?.length 
        ? issue.labels.map((l: any) => `<span class="label" style="background:#${l.color};color:${parseInt(l.color, 16) > 0x7fffff ? '#000' : '#fff'}">${escapeHtml(l.name)}</span>`).join(' ')
        : '';

    const commentsHtml = comments.map((comment: any) => `
        <div class="comment">
            <div class="comment-header">
                <img src="${comment.user.avatar_url}" alt="${escapeHtml(comment.user.login)}" class="avatar">
                <strong><a href="/${comment.user.login}">${escapeHtml(comment.user.login)}</a></strong>
                <span class="meta">commented on ${formatDate(comment.created_at)}</span>
            </div>
            <div class="comment-body">${escapeHtml(comment.body || '')}</div>
        </div>
    `).join('');

    const content = `
        <div class="container">
            <div class="breadcrumb">
                <a href="/${owner}">${escapeHtml(owner)}</a> / <a href="/${owner}/${repo}">${escapeHtml(repo)}</a> / Issues
            </div>
            
            <div class="issue-header">
                <h1>${escapeHtml(issue.title)} <span class="issue-number">#${issue.number}</span></h1>
                <div class="issue-meta">
                    <span class="state" style="background:${stateColor}">${stateIcon} ${issue.state}</span>
                    <span><a href="/${issue.user.login}">${escapeHtml(issue.user.login)}</a> opened this issue on ${formatDate(issue.created_at)}</span>
                    <span>· ${issue.comments} comment${issue.comments !== 1 ? 's' : ''}</span>
                </div>
                ${labelsHtml ? `<div class="labels">${labelsHtml}</div>` : ''}
            </div>

            <div class="issue-body">
                <div class="comment main-comment">
                    <div class="comment-header">
                        <img src="${issue.user.avatar_url}" alt="${escapeHtml(issue.user.login)}" class="avatar">
                        <strong><a href="/${issue.user.login}">${escapeHtml(issue.user.login)}</a></strong>
                        <span class="meta">opened on ${formatDate(issue.created_at)}</span>
                    </div>
                    <div class="comment-body">${escapeHtml(issue.body || 'No description provided.')}</div>
                </div>
                ${commentsHtml}
            </div>

            <div class="actions">
                <a href="https://github.com/${owner}/${repo}/issues/${issueNumber}" class="btn" target="_blank" rel="noopener">View on GitHub →</a>
            </div>
        </div>
    `;

    const styles = `
        .breadcrumb { font-size: 14px; color: var(--color-text-secondary); margin-bottom: 16px; }
        .issue-header { margin-bottom: 24px; }
        .issue-header h1 { font-size: 28px; margin: 0 0 12px 0; line-height: 1.3; }
        .issue-number { color: var(--color-text-secondary); font-weight: normal; }
        .issue-meta { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--color-text-secondary); flex-wrap: wrap; }
        .state { display: inline-flex; align-items: center; gap: 4px; padding: 4px 12px; border-radius: 20px; color: #fff; font-size: 14px; font-weight: 500; text-transform: capitalize; }
        .labels { margin-top: 12px; display: flex; gap: 6px; flex-wrap: wrap; }
        .label { padding: 2px 8px; border-radius: 12px; font-size: 12px; font-weight: 500; }
        .comment { background: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; margin-bottom: 16px; }
        .comment-header { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--color-border); background: var(--color-bg-tertiary); font-size: 14px; }
        .avatar { width: 24px; height: 24px; border-radius: 50%; }
        .meta { color: var(--color-text-secondary); }
        .comment-body { padding: 16px; white-space: pre-wrap; font-size: 14px; line-height: 1.6; }
        .actions { margin-top: 24px; padding-top: 24px; border-top: 1px solid var(--color-border); }
        .btn { display: inline-block; background: var(--color-bg-secondary); border: 1px solid var(--color-border); padding: 8px 16px; border-radius: 6px; font-size: 14px; color: var(--color-text); }
        .btn:hover { background: var(--color-bg-tertiary); text-decoration: none; }
    `;

    return c.html(renderPage(`Issue #${issueNumber} · ${owner}/${repo}`, content, styles));
};

export const issuesList = async (c: Context) => {
    const owner = c.req.param('owner');
    const repo = c.req.param('repo');
    const token = getGitHubToken();

    const headers: Record<string, string> = {
        'Accept': 'application/vnd.github.v3+json',
        'User-Agent': 'Equalify-Open-Source'
    };
    if (token) headers['Authorization'] = `token ${token}`;

    const response = await fetch(
        `https://api.github.com/repos/${owner}/${repo}/issues?state=open&per_page=50`,
        { headers }
    );
    
    const issues = response.ok ? await response.json() : [];

    const issuesHtml = issues.length ? issues.map((issue: any) => `
        <div class="issue-row">
            <div class="issue-icon" style="color:${issue.state === 'open' ? '#3fb950' : '#a371f7'}">
                <svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z"></path><path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Z"></path></svg>
            </div>
            <div class="issue-content">
                <a href="/${owner}/${repo}/issues/${issue.number}" class="issue-title">${escapeHtml(issue.title)}</a>
                <div class="issue-meta">#${issue.number} opened on ${formatDate(issue.created_at)} by ${escapeHtml(issue.user.login)}</div>
            </div>
            <div class="issue-comments">${issue.comments > 0 ? `💬 ${issue.comments}` : ''}</div>
        </div>
    `).join('') : '<p class="empty">No open issues</p>';

    const content = `
        <div class="container">
            <div class="breadcrumb">
                <a href="/${owner}">${escapeHtml(owner)}</a> / <a href="/${owner}/${repo}">${escapeHtml(repo)}</a> / Issues
            </div>
            <h1>Issues</h1>
            <div class="issues-list">${issuesHtml}</div>
            <div class="actions">
                <a href="https://github.com/${owner}/${repo}/issues" class="btn" target="_blank" rel="noopener">View all on GitHub →</a>
            </div>
        </div>
    `;

    const styles = `
        .breadcrumb { font-size: 14px; color: var(--color-text-secondary); margin-bottom: 16px; }
        h1 { font-size: 24px; margin: 0 0 20px 0; }
        .issues-list { border: 1px solid var(--color-border); border-radius: 6px; }
        .issue-row { display: flex; align-items: flex-start; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--color-border); }
        .issue-row:last-child { border-bottom: none; }
        .issue-icon { padding-top: 2px; }
        .issue-content { flex: 1; }
        .issue-title { font-size: 15px; font-weight: 600; color: var(--color-text); }
        .issue-title:hover { color: var(--color-link); }
        .issue-meta { font-size: 12px; color: var(--color-text-secondary); margin-top: 4px; }
        .issue-comments { font-size: 12px; color: var(--color-text-secondary); }
        .empty { padding: 24px; text-align: center; color: var(--color-text-secondary); }
        .actions { margin-top: 16px; }
        .btn { display: inline-block; background: var(--color-bg-secondary); border: 1px solid var(--color-border); padding: 8px 16px; border-radius: 6px; font-size: 14px; color: var(--color-text); }
        .btn:hover { background: var(--color-bg-tertiary); text-decoration: none; }
    `;

    return c.html(renderPage(`Issues · ${owner}/${repo}`, content, styles));
};