Swadhin Biswas Feature/ai code reviewer (#73) · 8/2/2026
105a287
258 lines · 9.9 KB
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
---
import SettingsLayout from "@/layouts/SettingsLayout.astro";

const user = Astro.locals.user;

if (!user) {
  return Astro.redirect("/login");
}
---

<SettingsLayout title="Public profile">
  <div class="max-w-[640px]">
    <!-- Header -->
    <div class="mb-6">
      <h2 class="text-[28px] font-semibold text-foreground">Public profile</h2>
      <p class="text-sm text-muted-foreground mt-1">
        This information will be displayed publicly so be careful what you share.
      </p>
    </div>

    <form id="profile-form" class="space-y-0">
      <!-- Avatar -->
      <div class="flex flex-col sm:flex-row gap-6 py-5 border-t border-border">
        <label class="text-sm font-medium text-foreground sm:w-[180px] sm:pt-2 shrink-0">
          Profile picture
        </label>
        <div class="flex-1">
          <div class="flex items-start gap-4">
            <div id="avatar-preview" class="w-[128px] h-[128px] rounded-full overflow-hidden border border-border bg-secondary flex-shrink-0">
              {user.avatarUrl ? (
                <img src={user.avatarUrl} alt={user.username} class="w-full h-full object-cover" id="avatar-img" />
              ) : (
                <div class="w-full h-full flex items-center justify-center text-4xl font-semibold text-muted-foreground bg-secondary" id="avatar-letter">
                  {user.username[0].toUpperCase()}
                </div>
              )}
            </div>
            <div class="flex-1 space-y-2">
              <input
                type="url"
                name="avatarUrl"
                id="avatarUrl"
                placeholder="https://example.com/avatar.jpg"
                value={user.avatarUrl || ""}
                class="w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-[#58a6ff] focus:ring-1 focus:ring-[#58a6ff]/30 transition-colors"
              />
              <p class="text-xs text-muted-foreground">
                URL to an image (PNG, JPG, GIF, SVG). Recommended 500x500px.
              </p>
            </div>
          </div>
        </div>
      </div>

      <!-- Name -->
      <div class="flex flex-col sm:flex-row gap-6 py-5 border-t border-border">
        <label for="displayName" class="text-sm font-medium text-foreground sm:w-[180px] sm:pt-2 shrink-0">
          Name
        </label>
        <div class="flex-1">
          <input
            type="text"
            name="displayName"
            id="displayName"
            value={user.displayName || ""}
            class="w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-[#58a6ff] focus:ring-1 focus:ring-[#58a6ff]/30 transition-colors"
          />
          <p class="text-xs text-muted-foreground mt-2">
            Your name may appear around OpenCodeHub where you contribute or are mentioned.
          </p>
        </div>
      </div>

      <!-- Bio -->
      <div class="flex flex-col sm:flex-row gap-6 py-5 border-t border-border">
        <label for="bio" class="text-sm font-medium text-foreground sm:w-[180px] sm:pt-2 shrink-0">
          Bio
        </label>
        <div class="flex-1">
          <textarea
            id="bio"
            name="bio"
            rows="3"
            maxlength="160"
            class="w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-[#58a6ff] focus:ring-1 focus:ring-[#58a6ff]/30 transition-colors resize-none"
          >{user.bio || ""}</textarea>
          <p class="text-xs text-muted-foreground mt-2">
            You can @mention other users or organizations to link to them.
            <span id="bio-count" class="float-right">{160 - (user.bio?.length || 0)}/160</span>
          </p>
        </div>
      </div>

      <!-- Company -->
      <div class="flex flex-col sm:flex-row gap-6 py-5 border-t border-border">
        <label for="company" class="text-sm font-medium text-foreground sm:w-[180px] sm:pt-2 shrink-0">
          Company
        </label>
        <div class="flex-1">
          <input
            type="text"
            name="company"
            id="company"
            value={user.company || ""}
            class="w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-[#58a6ff] focus:ring-1 focus:ring-[#58a6ff]/30 transition-colors"
          />
        </div>
      </div>

      <!-- Location -->
      <div class="flex flex-col sm:flex-row gap-6 py-5 border-t border-border">
        <label for="location" class="text-sm font-medium text-foreground sm:w-[180px] sm:pt-2 shrink-0">
          Location
        </label>
        <div class="flex-1">
          <input
            type="text"
            name="location"
            id="location"
            value={user.location || ""}
            placeholder="San Francisco, CA"
            class="w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-[#58a6ff] focus:ring-1 focus:ring-[#58a6ff]/30 transition-colors"
          />
        </div>
      </div>

      <!-- Website -->
      <div class="flex flex-col sm:flex-row gap-6 py-5 border-t border-border">
        <label for="website" class="text-sm font-medium text-foreground sm:w-[180px] sm:pt-2 shrink-0">
          Website
        </label>
        <div class="flex-1">
          <input
            type="url"
            name="website"
            id="website"
            placeholder="https://example.com"
            value={user.website || ""}
            class="w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-[#58a6ff] focus:ring-1 focus:ring-[#58a6ff]/30 transition-colors"
          />
        </div>
      </div>

      <!-- Email (read-only) -->
      <div class="flex flex-col sm:flex-row gap-6 py-5 border-t border-border">
        <label class="text-sm font-medium text-foreground sm:w-[180px] sm:pt-2 shrink-0">
          Email
        </label>
        <div class="flex-1">
          <input
            type="email"
            value={user.email || ""}
            disabled
            class="w-full rounded-md border border-border bg-secondary px-3 py-1.5 text-sm text-muted-foreground cursor-not-allowed"
          />
          <p class="text-xs text-muted-foreground mt-2">
            Your email is used for notifications and sign-in. <a href="/settings/account" class="text-[#58a6ff] hover:underline">Change in account settings</a>.
          </p>
        </div>
      </div>

      <!-- Submit -->
      <div class="flex justify-end py-6 border-t border-border mt-2">
        <div class="flex items-center gap-3">
          <span id="save-status" class="text-sm hidden">
            <span class="text-green-500">Profile updated successfully.</span>
          </span>
          <button
            type="submit"
            class="px-4 py-1.5 rounded-md bg-[#238636] border border-[#2ea043] text-sm font-medium text-white hover:bg-[#2ea043] transition-colors"
          >
            Update profile
          </button>
        </div>
      </div>
    </form>
  </div>
</SettingsLayout>

<script>
  const form = document.getElementById("profile-form") as HTMLFormElement;
  const avatarInput = document.getElementById("avatarUrl") as HTMLInputElement;
  const avatarPreview = document.getElementById("avatar-preview");
  const bioTextarea = document.getElementById("bio") as HTMLTextAreaElement;
  const bioCount = document.getElementById("bio-count");
  const saveStatus = document.getElementById("save-status");

  // Live avatar preview
  let debounceTimer: any;
  avatarInput?.addEventListener("input", () => {
    clearTimeout(debounceTimer);
    debounceTimer = setTimeout(() => {
      const url = avatarInput.value.trim();
      if (url && avatarPreview) {
        const img = new Image();
        img.onload = () => {
          avatarPreview.innerHTML = `<img src="${url}" alt="Avatar" class="w-full h-full object-cover" />`;
        };
        img.onerror = () => {};
        img.src = url;
      }
    }, 500);
  });

  // Bio character count
  bioTextarea?.addEventListener("input", () => {
    if (bioCount) {
      const remaining = 160 - bioTextarea.value.length;
      bioCount.textContent = `${remaining}/160`;
      bioCount.className = remaining < 0 ? "float-right text-red-500" : "float-right";
    }
  });

  // Form submission
  form?.addEventListener("submit", async (e) => {
    e.preventDefault();
    const formData = new FormData(form);
    const data = Object.fromEntries(formData);

    const submitBtn = form.querySelector('button[type="submit"]') as HTMLButtonElement;
    const originalText = submitBtn.textContent;
    submitBtn.disabled = true;
    submitBtn.textContent = "Updating...";

    try {
      const response = await fetch("/api/auth/me", {
        method: "PATCH",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data),
      });

      if (response.ok) {
        submitBtn.textContent = "Updated!";
        if (saveStatus) {
          saveStatus.classList.remove("hidden");
        }
        setTimeout(() => {
          submitBtn.textContent = originalText;
          submitBtn.disabled = false;
          if (saveStatus) {
            saveStatus.classList.add("hidden");
          }
        }, 3000);
      } else {
        const error = await response.json();
        alert(error.message || "Failed to update profile");
        submitBtn.textContent = originalText;
        submitBtn.disabled = false;
      }
    } catch (error) {
      console.error("Error updating profile:", error);
      alert("An error occurred while updating profile");
      submitBtn.textContent = originalText;
      submitBtn.disabled = false;
    }
  });
</script>