mapcomplete/UI/BigComponents/UserBadge.ts

141 lines
4.9 KiB
TypeScript
Raw Normal View History

import {VariableUiElement} from "../Base/VariableUIElement";
import Svg from "../../Svg";
import State from "../../State";
import Combine from "../Base/Combine";
import {FixedUiElement} from "../Base/FixedUiElement";
import LanguagePicker from "../LanguagePicker";
import Translations from "../i18n/Translations";
import Link from "../Base/Link";
2021-06-12 02:58:32 +02:00
import Toggle from "../Input/Toggle";
import Img from "../Base/Img";
2021-06-12 02:58:32 +02:00
export default class UserBadge extends Toggle {
2020-06-24 00:35:19 +02:00
constructor() {
2020-11-05 12:28:02 +01:00
2021-06-12 02:58:32 +02:00
const userDetails = State.state.osmConnection.userDetails;
const loginButton = Translations.t.general.loginWithOpenStreetMap
.Clone()
.SetClass("userbadge-login pt-3 w-full h-full")
.onClick(() => State.state.osmConnection.AttemptLogin());
2021-06-12 02:58:32 +02:00
const logout =
Svg.logout_svg()
2020-11-05 12:28:02 +01:00
.onClick(() => {
State.state.osmConnection.LogOut();
});
2020-06-24 00:35:19 +02:00
2020-07-01 17:51:55 +02:00
const userBadge = new VariableUiElement(userDetails.map(user => {
2021-06-12 02:58:32 +02:00
{
const homeButton = new VariableUiElement(
userDetails.map((userinfo) => {
if (userinfo.home) {
return Svg.home_ui();
}
return " ";
})
).onClick(() => {
const home = State.state.osmConnection.userDetails.data?.home;
if (home === undefined) {
return;
}
State.state.leafletMap.data.setView([home.lat, home.lon], 16);
});
const linkStyle = "flex items-baseline"
const languagePicker = (LanguagePicker.CreateLanguagePicker(State.state.layoutToUse.data.language) ?? new FixedUiElement(""))
.SetStyle("width:min-content;");
2020-06-24 00:35:19 +02:00
2021-06-12 02:58:32 +02:00
let messageSpan =
new Link(
new Combine([Svg.envelope, "" + user.totalMessages]).SetClass(linkStyle),
`${user.backend}/messages/inbox`,
2021-06-12 02:58:32 +02:00
true
)
const csCount =
new Link(
new Combine([Svg.star, "" + user.csCount]).SetClass(linkStyle),
`${user.backend}/user/${user.name}/history`,
2021-06-12 02:58:32 +02:00
true);
if (user.unreadMessages > 0) {
messageSpan = new Link(
new Combine([Svg.envelope, "" + user.unreadMessages]),
2021-07-08 14:55:21 +02:00
`${user.backend}/messages/inbox`,
2021-06-12 02:58:32 +02:00
true
).SetClass("alert")
2020-07-20 09:57:19 +02:00
}
2021-06-12 02:58:32 +02:00
let dryrun = new FixedUiElement("");
if (user.dryRun) {
dryrun = new FixedUiElement("TESTING").SetClass("alert font-xs p-0 max-h-4");
2021-06-12 02:58:32 +02:00
}
const settings =
new Link(Svg.gear_svg(),
`${user.backend}/user/${encodeURIComponent(user.name)}/account`,
2021-06-12 02:58:32 +02:00
true)
const userName = new Link(
new FixedUiElement(user.name),
`${user.backend}/user/${user.name}`,
2021-06-12 02:58:32 +02:00
true);
const userStats = new Combine([
homeButton,
settings,
messageSpan,
csCount,
languagePicker,
logout
])
.SetClass("userstats")
const usertext = new Combine([
new Combine([userName, dryrun]).SetClass("flex justify-end w-full"),
2021-06-12 02:58:32 +02:00
userStats
]).SetClass("flex flex-col sm:w-auto sm:pl-2 overflow-hidden w-0")
const userIcon =
(user.img === undefined ? Svg.osm_logo_ui() : new Img(user.img)).SetClass("rounded-full opacity-0 m-0 p-0 duration-500 w-16 min-width-16 h16 float-left")
.onClick(() => {
if (usertext.HasClass("w-0")) {
usertext.RemoveClass("w-0")
usertext.SetClass("w-min pl-2")
} else {
usertext.RemoveClass("w-min")
usertext.RemoveClass("pl-2")
usertext.SetClass("w-0")
}
})
2021-06-12 02:58:32 +02:00
return new Combine([
usertext,
userIcon,
]).SetClass("h-16 flex bg-white")
2020-07-20 09:57:19 +02:00
}
}));
2020-07-20 09:57:19 +02:00
userBadge.SetClass("inline-block m-0 w-full").SetStyle("pointer-events: all")
2021-06-12 02:58:32 +02:00
super(
userBadge,
2021-06-12 02:58:32 +02:00
loginButton,
State.state.osmConnection.isLoggedIn
)
2020-07-23 16:00:49 +02:00
this.SetClass("shadow rounded-full h-min overflow-hidden block w-max")
2020-06-24 00:35:19 +02:00
}
}