diff --git a/libs/react/README.md b/libs/react/README.md index 3fd270b7d..c3057dbe3 100644 --- a/libs/react/README.md +++ b/libs/react/README.md @@ -927,7 +927,10 @@ Report the failure from your command; the list does not show an error itself. - **Pin** or **Unpin** is offered according to the row's `pinned` value. With `reorderPinned`, pinned rows also get **Move up** and **Move down**; the command receives the ID of the pinned row to place it before, or `null` for - last. The list keeps your order and does not sort pinned rows itself. + last. Pinned rows can also be dragged with a mouse onto another pinned row: + a line shows whether the row lands before or after it, and the drop calls + the same command. Keyboard and touch users keep **Move up** and **Move + down**. The list keeps your order and does not sort pinned rows itself. - **Move to project** is offered when you also pass `projects`. The open menu swaps to **No project** and your projects, and `moveToProject` receives the chosen project ID or `null`. The row stays in place until you update `threads`. diff --git a/libs/react/src/chat/styles.css b/libs/react/src/chat/styles.css index da5ae7e01..00d404999 100644 --- a/libs/react/src/chat/styles.css +++ b/libs/react/src/chat/styles.css @@ -368,6 +368,29 @@ .tp-thread-list__item { min-width: 0; } +.tp-thread-list__item[draggable='true'] { + position: relative; +} +.tp-thread-list__item[data-dragging] { + opacity: 0.4; +} +/* Where a dragged pinned row would land. */ +.tp-thread-list__item[data-drop-position='before']::before, +.tp-thread-list__item[data-drop-position='after']::after { + content: ''; + position: absolute; + inset-inline: 4px; + block-size: 2px; + border-radius: 1px; + background: var(--tp-chat-accent, var(--ds-accent, Highlight)); + pointer-events: none; +} +.tp-thread-list__item[data-drop-position='before']::before { + inset-block-start: calc(-0.25rem - 1px); +} +.tp-thread-list__item[data-drop-position='after']::after { + inset-block-end: calc(-0.25rem - 1px); +} .tp-thread-list__item--actions { display: flex; align-items: center; diff --git a/libs/react/src/chat/thread-list-drag.spec.tsx b/libs/react/src/chat/thread-list-drag.spec.tsx new file mode 100644 index 000000000..42c70c92d --- /dev/null +++ b/libs/react/src/chat/thread-list-drag.spec.tsx @@ -0,0 +1,139 @@ +import { act, cleanup, createEvent, fireEvent, render } from '@testing-library/react'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { ThreadList, type ThreadActionAdapter, type ThreadListItem } from './index'; + +afterEach(() => { + cleanup(); + vi.restoreAllMocks(); +}); + +const threads: readonly ThreadListItem[] = [ + { id: 'a', title: 'Alpha', pinned: true }, + { id: 'b', title: 'Beta', pinned: true }, + { id: 'c', title: 'Gamma', pinned: true }, + { id: 'd', title: 'Delta' }, +]; +const list = (actions?: ThreadActionAdapter) => + render( undefined} actions={actions} />); +const row = (view: ReturnType, id: string) => + view.container.querySelector(`[data-row-id="${id}"]`)!; +const order = (view: ReturnType) => + Array.from(view.container.querySelectorAll('[data-row-id]')).map( + (item) => item.dataset['rowId'] + ); +const transfer = () => { + const data = new Map(); + return { + setData: (type: string, value: string) => void data.set(type, value), + getData: (type: string) => data.get(type) ?? '', + effectAllowed: 'none', + dropEffect: 'none', + }; +}; +/** Rows are 40px tall; a pointer at `y` within the row picks its half. */ +const over = (target: HTMLElement, y: number, dataTransfer: ReturnType) => { + vi.spyOn(target, 'getBoundingClientRect').mockReturnValue({ + top: 0, left: 0, right: 200, bottom: 40, width: 200, height: 40, x: 0, y: 0, toJSON: () => ({}), + }); + const event = createEvent.dragOver(target, { dataTransfer }); + Object.defineProperty(event, 'clientY', { value: y }); + fireEvent(target, event); + return event; +}; + +describe('ThreadList pinned drag reorder', () => { + it('makes nothing draggable without a reorder command', () => { + const view = list({ pin: vi.fn() }); + expect(view.container.querySelector('[draggable]')).toBeNull(); + }); + + it('makes only pinned rows draggable', () => { + const view = list({ reorderPinned: vi.fn() }); + expect(row(view, 'a').getAttribute('draggable')).toBe('true'); + expect(row(view, 'c').getAttribute('draggable')).toBe('true'); + expect(row(view, 'd').hasAttribute('draggable')).toBe(false); + }); + + it('marks the dragged row and shows where it would land', () => { + const view = list({ reorderPinned: vi.fn() }); + const data = transfer(); + fireEvent.dragStart(row(view, 'a'), { dataTransfer: data }); + expect(data.getData('text/plain')).toBe('a'); + expect(data.effectAllowed).toBe('move'); + expect(row(view, 'a').hasAttribute('data-dragging')).toBe(true); + const accepted = over(row(view, 'c'), 5, data); + expect(accepted.defaultPrevented).toBe(true); + expect(row(view, 'c').getAttribute('data-drop-position')).toBe('before'); + over(row(view, 'c'), 35, data); + expect(row(view, 'c').getAttribute('data-drop-position')).toBe('after'); + fireEvent.dragLeave(row(view, 'c')); + expect(row(view, 'c').hasAttribute('data-drop-position')).toBe(false); + }); + + it('drops before a row: reorders at once with that row as the anchor', async () => { + const reorderPinned = vi.fn(async () => undefined); + const view = list({ reorderPinned }); + const data = transfer(); + fireEvent.dragStart(row(view, 'c'), { dataTransfer: data }); + over(row(view, 'a'), 5, data); + fireEvent.drop(row(view, 'a'), { dataTransfer: data }); + expect(reorderPinned.mock.calls).toEqual([['c', 'a']]); + expect(order(view)).toEqual(['c', 'a', 'b', 'd']); + expect(view.container.querySelector('[data-dragging], [data-drop-position]')).toBeNull(); + await act(async () => undefined); + }); + + it('drops after a row: anchors on the next pinned row, or last', async () => { + const reorderPinned = vi.fn(async () => undefined); + const view = list({ reorderPinned }); + const data = transfer(); + fireEvent.dragStart(row(view, 'a'), { dataTransfer: data }); + over(row(view, 'b'), 35, data); + fireEvent.drop(row(view, 'b'), { dataTransfer: data }); + expect(reorderPinned.mock.calls[0]).toEqual(['a', 'c']); + await act(async () => undefined); + fireEvent.dragStart(row(view, 'b'), { dataTransfer: data }); + over(row(view, 'c'), 35, data); + fireEvent.drop(row(view, 'c'), { dataTransfer: data }); + expect(reorderPinned.mock.calls[1]).toEqual(['b', null]); + await act(async () => undefined); + }); + + it('refuses a drop on itself, on an unpinned row, and from outside the list', () => { + const reorderPinned = vi.fn(async () => undefined); + const view = list({ reorderPinned }); + const data = transfer(); + // Nothing from this list is being dragged. + expect(over(row(view, 'a'), 5, data).defaultPrevented).toBe(false); + fireEvent.drop(row(view, 'a'), { dataTransfer: data }); + fireEvent.dragStart(row(view, 'a'), { dataTransfer: data }); + expect(over(row(view, 'a'), 5, data).defaultPrevented).toBe(false); + expect(over(row(view, 'd'), 5, data).defaultPrevented).toBe(false); + fireEvent.drop(row(view, 'd'), { dataTransfer: data }); + expect(reorderPinned).not.toHaveBeenCalled(); + }); + + it('clears the drag state when the drag ends without a drop', () => { + const view = list({ reorderPinned: vi.fn() }); + const data = transfer(); + fireEvent.dragStart(row(view, 'a'), { dataTransfer: data }); + over(row(view, 'b'), 5, data); + fireEvent.dragEnd(row(view, 'a')); + expect(view.container.querySelector('[data-dragging], [data-drop-position]')).toBeNull(); + }); + + it('puts the rows back when the command rejects', async () => { + const reorderPinned = vi.fn(async () => { + throw new Error('no'); + }); + const view = list({ reorderPinned }); + const data = transfer(); + fireEvent.dragStart(row(view, 'c'), { dataTransfer: data }); + over(row(view, 'a'), 5, data); + fireEvent.drop(row(view, 'a'), { dataTransfer: data }); + expect(order(view)).toEqual(['c', 'a', 'b', 'd']); + await act(async () => undefined); + await act(async () => undefined); + expect(order(view)).toEqual(['a', 'b', 'c', 'd']); + }); +}); diff --git a/libs/react/src/chat/thread-list.tsx b/libs/react/src/chat/thread-list.tsx index 8239fd17b..198f5e284 100644 --- a/libs/react/src/chat/thread-list.tsx +++ b/libs/react/src/chat/thread-list.tsx @@ -152,6 +152,12 @@ export function ThreadList({ readonly returnTo: HTMLElement | null; } | null>(null); const [order, setOrder] = useState(NO_ORDER); + // A pinned row being dragged, and where it would land. + const [dragging, setDragging] = useState(null); + const [dropTarget, setDropTarget] = useState<{ + readonly id: string; + readonly position: 'before' | 'after'; + } | null>(null); const reference = validDate(now); let visible: readonly ThreadListItem[] = threads; @@ -332,6 +338,7 @@ export function ThreadList({ ); } const pinned = thread.pinned === true; + const canDrag = adapter?.reorderPinned !== undefined; const title = thread.title || thread.id; const content = renderTitle ? renderTitle(thread) : title; const ownerActions = renderActions?.(thread); @@ -404,6 +411,92 @@ export function ThreadList({ className={`tp-thread-list__item${ hasActions ? ' tp-thread-list__item--actions' : '' }`} + draggable={canDrag && pinned ? true : undefined} + data-dragging={dragging === thread.id ? '' : undefined} + data-drop-position={ + dropTarget?.id === thread.id ? dropTarget.position : undefined + } + onDragStart={ + canDrag && pinned + ? (event) => { + event.dataTransfer.setData('text/plain', thread.id); + event.dataTransfer.effectAllowed = 'move'; + setDragging(thread.id); + } + : undefined + } + onDragOver={ + canDrag && pinned + ? (event) => { + // Only a pinned row from this list can land here. + if (dragging === null || dragging === thread.id) return; + event.preventDefault(); + event.dataTransfer.dropEffect = 'move'; + const rect = event.currentTarget.getBoundingClientRect(); + const position = + event.clientY - rect.top < rect.height / 2 + ? 'before' + : 'after'; + setDropTarget((current) => + current?.id === thread.id && + current.position === position + ? current + : { id: thread.id, position } + ); + } + : undefined + } + onDragLeave={ + canDrag && pinned + ? (event) => { + // Moving between the row's own children is not leaving it. + if ( + event.relatedTarget instanceof Node && + event.currentTarget.contains(event.relatedTarget) + ) + return; + setDropTarget((current) => + current?.id === thread.id ? null : current + ); + } + : undefined + } + onDrop={ + canDrag && pinned + ? (event) => { + event.preventDefault(); + const moved = dragging; + const target = dropTarget; + setDragging(null); + setDropTarget(null); + if ( + moved === null || + moved === thread.id || + target?.id !== thread.id + ) + return; + const others = visible.filter( + (row) => row.pinned === true && row.id !== moved + ); + const at = others.findIndex((row) => row.id === thread.id); + if (at < 0) return; + reorder( + moved, + target.position === 'before' + ? thread.id + : others[at + 1]?.id ?? null + ); + } + : undefined + } + onDragEnd={ + canDrag && pinned + ? () => { + setDragging(null); + setDropTarget(null); + } + : undefined + } >