Skip to content

Commit 95da1fd

Browse files
committed
refactor(ui): centralize composer attachment and capability controls
1 parent 85fd4c9 commit 95da1fd

3 files changed

Lines changed: 164 additions & 265 deletions

File tree

‎ui/src/pages/chat/components/chat-attachments.ts‎

Lines changed: 74 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class ChatAttachmentReadLifecycle {
6363
}
6464
}
6565

66-
export function isFileDrag(dataTransfer: DataTransfer | null): boolean {
66+
function isFileDrag(dataTransfer: DataTransfer | null): boolean {
6767
return Array.from(dataTransfer?.types ?? []).includes("Files");
6868
}
6969

@@ -81,7 +81,7 @@ const TEXT_ENTRY_INPUT_TYPES = new Set([
8181
// actually accept it; anywhere else (disabled/readonly inputs, non-text
8282
// controls like checkbox/range) an uncancelled URL drop navigates the app
8383
// away and discards unsent drafts.
84-
export function isEditableDropTarget(event: DragEvent): boolean {
84+
function isEditableDropTarget(event: DragEvent): boolean {
8585
const target = event.target;
8686
if (!(target instanceof Element)) {
8787
return false;
@@ -100,7 +100,7 @@ function currentAttachments(props: ChatAttachmentControlsProps): ChatAttachment[
100100
return props.getAttachments?.() ?? props.attachments ?? [];
101101
}
102102

103-
export function clickComposerInput(target: HTMLElement, selector: string) {
103+
function clickComposerInput(target: HTMLElement, selector: string) {
104104
target.closest("details")?.removeAttribute("open");
105105
target
106106
.closest(".agent-chat__composer-shell, .new-session-page__composer")
@@ -363,7 +363,7 @@ function handleChatAttachmentFileSelect(e: Event, props: ChatAttachmentControlsP
363363
void appendAttachmentFiles(files, props);
364364
}
365365

366-
export function handleChatAttachmentDrop(e: DragEvent, props: ChatAttachmentControlsProps) {
366+
function handleChatAttachmentDrop(e: DragEvent, props: ChatAttachmentControlsProps) {
367367
e.preventDefault();
368368
void appendAttachmentFiles([...(e.dataTransfer?.files ?? [])], props);
369369
}
@@ -372,6 +372,8 @@ type ChatAttachmentDropProps = ChatAttachmentControlsProps & {
372372
canCompose: boolean;
373373
};
374374

375+
// Both composers share balanced nested drag state and cancel non-editable
376+
// text/URL drops so disabled surfaces cannot navigate away from a draft.
375377
export function createChatAttachmentDropHandlers(props: ChatAttachmentDropProps) {
376378
let depth = 0;
377379
const setActive = (event: DragEvent, active: boolean) => {
@@ -432,42 +434,74 @@ export function createChatAttachmentDropHandlers(props: ChatAttachmentDropProps)
432434

433435
export function renderChatAttachmentInputs(props: ChatAttachmentControlsProps) {
434436
return html`
435-
<input
436-
type="file"
437-
accept=${CHAT_ATTACHMENT_ACCEPT}
438-
multiple
439-
class="agent-chat__file-input"
440-
?disabled=${props.disabled}
441-
@change=${(event: Event) => {
442-
if (!props.disabled) {
443-
handleChatAttachmentFileSelect(event, props);
444-
}
445-
}}
446-
/>
447-
<input
448-
type="file"
449-
accept="image/*"
450-
multiple
451-
class="agent-chat__photo-input"
452-
?disabled=${props.disabled}
453-
@change=${(event: Event) => {
454-
if (!props.disabled) {
455-
handleChatAttachmentFileSelect(event, props);
456-
}
457-
}}
458-
/>
459-
<input
460-
type="file"
461-
accept="image/*"
462-
capture="environment"
463-
class="agent-chat__camera-input"
464-
?disabled=${props.disabled}
465-
@change=${(event: Event) => {
466-
if (!props.disabled) {
467-
handleChatAttachmentFileSelect(event, props);
437+
${(["file", "photo", "camera"] as const).map(
438+
(kind) => html`
439+
<input
440+
type="file"
441+
accept=${kind === "file" ? CHAT_ATTACHMENT_ACCEPT : "image/*"}
442+
?multiple=${kind !== "camera"}
443+
capture=${kind === "camera" ? "environment" : nothing}
444+
class=${`agent-chat__${kind}-input`}
445+
?disabled=${props.disabled}
446+
@change=${(event: Event) => {
447+
if (!props.disabled) {
448+
handleChatAttachmentFileSelect(event, props);
449+
}
450+
}}
451+
/>
452+
`,
453+
)}
454+
`;
455+
}
456+
457+
export function handleChatAttachmentMenuSelection(
458+
event: CustomEvent<{ item: { value?: string } }>,
459+
): boolean {
460+
const value = event.detail.item.value;
461+
if (value !== "camera" && value !== "photo" && value !== "file") {
462+
return false;
463+
}
464+
clickComposerInput(event.currentTarget as HTMLElement, `.agent-chat__${value}-input`);
465+
return true;
466+
}
467+
468+
export function renderChatAttachmentMenuTrigger(disabled: boolean | undefined) {
469+
return html`
470+
<button
471+
slot="trigger"
472+
type="button"
473+
class="agent-chat__input-btn agent-chat__input-btn--attach"
474+
aria-label=${t("chat.composer.addAttachment")}
475+
?disabled=${disabled}
476+
title=${t("chat.composer.addAttachment")}
477+
@pointerdown=${(event: PointerEvent) => {
478+
const composer = (event.currentTarget as HTMLElement)
479+
.closest(".agent-chat__composer-shell")
480+
?.querySelector("textarea");
481+
if (document.activeElement === composer) {
482+
event.preventDefault();
468483
}
469484
}}
470-
/>
485+
>
486+
${icons.plus}
487+
</button>
488+
`;
489+
}
490+
491+
export function renderChatAttachmentMenuOptions(fileIcon = icons.folder) {
492+
return html`
493+
<wa-dropdown-item class="agent-chat__attach-menu-option" value="camera">
494+
<span slot="icon" aria-hidden="true">${icons.camera}</span>
495+
<span>${t("chat.composer.takePhoto")}</span>
496+
</wa-dropdown-item>
497+
<wa-dropdown-item class="agent-chat__attach-menu-option" value="photo">
498+
<span slot="icon" aria-hidden="true">${icons.image}</span>
499+
<span>${t("chat.composer.attachPhoto")}</span>
500+
</wa-dropdown-item>
501+
<wa-dropdown-item class="agent-chat__attach-menu-option" value="file">
502+
<span slot="icon" aria-hidden="true">${fileIcon}</span>
503+
<span>${t("chat.composer.attachFileOption")}</span>
504+
</wa-dropdown-item>
471505
`;
472506
}
473507

@@ -477,51 +511,9 @@ export function renderChatAttachmentMenu(props: ChatAttachmentControlsProps) {
477511
class="agent-chat__attach-menu"
478512
placement="top-start"
479513
aria-label=${t("chat.composer.addAttachment")}
480-
@wa-select=${(event: CustomEvent<{ item: { value?: string } }>) => {
481-
const menu = event.currentTarget as HTMLElement;
482-
const selector =
483-
event.detail.item.value === "camera"
484-
? ".agent-chat__camera-input"
485-
: event.detail.item.value === "photo"
486-
? ".agent-chat__photo-input"
487-
: event.detail.item.value === "file"
488-
? ".agent-chat__file-input"
489-
: null;
490-
if (selector) {
491-
clickComposerInput(menu, selector);
492-
}
493-
}}
514+
@wa-select=${handleChatAttachmentMenuSelection}
494515
>
495-
<button
496-
slot="trigger"
497-
type="button"
498-
class="agent-chat__input-btn agent-chat__input-btn--attach"
499-
aria-label=${t("chat.composer.addAttachment")}
500-
?disabled=${props.disabled}
501-
title=${t("chat.composer.addAttachment")}
502-
@pointerdown=${(event: PointerEvent) => {
503-
const composer = (event.currentTarget as HTMLElement)
504-
.closest(".agent-chat__composer-shell")
505-
?.querySelector("textarea");
506-
if (document.activeElement === composer) {
507-
event.preventDefault();
508-
}
509-
}}
510-
>
511-
${icons.plus}
512-
</button>
513-
<wa-dropdown-item class="agent-chat__attach-menu-option" value="camera">
514-
<span slot="icon" aria-hidden="true">${icons.camera}</span>
515-
<span>${t("chat.composer.takePhoto")}</span>
516-
</wa-dropdown-item>
517-
<wa-dropdown-item class="agent-chat__attach-menu-option" value="photo">
518-
<span slot="icon" aria-hidden="true">${icons.image}</span>
519-
<span>${t("chat.composer.attachPhoto")}</span>
520-
</wa-dropdown-item>
521-
<wa-dropdown-item class="agent-chat__attach-menu-option" value="file">
522-
<span slot="icon" aria-hidden="true">${icons.folder}</span>
523-
<span>${t("chat.composer.attachFileOption")}</span>
524-
</wa-dropdown-item>
516+
${renderChatAttachmentMenuTrigger(props.disabled)} ${renderChatAttachmentMenuOptions()}
525517
</wa-dropdown>
526518
`;
527519
}

0 commit comments

Comments
 (0)