Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
48 lines
2.0 KiB
TypeScript
48 lines
2.0 KiB
TypeScript
import { useEffect } from "react";
|
||
import { navigate } from "../router";
|
||
import { useT } from "../i18n";
|
||
import { ChatView } from "./ChatView";
|
||
import { Icon } from "./Icon";
|
||
|
||
/** Desktop only: chats of the factory open over the board and are closed with one click. */
|
||
export const isDesktop = () => window.matchMedia("(min-width: 801px)").matches;
|
||
|
||
/** Open the chat of a phase: in the overlay on the desktop, as a page on small screens. */
|
||
export function openFactoryChat(sessionId: string) {
|
||
navigate(isDesktop() ? `/factory/s/${sessionId}` : `/s/${sessionId}`);
|
||
}
|
||
|
||
export function ChatOverlay({ id }: { id: string }) {
|
||
const t = useT();
|
||
const close = () => navigate("/factory");
|
||
useEffect(() => {
|
||
const onKey = (e: KeyboardEvent) => {
|
||
// Esc closes – unless it is meant for a field, a dialog or the image viewer on top.
|
||
const el = document.activeElement;
|
||
const typing = el instanceof HTMLTextAreaElement || el instanceof HTMLInputElement || el instanceof HTMLSelectElement;
|
||
if (e.key === "Escape" && !typing && !document.querySelector(".modal-backdrop, .viewer-overlay")) close();
|
||
};
|
||
window.addEventListener("keydown", onKey);
|
||
return () => window.removeEventListener("keydown", onKey);
|
||
}, []);
|
||
return (
|
||
<div className="chat-overlay" onMouseDown={(e) => e.target === e.currentTarget && close()}>
|
||
<div className="chat-overlay-box" role="dialog" aria-modal="true">
|
||
<div className="chat-overlay-bar">
|
||
<span className="hint">{t("factory.chatOverlay")}</span>
|
||
<span className="spacer" />
|
||
<button className="small" onClick={() => navigate(`/s/${id}`)} title={t("factory.openFullHint")}>
|
||
<Icon name="external" /> {t("factory.openFull")}
|
||
</button>
|
||
<button className="small" onClick={close} aria-label={t("common.close")}>
|
||
<Icon name="close" /> {t("common.close")}
|
||
</button>
|
||
</div>
|
||
<div className="chat-overlay-body">
|
||
<ChatView key={id} sessionId={id} onMenu={close} />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|