Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): pin imageUrl-only for non-media keys (canon-sheet/comic-page/noun) so they don't 400; hide button when nothing pinnable (#1455)

Adam Eivycf8c82b898d9
diff --git a/client/src/components/media/PinToMoodBoardMenu.jsx b/client/src/components/media/PinToMoodBoardMenu.jsx
index f0e308a345..f25e146f18 100644
--- a/client/src/components/media/PinToMoodBoardMenu.jsx
+++ b/client/src/components/media/PinToMoodBoardMenu.jsx
@@ -14,44 +14,63 @@ import {
// CollectionPickerShell with the mood-board API injected as the data source, so
// the popover positioning / search / inline-create plumbing isn't duplicated.
//
-// `item` is the normalized MediaCard shape: `item.key` is the `<kind>:<ref>`
-// media-key (e.g. `image:foo.png`, `video:job-123`) and `item.previewUrl` is a
-// renderable thumbnail URL (or null). We pin as an `image`-type board item
-// carrying that mediaKey for source linkage PLUS the previewUrl as `imageUrl`
-// so the board renders the thumbnail directly — this is how video pins show a
-// frame without the detail-page resolver needing to map a `video:` key back to
-// a thumbnail filename (which it can't derive from the id alone).
+// `item` is the normalized MediaCard shape: `item.previewUrl` is a renderable
+// thumbnail URL (or null), and `item.key` is usually a `<kind>:<ref>` media-key
+// (e.g. `image:foo.png`, `video:job-123`) — but NOT always: the lightbox is
+// also used for synthetic items keyed `canon-sheet:…`, `comic-page:…`, `noun:…`
+// that the server's media-key validator rejects. So we pin a real mediaKey only
+// when the key is a valid `image:`/`video:` media-key (for source linkage +
+// dedup), and fall back to an imageUrl-only pin otherwise — keying membership
+// on whichever identifier we sent. The button hides only when NEITHER a valid
+// media-key NOR a renderable thumbnail is available (nothing to pin).
//
-// Membership is keyed on `mediaKey === item.key`: a board "contains" this asset
-// when any of its items pins the same media-key. Pinning again removes it
-// (toggle), matching the collection menu's behavior.
+// Pinning the same asset again removes it (toggle), matching the collection
+// menu's behavior.
const SIZES = {
sm: { button: 'px-1.5 py-1 text-[10px]', icon: 'w-3 h-3' },
md: { button: 'px-2 py-1.5 text-xs', icon: 'w-3.5 h-3.5' },
};
-// Find the board item that pins this media-key (or undefined).
-const findPinned = (board, mediaKey) =>
- (Array.isArray(board?.items) ? board.items : []).find((it) => it?.mediaKey === mediaKey);
+// Minimal mirror of the server's media-key vocabulary (server/lib/mediaItemKey
+// — only `image:`/`video:` kinds, non-empty single-segment ref). Kept inline
+// (one predicate) rather than mirroring the whole module: the board item schema
+// rejects anything else, so we must not send a non-media key as `mediaKey`.
+const isValidMediaKey = (key) => /^(image|video):[^:]+$/.test(key || '');
+
+// Find the board item this asset is pinned as — by mediaKey when we have a real
+// one, else by the imageUrl we pinned it under.
+const findPinned = (board, { mediaKey, imageUrl }) =>
+ (Array.isArray(board?.items) ? board.items : []).find((it) => (
+ mediaKey ? it?.mediaKey === mediaKey : (!!imageUrl && it?.imageUrl === imageUrl)
+ ));
export default function PinToMoodBoardMenu({ item, size = 'sm' }) {
const [open, setOpen] = useState(false);
const [busyId, setBusyId] = useState(null);
const triggerRef = useRef(null);
- const mediaKey = item.key;
+ // A real media-key only when it matches the server vocabulary; synthetic keys
+ // (canon-sheet:/comic-page:/noun:) fall through to an imageUrl-only pin.
+ const mediaKey = isValidMediaKey(item.key) ? item.key : null;
// Only http(s) / absolute app paths are valid imageUrls (matches the board
- // item schema); a missing/blob preview is dropped so we pin mediaKey-only.
+ // item schema); a missing/blob preview is dropped.
const thumbUrl = typeof item.previewUrl === 'string' && /^(https?:\/\/|\/)/.test(item.previewUrl)
? item.previewUrl
: null;
- // The board item payload: the media-key for source linkage, plus the
- // thumbnail as imageUrl so a video pin (no derivable thumbnail from its key)
- // still renders. Built once so the toggle + create-and-pin paths can't drift.
- const pinPayload = useMemo(() => (thumbUrl
- ? { type: 'image', mediaKey, imageUrl: thumbUrl }
- : { type: 'image', mediaKey }), [mediaKey, thumbUrl]);
+ // The board item payload: a real media-key for source linkage + dedup when we
+ // have one, plus the thumbnail as imageUrl so the board renders directly (and
+ // so a video pin shows a frame its key can't resolve). Built once so the
+ // toggle + create-and-pin paths can't drift. Null when there's nothing to pin.
+ const pinPayload = useMemo(() => {
+ if (!mediaKey && !thumbUrl) return null;
+ const payload = { type: 'image' };
+ if (mediaKey) payload.mediaKey = mediaKey;
+ if (thumbUrl) payload.imageUrl = thumbUrl;
+ return payload;
+ }, [mediaKey, thumbUrl]);
+ // The identifiers membership/dedup keys on (mirror the payload).
+ const pinKey = useMemo(() => ({ mediaKey, imageUrl: thumbUrl }), [mediaKey, thumbUrl]);
const handleToggleOpen = (e) => {
e.stopPropagation();
@@ -59,7 +78,7 @@ export default function PinToMoodBoardMenu({ item, size = 'sm' }) {
};
const handleTogglePin = useCallback(async (board, updateBoards) => {
- const pinned = findPinned(board, mediaKey);
+ const pinned = findPinned(board, pinKey);
setBusyId(board.id);
if (pinned) {
const updated = await removeMoodBoardItem(board.id, pinned.id, { silent: true }).catch((err) => {
@@ -84,10 +103,10 @@ export default function PinToMoodBoardMenu({ item, size = 'sm' }) {
b.id === board.id ? { ...b, items: [...(Array.isArray(b.items) ? b.items : []), created] } : b
)));
toast.success(`Pinned to ${board.name}`);
- }, [mediaKey, pinPayload]);
+ }, [pinKey, pinPayload]);
const renderItem = (board, { updateCollections }) => {
- const pinned = !!findPinned(board, mediaKey);
+ const pinned = !!findPinned(board, pinKey);
return (
<button
key={board.id}
@@ -124,6 +143,10 @@ export default function PinToMoodBoardMenu({ item, size = 'sm' }) {
}
}, [pinPayload]);
+ // Nothing pinnable (no valid media-key and no renderable thumbnail) — hide the
+ // button rather than render a control that would 400 on click.
+ if (!pinPayload) return null;
+
const sizeCls = SIZES[size] || SIZES.sm;
return (
diff --git a/client/src/components/media/PinToMoodBoardMenu.test.jsx b/client/src/components/media/PinToMoodBoardMenu.test.jsx
index d5958d8b89..48e61a167f 100644
--- a/client/src/components/media/PinToMoodBoardMenu.test.jsx
+++ b/client/src/components/media/PinToMoodBoardMenu.test.jsx
@@ -84,4 +84,48 @@ describe('PinToMoodBoardMenu', () => {
{ silent: true },
);
});
+
+ it('pins imageUrl-only (no mediaKey) for a synthetic non-media key like canon-sheet:', async () => {
+ api.listMoodBoards.mockResolvedValue([{ id: 'b1', name: 'Refs', items: [] }]);
+ api.addMoodBoardItem.mockResolvedValue({ id: 'mbi-3', type: 'image', imageUrl: '/data/image-refs/sheet.png' });
+
+ render(<PinToMoodBoardMenu item={{
+ kind: 'image', key: 'canon-sheet:hero:sheet.png', previewUrl: '/data/image-refs/sheet.png',
+ }} />);
+ fireEvent.click(screen.getByTitle('Pin to mood board'));
+ const row = await screen.findByRole('menuitemcheckbox', { name: /Refs/ });
+ await act(async () => { fireEvent.click(row); });
+
+ // The server rejects `canon-sheet:` as a mediaKey, so we send imageUrl only.
+ expect(api.addMoodBoardItem).toHaveBeenCalledWith(
+ 'b1',
+ { type: 'image', imageUrl: '/data/image-refs/sheet.png' },
+ { silent: true },
+ );
+ });
+
+ it('matches existing imageUrl-only pins for membership/toggle (synthetic key)', async () => {
+ api.listMoodBoards.mockResolvedValue([
+ { id: 'b1', name: 'Refs', items: [{ id: 'mbi-7', type: 'image', imageUrl: '/data/image-refs/sheet.png' }] },
+ ]);
+ api.removeMoodBoardItem.mockResolvedValue({ id: 'b1', name: 'Refs', items: [] });
+
+ render(<PinToMoodBoardMenu item={{
+ kind: 'image', key: 'canon-sheet:hero:sheet.png', previewUrl: '/data/image-refs/sheet.png',
+ }} />);
+ fireEvent.click(screen.getByTitle('Pin to mood board'));
+ const row = await screen.findByRole('menuitemcheckbox', { name: /Refs/ });
+ expect(row).toHaveAttribute('aria-checked', 'true');
+
+ await act(async () => { fireEvent.click(row); });
+ expect(api.removeMoodBoardItem).toHaveBeenCalledWith('b1', 'mbi-7', { silent: true });
+ });
+
+ it('renders nothing when there is no valid media-key and no renderable thumbnail', () => {
+ const { container } = render(
+ <PinToMoodBoardMenu item={{ kind: 'image', key: 'noun:x.png', previewUrl: 'blob:abc' }} />,
+ );
+ expect(container).toBeEmptyDOMElement();
+ expect(screen.queryByTitle('Pin to mood board')).toBeNull();
+ });
});