The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { PullRequestContextMetadata } from "@t3tools/contracts"; |
| 2 | import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger"; |
| 3 | import { Option, Schema } from "effect"; |
| 4 | |
| 5 | export const COMPOSER_MENTION_DRAG_TYPE = "application/x-t3code-composer-mention"; |
| 6 | |
| 7 | export const COMPOSER_PULL_REQUEST_DRAG_TYPE = "application/x-t3code-pull-request"; |
| 8 | |
| 9 | const PullRequestDragPayload = Schema.fromJsonString(PullRequestContextMetadata); |
| 10 | const decodePullRequestPayload = Schema.decodeUnknownOption(PullRequestDragPayload); |
| 11 | |
| 12 | export const encodePullRequestDragPayload = Schema.encodeSync(PullRequestDragPayload); |
| 13 | |
| 14 | export function composerMentionFromTreePath(treePath: string): string | null { |
| 15 | const relativePath = treePath.replace(/\/+$/, ""); |
| 16 | if (relativePath.length === 0) { |
| 17 | return null; |
| 18 | } |
| 19 | return serializeComposerFileLink(relativePath); |
| 20 | } |
| 21 | |
| 22 | export function dataTransferHasComposerMention(types: ReadonlyArray<string>): boolean { |
| 23 | return ( |
| 24 | types.includes(COMPOSER_MENTION_DRAG_TYPE) || types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE) |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | interface ComposerMentionDragTransfer { |
| 29 | readonly types: ReadonlyArray<string>; |
| 30 | getData(format: string): string; |
| 31 | dropEffect: string; |
| 32 | } |
| 33 | |
| 34 | interface ComposerMentionDragEvent { |
| 35 | readonly dataTransfer: ComposerMentionDragTransfer; |
| 36 | readonly nativeEvent: { stopPropagation(): void }; |
| 37 | preventDefault(): void; |
| 38 | stopPropagation(): void; |
| 39 | } |
| 40 | |
| 41 | /** Insertion focuses next frame; synchronous focus would overwrite the unreconciled mention. */ |
| 42 | export interface ComposerMentionDropHost { |
| 43 | insertMentionAtEnd(text: string): boolean; |
| 44 | insertPullRequestAtEnd(pullRequest: PullRequestContextMetadata): boolean; |
| 45 | setDragActive(active: boolean): void; |
| 46 | onInsertRejected(): void; |
| 47 | } |
| 48 | |
| 49 | interface ComposerMentionDragHandlers { |
| 50 | onDragEnter(event: ComposerMentionDragEvent): void; |
| 51 | onDragOver(event: ComposerMentionDragEvent): void; |
| 52 | onDrop(event: ComposerMentionDragEvent): void; |
| 53 | } |
| 54 | |
| 55 | export function makeComposerMentionDragHandlers( |
| 56 | host: ComposerMentionDropHost, |
| 57 | ): ComposerMentionDragHandlers { |
| 58 | // Stop native propagation so the editor cannot process claimed drags. |
| 59 | const claim = (event: ComposerMentionDragEvent): boolean => { |
| 60 | if (!dataTransferHasComposerMention(event.dataTransfer.types)) { |
| 61 | return false; |
| 62 | } |
| 63 | event.preventDefault(); |
| 64 | event.stopPropagation(); |
| 65 | event.nativeEvent.stopPropagation(); |
| 66 | return true; |
| 67 | }; |
| 68 | return { |
| 69 | onDragEnter(event) { |
| 70 | if (claim(event)) { |
| 71 | host.setDragActive(true); |
| 72 | } |
| 73 | }, |
| 74 | onDragOver(event) { |
| 75 | if (!claim(event)) { |
| 76 | return; |
| 77 | } |
| 78 | // Tree drags allow only "move"; other drop effects cancel delivery. |
| 79 | event.dataTransfer.dropEffect = "move"; |
| 80 | host.setDragActive(true); |
| 81 | }, |
| 82 | onDrop(event) { |
| 83 | if (!claim(event)) { |
| 84 | return; |
| 85 | } |
| 86 | host.setDragActive(false); |
| 87 | if (event.dataTransfer.types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)) { |
| 88 | const pullRequest = decodePullRequestPayload( |
| 89 | event.dataTransfer.getData(COMPOSER_PULL_REQUEST_DRAG_TYPE), |
| 90 | ); |
| 91 | if (Option.isSome(pullRequest) && !host.insertPullRequestAtEnd(pullRequest.value)) { |
| 92 | host.onInsertRejected(); |
| 93 | } |
| 94 | return; |
| 95 | } |
| 96 | const mention = event.dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE); |
| 97 | if (mention.length === 0) { |
| 98 | return; |
| 99 | } |
| 100 | if (!host.insertMentionAtEnd(`${mention} `)) { |
| 101 | host.onInsertRejected(); |
| 102 | } |
| 103 | }, |
| 104 | }; |
| 105 | } |