diff --git a/apps/desktop/src/lib/api.ts b/apps/desktop/src/lib/api.ts index f092f9b..62962ac 100644 --- a/apps/desktop/src/lib/api.ts +++ b/apps/desktop/src/lib/api.ts @@ -440,11 +440,16 @@ export const api = { id: string, body: string, author?: string, - project?: string + project?: string, + /** Media staged with `stageMedia`: it belongs to the comment. */ + attachments?: Attachment[] ): Promise<{ ok: boolean; comment: string }> { return req<{ ok: boolean; comment: string }>( `/api/tickets/${encodeURIComponent(id)}/comments${qs({ project })}`, - { method: 'POST', body: JSON.stringify({ body, author }) } + { + method: 'POST', + body: JSON.stringify({ body, author, attachments: attachments?.length ? attachments : undefined }) + } ); }, diff --git a/apps/desktop/src/lib/components/TicketModal.svelte b/apps/desktop/src/lib/components/TicketModal.svelte index 8a7de54..9fcd367 100644 --- a/apps/desktop/src/lib/components/TicketModal.svelte +++ b/apps/desktop/src/lib/components/TicketModal.svelte @@ -2,7 +2,16 @@ import { fade, scale, fly } from 'svelte/transition'; import { browser } from '$app/environment'; import { get } from 'svelte/store'; - import { X, Pencil, Send, Flag, Check, CheckSquare, ShieldCheck } from 'lucide-svelte'; + import { + X, + Pencil, + Send, + Flag, + Check, + CheckSquare, + ShieldCheck, + Paperclip + } from 'lucide-svelte'; import Chip from './ui/Chip.svelte'; import Avatar from './Avatar.svelte'; import Markdown from './Markdown.svelte'; @@ -205,17 +214,57 @@ // --- comments --- let commentDraft = $state(''); + // Media pasted into the comment box belongs to that comment (the screenshot + // it talks about): it is staged in the board's media store right away, shown + // under the box, and sent with the comment - like a forum post's media. + let commentMedia = $state([]); + let commentUploading = $state(0); + async function stageForComment(files: File[]) { + saveError = null; + for (const f of files) { + commentUploading++; + try { + const a = await api.stageMedia(f, proj()); + if (!commentMedia.some((m) => m.path === a.path)) commentMedia = [...commentMedia, a]; + } catch (e) { + saveError = e instanceof Error ? e.message : String(e); + } finally { + commentUploading--; + } + } + } + function onPasteComment(e: ClipboardEvent) { + if (!ticket || readOnly) return; + const files = filesFromClipboard(e.clipboardData); + if (files.length) { + e.preventDefault(); + void stageForComment(files); + return; + } + // (a pasted local path still attaches that file to the ticket) + void onPasteAttach(e, 'comment'); + } + $effect(() => { + void ticketId; + commentMedia = []; + }); async function addComment() { const b = commentDraft.trim(); - if (!ticket || !b) return; + const media = commentMedia; + // (an image alone is a comment too; wait for a paste still uploading) + if (!ticket || (!b && !media.length) || commentUploading) return; commentDraft = ''; + commentMedia = []; saveError = null; markSelfChange(ticket.id); try { - await api.addComment(ticket.id, b, undefined, proj()); + await api.addComment(ticket.id, b, undefined, proj(), media); } catch (e) { saveError = e instanceof Error ? e.message : String(e); + // Not sent: the text and its media come back into the box. + commentDraft = b; + commentMedia = media; } } @@ -577,7 +626,7 @@ use:mention use:mdkeys use:mdlists - onpaste={(e) => onPasteAttach(e, 'comment')} + onpaste={onPasteComment} onkeydown={(e) => { if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) addComment(); }} @@ -588,6 +637,37 @@ + {#if commentMedia.length || commentUploading} +
+ {commentUploading ? 'Uploading…' : 'Attached to this comment'} +
+ {#each commentMedia as m (m.path)} + + {#if mediaKind(m.mime, m.name) === 'image'} + + {:else} + {m.name} + {/if} + + + {/each} +
+
+ {/if} {#if pasted.at === 'comment' && (pasted.files.length || pasted.paths.length)}
Attached to the ticket @@ -620,7 +700,36 @@ > {/if}
-
+ {#if item.comment.body} +
+ {/if} + {#if item.comment.attachments?.length} +
+ {#each item.comment.attachments as m (m.path)} + {#if mediaKind(m.mime, m.name) === 'image'} + + {:else} + + {/if} + {/each} +
+ {/if} {:else}
@@ -757,6 +866,60 @@ flex-direction: column; gap: 6px; } + /* A comment's own media: thumbnails under its text (and under the box while + it is being written); a click opens the full-screen preview. */ + .c-media { + display: flex; + flex-wrap: wrap; + gap: 6px; + margin-top: 6px; + } + .c-thumb { + padding: 0; + border: 1px solid var(--wp-border); + border-radius: var(--wp-r-sm); + background: none; + cursor: zoom-in; + overflow: hidden; + } + .c-thumb img { + display: block; + height: 120px; + max-width: 240px; + object-fit: cover; + } + .c-file { + display: inline-flex; + align-items: center; + gap: 5px; + height: 26px; + padding: 0 9px; + border: 1px solid var(--wp-border); + border-radius: var(--wp-r-sm); + background: var(--wp-card); + color: var(--wp-text-muted); + font-size: 12px; + } + button.c-file { + cursor: pointer; + } + .c-staged { + position: relative; + display: inline-flex; + } + .c-unstage { + position: absolute; + top: 4px; + right: 4px; + display: inline-flex; + padding: 3px; + border: none; + border-radius: var(--wp-r-pill); + background: color-mix(in srgb, var(--wp-card) 78%, transparent); + backdrop-filter: blur(4px); + color: var(--wp-text); + cursor: pointer; + } .pasted-cap { font-size: 11px; color: var(--wp-text-subtle); diff --git a/apps/desktop/src/lib/types.ts b/apps/desktop/src/lib/types.ts index a28bff2..a40231e 100644 --- a/apps/desktop/src/lib/types.ts +++ b/apps/desktop/src/lib/types.ts @@ -6,6 +6,8 @@ export interface Comment { body: string; created: string; edited?: string; + /** Media that belongs to this comment (omitted when there is none). */ + attachments?: Attachment[]; } /** A structured change event on a ticket (see wipe-core `Activity`). */ diff --git a/crates/wipe-core/src/model.rs b/crates/wipe-core/src/model.rs index 300ba32..b1f9ddb 100644 --- a/crates/wipe-core/src/model.rs +++ b/crates/wipe-core/src/model.rs @@ -352,6 +352,7 @@ impl Ticket { body: body.into(), created: now, edited: None, + attachments: Vec::new(), }); self.updated = now; id @@ -446,6 +447,11 @@ pub struct Comment { /// When the comment was last edited, if ever. #[serde(default, skip_serializing_if = "Option::is_none")] pub edited: Option>, + /// Media/files that belong to this comment (same storage as ticket + /// attachments), e.g. the screenshot it talks about. They are not part of + /// the ticket's own attachment list. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub attachments: Vec, } /// A single checklist / to-do item within a ticket. Ordered by position in the diff --git a/crates/wipe-core/src/ops.rs b/crates/wipe-core/src/ops.rs index a65f774..2b073ff 100644 --- a/crates/wipe-core/src/ops.rs +++ b/crates/wipe-core/src/ops.rs @@ -210,6 +210,26 @@ pub fn add_comment( Ok(id) } +/// Append a comment that carries attachments - media staged with +/// [`stage_media`]. They belong to the comment and stay with it; the ticket's +/// own attachment list is not touched. Returns the new comment's ID. +pub fn add_comment_with_attachments( + store: &Store, + ticket_id: &str, + author: &str, + body: &str, + attachments: Vec, + now: DateTime, +) -> Result { + let mut ticket = store.load_ticket(ticket_id)?; + let id = ticket.add_comment(author, body, now); + if let Some(comment) = ticket.comments.last_mut() { + comment.attachments = attachments; + } + store.save_ticket(&ticket)?; + Ok(id) +} + /// Delete a comment from a ticket by its ID. Errors if the comment is absent. /// `next_comment` is deliberately NOT decremented, so IDs are never reused. pub fn delete_comment( @@ -1500,6 +1520,23 @@ mod tests { assert!(b.path.starts_with(".wipe/media/") && root.join(&b.path).exists()); } + #[test] + fn a_comment_keeps_its_attachments() { + let (_d, s) = project(); + let id = new_ticket(&s, "A", None, "t").id; + let shot = stage_media(&s, "shot.png", b"PNGDATA", "image/png").unwrap(); + let with = vec![shot.clone()]; + let cid = add_comment_with_attachments(&s, &id, "t", "see it", with, now()).unwrap(); + add_comment(&s, &id, "t", "plain", now()).unwrap(); + + let t = s.load_ticket(&id).unwrap(); + assert_eq!(t.comments[0].id, cid); + assert_eq!(t.comments[0].attachments, vec![shot]); + assert!(t.comments[1].attachments.is_empty()); + // It is the comment's image, not one more attachment of the ticket. + assert!(t.attachments.is_empty()); + } + #[test] fn agents_belong_to_a_person_and_ids_stay_unique() { let (_d, s) = project(); diff --git a/crates/wipe-daemon/src/api.rs b/crates/wipe-daemon/src/api.rs index 944d2cc..c2c5197 100644 --- a/crates/wipe-daemon/src/api.rs +++ b/crates/wipe-daemon/src/api.rs @@ -625,6 +625,9 @@ pub struct CommentBody { #[serde(default)] author: Option, body: String, + /// Media that belongs to the comment (staged with `POST /api/media`). + #[serde(default)] + attachments: Vec, } /// `POST /api/tickets/{id}/comments` @@ -639,7 +642,12 @@ pub async fn add_comment( // default) instead of stamping the sentinel "ui", so UI comments are attributed // to the real identity and render consistently with CLI/forum authorship. let author = resolve_actor(&state, &store, b.author); - let cid = ops::add_comment(&store, &id, &author, &b.body, Utc::now())?; + let media = checked_media(b.attachments)?; + let cid = if media.is_empty() { + ops::add_comment(&store, &id, &author, &b.body, Utc::now())? + } else { + ops::add_comment_with_attachments(&store, &id, &author, &b.body, media, Utc::now())? + }; notify(&state); Ok(Json(json!({ "ok": true, "ticket": id, "comment": cid }))) }