Files
sorsfonalak/services/frontend/src/components/RateContent.tsx
T
fecoandClaude Opus 5.5 62ae4a438e Feedback in the system: ratings, messages, the recommendation question
- Under the summary, the detailed analyses, the forecast and tarot: "How
  tailored to you did it feel?" (1–5 stars); thumbs under answers. Only
  the owner rates, once (changeable); tags, a comment, and a permission
  to read the text; model and prompt version are kept with it
- /visszajelzes: bug, idea, question, other (the page and the browser go
  with it, 10 a day); the state and the operator's reply show there, a
  dot in the account menu marks a new reply. No e-mail, no Telegram
- One NPS question (0–10) in a bar under the header, after two finished
  contents; closing it counts
- Admin → Visszajelzések: numbers by kind, week, tags, model and prompt,
  buyers and others, NPS; ratings with filters, the text only if allowed;
  messages with state, internal note and reply; a count in the admin menu
- Nothing is public. Deleting an account keeps the stars without a name
  or comment; messages go. Privacy policy (hu, en) updated
- The admin menu no longer shows a vertical scrollbar

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-24 07:01:40 +00:00

216 lines
6.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useEffect, useId, useState } from "react";
import { api, errorText } from "@/lib/api";
import { HIGH_TAGS, LOW_TAGS, TAG_LABELS } from "@/lib/feedback";
import type { Rating } from "@/lib/types";
const MAX_COMMENT = 1000;
/**
* "Mennyire érezted magadra szabottnak?": the user rates what they got (an
* analysis: 1–5 stars; an answer: thumbs). A click saves it at once; tags, a
* comment and the permission to read the text can follow. Only the
* operator sees it, nothing is public.
*/
export function RateContent({
analysisId,
questionId,
thumbs = false,
}: {
analysisId?: string;
questionId?: string;
/** Thumbs up/down instead of stars (answers to questions). */
thumbs?: boolean;
}) {
const id = useId();
const [rating, setRating] = useState<Rating | null>(null);
const [open, setOpen] = useState(false);
const [tags, setTags] = useState<string[]>([]);
const [comment, setComment] = useState("");
const [mayRead, setMayRead] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [saved, setSaved] = useState(false);
const target = analysisId
? { analysis_id: analysisId }
: { question_id: questionId };
useEffect(() => {
const query = analysisId
? `analysis_ids=${analysisId}`
: `question_ids=${questionId}`;
api<{ ratings: Rating[] }>(`/ratings?${query}`)
.then((r) => {
const mine = r.ratings[0];
if (!mine) return;
setRating(mine);
setTags(mine.tags);
setComment(mine.comment ?? "");
setMayRead(mine.may_read);
})
.catch(() => {});
}, [analysisId, questionId]);
async function save(stars: number, details: boolean) {
setBusy(true);
setError(null);
setSaved(false);
try {
const shown = stars >= 4 ? HIGH_TAGS : LOW_TAGS;
const next = await api<Rating>("/ratings", {
method: "PUT",
body: {
...target,
stars,
// Tags of the other side do not fit a changed rating.
tags: tags.filter((t) => shown.includes(t)),
comment: comment.trim() || null,
may_read: mayRead,
},
});
setRating(next);
setTags(next.tags);
if (details) {
setSaved(true);
setOpen(false);
} else {
setOpen(true);
}
} catch (e) {
setError(errorText(e));
} finally {
setBusy(false);
}
}
const stars = rating?.stars ?? 0;
const offered = stars >= 4 ? HIGH_TAGS : LOW_TAGS;
const toggle = (tag: string) =>
setTags((all) =>
all.includes(tag) ? all.filter((t) => t !== tag) : [...all, tag],
);
return (
<div className={thumbs ? "rate rate-thumbs" : "rate"}>
<div className="rate-row">
<span className="rate-question" id={`${id}-q`}>
{thumbs
? "Hasznos volt a válasz?"
: "Mennyire érezted magadra szabottnak?"}
</span>
<div className="rate-buttons" role="group" aria-labelledby={`${id}-q`}>
{thumbs
? [
{ value: 5, label: "Igen", icon: "👍" },
{ value: 1, label: "Nem", icon: "👎" },
].map((b) => (
<button
key={b.value}
type="button"
className={
stars === b.value ? "rate-thumb is-on" : "rate-thumb"
}
aria-pressed={stars === b.value}
aria-label={b.label}
disabled={busy}
onClick={() => save(b.value, false)}
>
<span aria-hidden="true">{b.icon}</span>
</button>
))
: [1, 2, 3, 4, 5].map((n) => (
<button
key={n}
type="button"
className={n <= stars ? "rate-star is-on" : "rate-star"}
aria-pressed={n === stars}
aria-label={`${n} csillag`}
disabled={busy}
onClick={() => save(n, false)}
>
<span aria-hidden="true">★</span>
</button>
))}
</div>
{rating && !open && (
<button
type="button"
className="btn btn-ghost btn-sm"
onClick={() => setOpen(true)}
>
{rating.comment || rating.tags.length
? "Módosítom"
: "Hozzáfűzök valamit"}
</button>
)}
</div>
{open && rating && (
<div className="rate-details">
<p className="hint">
Köszönjük! Ha szeretnéd, mondd el bővebben is (nem kötelező):
</p>
<div className="rate-tags">
{offered.map((tag) => (
<button
key={tag}
type="button"
className={tags.includes(tag) ? "rate-tag is-on" : "rate-tag"}
aria-pressed={tags.includes(tag)}
onClick={() => toggle(tag)}
>
{TAG_LABELS[tag]}
</button>
))}
</div>
<label htmlFor={`${id}-comment`} className="sr-only">
Megjegyzés
</label>
<textarea
id={`${id}-comment`}
className="inp"
rows={2}
maxLength={MAX_COMMENT}
value={comment}
onChange={(e) => setComment(e.target.value)}
placeholder="Mi tetszett, mi nem? Mi hiányzott?"
/>
<label className="check">
<input
type="checkbox"
checked={mayRead}
onChange={(e) => setMayRead(e.target.checked)}
/>
Megnézhetitek{" "}
{thumbs ? "a kérdést és a választ" : "az elemzés szövegét"}, hogy
javíthassatok rajta.
</label>
<div className="form-actions">
<button
type="button"
className="btn btn-primary btn-sm"
disabled={busy}
onClick={() => save(stars, true)}
>
{busy ? "Mentés…" : "Elküldöm"}
</button>
<button
type="button"
className="btn btn-ghost btn-sm"
onClick={() => setOpen(false)}
>
Most nem
</button>
</div>
</div>
)}
<div aria-live="polite">
{error && <p className="msg msg-error">{error}</p>}
{saved && <p className="hint">Köszönjük a visszajelzést!</p>}
</div>
</div>
);
}