fi.evolver.ai.vaadin.component.ChatAvatarItem Maven / Gradle / Ivy
package fi.evolver.ai.vaadin.component;
import java.io.Serial;
import java.time.LocalDateTime;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.HasSize;
import com.vaadin.flow.component.avatar.Avatar;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import fi.evolver.utils.DateUtils;
public class ChatAvatarItem extends Composite implements HasSize {
@Serial
private static final long serialVersionUID = 1L;
public ChatAvatarItem(LocalDateTime sendTime, String user, Component content) {
getContent().setAlignItems(FlexComponent.Alignment.START);
VerticalLayout column = new VerticalLayout(
generateHeader(sendTime, user),
content);
column.setPadding(false);
column.setSpacing(false);
getContent().add(column);
getContent().getStyle().set("line-height", "var(--lumo-line-height-m)");
getContent().addComponentAsFirst(new Avatar(user));
}
public ChatAvatarItem(String user, Component content) {
this(LocalDateTime.now(), user, content);
}
private static Div generateHeader(LocalDateTime sendTime, String user) {
Div result = new Div();
Span userPart = new Span(user);
userPart.getStyle().set("margin-right", "var(--lumo-space-s)");
Span dateTimePart = new Span(sendTime.format(DateUtils.FORMAT_DATE_TIME_FI));
dateTimePart.getStyle()
.set("color", "var(--lumo-secondary-text-color)")
.set("font-size", "var(--lumo-font-size-s)");
result.add(userPart, dateTimePart);
return result;
}
}