fi.evolver.ai.vaadin.view.ChatHistoryAwareView Maven / Gradle / Ivy
package fi.evolver.ai.vaadin.view;
import java.io.Serial;
import java.util.List;
import java.util.Optional;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.Location;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.theme.lumo.LumoUtility;
import jakarta.annotation.PostConstruct;
public abstract class ChatHistoryAwareView extends VerticalLayout implements BeforeEnterObserver {
@Serial
private static final long serialVersionUID = 1L;
@PostConstruct
public void init() {
addClassName(LumoUtility.Overflow.HIDDEN);
if (isFullSize())
setSizeFull();
getPreChatComponent().ifPresent(this::add);
add((Component)(getChatComponent()));
}
@Override
public void beforeEnter(BeforeEnterEvent event) {
startChatIfExists(event);
}
public void startChatIfExists(BeforeEvent event) {
Location location = event.getLocation();
QueryParameters queryParameters = location.getQueryParameters();
List chatIdParams = queryParameters.getParameters().get("chatId");
if (chatIdParams != null && !chatIdParams.isEmpty())
getChatComponent().startChatWithHistory(chatIdParams.get(0));
}
public Optional getPreChatComponent() {
return Optional.empty();
}
public abstract HistoryAwareChat getChatComponent();
public boolean isFullSize() {
return true;
}
}