mapcomplete/UI/Reviews/ReviewElement.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-12-08 23:44:34 +01:00
import { UIEventSource } from "../../Logic/UIEventSource"
import { Review } from "../../Logic/Web/Review"
import Combine from "../Base/Combine"
import Translations from "../i18n/Translations"
2020-12-11 16:29:51 +01:00
import SingleReview from "./SingleReview"
2021-06-11 22:51:45 +02:00
import BaseUIElement from "../BaseUIElement"
import Img from "../Base/Img"
import { VariableUiElement } from "../Base/VariableUIElement"
import Link from "../Base/Link"
2020-12-08 23:44:34 +01:00
/**
* Shows the reviews and scoring base on mangrove.reviews
* The middle element is some other component shown in the middle, e.g. the review input element
*/
export default class ReviewElement extends VariableUiElement {
2021-06-11 22:51:45 +02:00
constructor(subject: string, reviews: UIEventSource<Review[]>, middleElement: BaseUIElement) {
super(
reviews.map((revs) => {
const elements = []
revs.sort((a, b) => b.date.getTime() - a.date.getTime()) // Sort with most recent first
const avg =
revs.map((review) => review.rating).reduce((a, b) => a + b, 0) / revs.length
elements.push(
new Combine([
SingleReview.GenStars(avg),
new Link(
revs.length === 1
? Translations.t.reviews.title_singular.Clone()
: Translations.t.reviews.title.Subs({ count: "" + revs.length }),
`https://mangrove.reviews/search?sub=${encodeURIComponent(subject)}`,
true
),
]).SetClass("font-2xl flex justify-between items-center pl-2 pr-2")
2022-09-08 21:40:48 +02:00
)
elements.push(middleElement)
elements.push(...revs.map((review) => new SingleReview(review)))
elements.push(
new Combine([
Translations.t.reviews.attribution.Clone(),
new Img("./assets/mangrove_logo.png"),
]).SetClass("review-attribution")
2022-09-08 21:40:48 +02:00
)
return new Combine(elements).SetClass("block")
})
)
2020-12-07 03:02:50 +01:00
}
}