import { getQuestions, submitQuestion } from "~/lib/api"; import { frostedBox } from "~/components/frosted-box"; function escapeHtml(str: string): string { const div = document.createElement("div"); div.textContent = str; return div.innerHTML; } export async function qaPage(outlet: HTMLElement) { outlet.innerHTML = `
${frostedBox(`
Ask a Question
0/200

Loading...
`)}
`; const form = document.getElementById("qa-form") as HTMLFormElement; const input = document.getElementById("qa-input") as HTMLTextAreaElement; const charCount = document.getElementById("char-count")!; const status = document.getElementById("qa-status")!; const list = document.getElementById("qa-list")!; input.addEventListener("input", () => { charCount.textContent = `${input.value.length}/200`; }); form.addEventListener("submit", (e) => { e.preventDefault(); const question = input.value.trim(); if (!question) return; status.textContent = "Submitting..."; status.style.color = "var(--light-gray)"; submitQuestion(question) .then(() => { input.value = ""; charCount.textContent = "0/200"; status.textContent = "Question submitted! It will appear here once answered."; status.style.color = "var(--light-green)"; }) .catch((err: unknown) => { status.textContent = err instanceof Error ? err.message : "Failed to submit question."; status.style.color = "var(--light-red)"; }); }); try { const questions = await getQuestions(); if (questions.length === 0) { list.textContent = "No questions answered yet."; list.style.color = "var(--dark-gray)"; } else { list.innerHTML = questions .map( (q) => `
#${String(q.id)}

${escapeHtml(q.question)}

${escapeHtml(q.answer)}

Asked ${q.created_at} ยท Answered ${q.answered_at}

`, ) .join(""); } } catch { list.textContent = "Failed to load questions."; list.style.color = "var(--light-red)"; list.style.textAlign = "center"; } }