Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/desktop/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
);
},

Expand Down
173 changes: 168 additions & 5 deletions apps/desktop/src/lib/components/TicketModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<Attachment[]>([]);
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;
}
}

Expand Down Expand Up @@ -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();
}}
Expand All @@ -588,6 +637,37 @@
<Send size={14} />
</button>
</div>
{#if commentMedia.length || commentUploading}
<div class="pasted">
<span class="pasted-cap"
>{commentUploading ? 'Uploading…' : 'Attached to this comment'}</span
>
<div class="c-media">
{#each commentMedia as m (m.path)}
<span class="c-staged">
{#if mediaKind(m.mime, m.name) === 'image'}
<button
class="c-thumb"
onclick={() =>
openLightbox({ src: mediaUrl(m.path, proj()), name: m.name, kind: 'image' })}
>
<img src={mediaUrl(m.path, proj())} alt={m.name} />
</button>
{:else}
<span class="c-file"><Paperclip size={12} />{m.name}</span>
{/if}
<button
class="c-unstage"
aria-label="Remove {m.name} from the comment"
title="Remove from the comment"
onclick={() => (commentMedia = commentMedia.filter((x) => x.path !== m.path))}
><X size={12} /></button
>
</span>
{/each}
</div>
</div>
{/if}
{#if pasted.at === 'comment' && (pasted.files.length || pasted.paths.length)}
<div class="pasted"><span class="pasted-cap">Attached to the ticket</span>
<StagedMedia files={pasted.files} paths={pasted.paths} />
Expand Down Expand Up @@ -620,7 +700,36 @@
>
{/if}
</div>
<div class="c-body"><Markdown source={item.comment.body} /></div>
{#if item.comment.body}
<div class="c-body"><Markdown source={item.comment.body} /></div>
{/if}
{#if item.comment.attachments?.length}
<div class="c-media">
{#each item.comment.attachments as m (m.path)}
{#if mediaKind(m.mime, m.name) === 'image'}
<button
class="c-thumb"
onclick={() =>
openLightbox({ src: mediaUrl(m.path, proj()), name: m.name, kind: 'image' })}
>
<img src={mediaUrl(m.path, proj())} alt={m.name} loading="lazy" />
</button>
{:else}
<button
class="c-file"
onclick={() =>
openLightbox({
src: mediaUrl(m.path, proj()),
name: m.name,
kind: mediaKind(m.mime, m.name)
})}
>
<Paperclip size={12} />{m.name}
</button>
{/if}
{/each}
</div>
{/if}
</div>
{:else}
<div class="event">
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`). */
Expand Down
6 changes: 6 additions & 0 deletions crates/wipe-core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ impl Ticket {
body: body.into(),
created: now,
edited: None,
attachments: Vec::new(),
});
self.updated = now;
id
Expand Down Expand Up @@ -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<DateTime<Utc>>,
/// 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<Attachment>,
}

/// A single checklist / to-do item within a ticket. Ordered by position in the
Expand Down
37 changes: 37 additions & 0 deletions crates/wipe-core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attachment>,
now: DateTime<Utc>,
) -> Result<String> {
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(
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion crates/wipe-daemon/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ pub struct CommentBody {
#[serde(default)]
author: Option<String>,
body: String,
/// Media that belongs to the comment (staged with `POST /api/media`).
#[serde(default)]
attachments: Vec<Attachment>,
}

/// `POST /api/tickets/{id}/comments`
Expand All @@ -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 })))
}
Expand Down