fi.evolver.ai.vaadin.component.BaseChatComponent Maven / Gradle / Ivy
package fi.evolver.ai.vaadin.component;
import java.io.Serial;
import org.vaadin.lineawesome.LineAwesomeIcon;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.DetachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.contextmenu.MenuItem;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.menubar.MenuBarVariant;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.page.Page;
import com.vaadin.flow.dom.Style.Position;
import com.vaadin.flow.shared.Registration;
import fi.evolver.ai.vaadin.component.form.StarRatingComponent;
abstract class BaseChatComponent extends VerticalLayout {
private static final int BREAKPOINT_PX = 800;
@Serial
private static final long serialVersionUID = 1L;
protected final StarRatingComponent chatRating = new StarRatingComponent(this::getTranslation);
protected final MenuBar actionMenu = new MenuBar();
private final MenuItem startNewChat = actionMenu.addItem(LineAwesomeIcon.EDIT.create(), getTranslation("common.newChat"));
private MenuItem chatRatingMenuItem = actionMenu.addItem(chatRating, getTranslation("common.rateChat"));
private boolean isMobile = false;
protected final Class viewClass;
private Registration listener;
public BaseChatComponent(Class viewClass) {
this.viewClass = viewClass;
setupStartNewChat();
setupChatRating();
setupActionMenu();
}
public abstract void reset();
protected abstract void onRatingChange(Integer newValue);
public MenuBar getMenuBar() {
return actionMenu;
}
@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
Page page = attachEvent.getUI().getPage();
listener = page.addBrowserWindowResizeListener(event -> setInputWidths(event.getWidth()));
page.retrieveExtendedClientDetails(receiver -> setInputWidths(receiver.getBodyClientWidth()));
}
@Override
protected void onDetach(DetachEvent detachEvent) {
listener.remove();
super.onDetach(detachEvent);
}
private void setInputWidths(int width) {
if (isMobile == width <= BREAKPOINT_PX)
return;
isMobile = width <= BREAKPOINT_PX;
if (isMobile) {
chatRatingMenuItem.removeAll();
actionMenu.remove(chatRatingMenuItem);
chatRatingMenuItem = actionMenu.addItem(LineAwesomeIcon.STAR_HALF_ALT_SOLID.create(), getTranslation("common.rateChat"));
MenuItem ratingSubMenuItem = chatRatingMenuItem.getSubMenu().addItem(chatRating);
ratingSubMenuItem.getElement().addEventListener("click", (e) -> {}).addEventData("event.preventDefault()");
}
else {
chatRatingMenuItem.removeAll();
actionMenu.remove(chatRatingMenuItem);
chatRatingMenuItem = actionMenu.addItem(chatRating, getTranslation("common.rateChat"));
chatRatingMenuItem.getElement().addEventListener("click", (e) -> {}).addEventData("event.preventDefault()");
}
}
protected void setupStartNewChat() {
startNewChat.setAriaLabel("common.newChat");
startNewChat.addClickListener(event -> {
UI.getCurrent().navigate(viewClass);
reset();
});
actionMenu.setEnabled(false);
}
protected void setupChatRating() {
chatRating.addValueChangeListener(this::onRatingChange);
}
protected void setupActionMenu() {
actionMenu.addThemeVariants(MenuBarVariant.LUMO_ICON);
actionMenu.getStyle()
.setPosition(Position.ABSOLUTE)
.setPaddingRight("5px")
.setZIndex(1)
.setFontSize("20px")
.setRight("10px")
.setTop("5px");
add(actionMenu);
}
}