managarten/apps/mana/apps/web/src/lib/modules/picture/queries.ts
Till JS 4fc9d6c59c feat(wardrobe): module foundation — garments + outfits space-scoped data layer (M1)
M1 of docs/plans/wardrobe-module.md — pure data layer + backend plumbing,
zero UI (that's M2). A user can now hold a digital wardrobe per space:
brand merch, club Trikots, family Kleiderschrank, team Kostüme, practice
Dresscode, and personal closet all live as separate pools under the same
Dexie tables, space-scoped like tags/scenes/agents after Phase 2c.

Data model — two tables, no join:

- wardrobeGarments (Dexie v41): single clothing items / accessories.
  Indexed on `category` + `createdAt` + `isArchived`. Encrypted:
  name/brand/color/size/material/tags/notes. Plaintext: category,
  mediaIds, counters, timestamps — all indexed or structural.
  `mediaIds[0]` is the primary photo used for try-on; additional
  ids are alternate views (back, detail) for M7.

- wardrobeOutfits (Dexie v41): named compositions referencing
  garment ids. Encrypted: name/description/tags. Plaintext:
  garmentIds (FK array), occasion (closed enum — useful for
  undecrypted filtering), season, booleans, lastTryOn snapshot.

- picture.images gains `wardrobeOutfitId?: string | null` as a
  plaintext back-reference. Try-on results land in the Picture
  gallery like any other generation; the outfit detail view
  queries them via this id rather than maintaining a third table.

Space scope:

- `wardrobe` added to all five explicit allowlists in shared-types/
  spaces.ts (personal is wildcard, no edit needed). Each space type
  gets a one-line comment explaining the real-world use case.
- App registry: `wardrobe` entry in shared-branding/mana-apps.ts
  with a rose→fuchsia gradient icon (T-shirt on hanger silhouette),
  color #e11d48, tier 'beta', status 'beta'.
- Module registry: wardrobeModuleConfig imported + appended to
  MODULE_CONFIGS so SYNC_APP_MAP picks it up automatically.

Backend:

- MAX_REFERENCE_IMAGES bumped 4 → 8 in picture/generate-with-
  reference (plus the client-side default in ReferenceImagePicker).
  Justified with a comment: face + body + top + bottom + shoes +
  outerwear + 2 accessories = 8. Cost doesn't scale with ref count
  (OpenAI bills per output), so the bump is a pure capability
  expansion with no credit-side risk.
- New POST /api/v1/wardrobe/garments/upload wraps uploadImageToMedia
  with app='wardrobe'. Registered under /api/v1/wardrobe in index.ts.
  Pattern 1:1 with the profile/me-images/upload endpoint; tier-gating
  falls out of wardrobe NOT being in RESOURCE_MODULES (tier='guest'
  works — consistent with picture's plain CRUD).

Stores emit domain events (WardrobeGarmentAdded, WardrobeOutfitCreated,
WardrobeOutfitTryOn, etc.) so later mana-ai missions can observe
activity without polling.

No UI in this commit. M2 (Garments-Grundlayer) wires the route + grid
+ upload-zone; M3 the Outfit composer; M4 the Try-On integration.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 18:27:37 +02:00

192 lines
6.7 KiB
TypeScript

/**
* Reactive Queries & Pure Helpers for Picture module.
*
* Uses Dexie liveQuery to automatically re-render when IndexedDB changes
* (local writes, sync updates, other tabs). Components call these hooks
* at init time; no manual fetch/refresh needed.
*/
import { liveQuery } from 'dexie';
import { useLiveQueryWithDefault } from '@mana/local-store/svelte';
import { db } from '$lib/data/database';
import { scopedForModule } from '$lib/data/scope';
import { decryptRecords } from '$lib/data/crypto';
import type {
LocalImage,
LocalBoard,
LocalBoardItem,
LocalImageTag,
Image,
Board,
BoardWithCount,
} from './types';
// ─── Type Converters ──────────────────────────────────────
export function toImage(local: LocalImage): Image {
return {
id: local.id,
prompt: local.prompt,
negativePrompt: local.negativePrompt ?? undefined,
model: local.model ?? undefined,
style: local.style ?? undefined,
publicUrl: local.publicUrl ?? undefined,
storagePath: local.storagePath,
filename: local.filename,
format: local.format ?? undefined,
width: local.width ?? undefined,
height: local.height ?? undefined,
fileSize: local.fileSize ?? undefined,
blurhash: local.blurhash ?? undefined,
isPublic: local.isPublic,
isFavorite: local.isFavorite,
downloadCount: local.downloadCount,
rating: local.rating ?? undefined,
isArchived: local.isArchived ?? undefined,
generationId: local.generationId ?? undefined,
sourceImageId: local.sourceImageId ?? undefined,
referenceImageIds: local.referenceImageIds ?? undefined,
generationMode: local.generationMode ?? undefined,
wardrobeOutfitId: local.wardrobeOutfitId ?? undefined,
createdAt: local.createdAt ?? new Date().toISOString(),
updatedAt: local.updatedAt ?? new Date().toISOString(),
};
}
export function toBoard(local: LocalBoard): Board {
return {
id: local.id,
name: local.name,
description: local.description ?? undefined,
thumbnailUrl: local.thumbnailUrl ?? undefined,
canvasWidth: local.canvasWidth,
canvasHeight: local.canvasHeight,
backgroundColor: local.backgroundColor,
isPublic: local.isPublic,
createdAt: local.createdAt ?? new Date().toISOString(),
updatedAt: local.updatedAt ?? new Date().toISOString(),
};
}
// ─── Svelte 5 Reactive Hooks (call during component init) ──
/** All non-archived images, sorted by createdAt desc. */
export function useAllImages() {
return useLiveQueryWithDefault(async () => {
const locals = await scopedForModule<LocalImage, string>('picture', 'images').toArray();
const visible = locals.filter((img) => !img.isArchived && !img.deletedAt);
const decrypted = await decryptRecords('images', visible);
return decrypted
.map(toImage)
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
}, [] as Image[]);
}
/** All archived images, sorted by createdAt desc. */
export function useArchivedImages() {
return useLiveQueryWithDefault(async () => {
const locals = await scopedForModule<LocalImage, string>('picture', 'images').toArray();
const visible = locals.filter((img) => !!img.isArchived && !img.deletedAt);
const decrypted = await decryptRecords('images', visible);
return decrypted
.map(toImage)
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
}, [] as Image[]);
}
/** All boards with item counts, sorted by updatedAt desc. */
export function useAllBoards() {
return useLiveQueryWithDefault(async () => {
const locals = await scopedForModule<LocalBoard, string>('picture', 'boards').toArray();
const allItems = await scopedForModule<LocalBoardItem, string>(
'picture',
'boardItems'
).toArray();
// boardItems.textContent is encrypted but the count map only
// looks at structural fields (deletedAt + boardId), so no
// decrypt needed for the counter.
const itemCounts = new Map<string, number>();
for (const item of allItems) {
if (!item.deletedAt) {
itemCounts.set(item.boardId, (itemCounts.get(item.boardId) || 0) + 1);
}
}
const visible = locals.filter((b) => !b.deletedAt);
// boards.name + description are encrypted on disk; the workbench
// + dashboard widgets render them, so decrypt before mapping.
const decrypted = await decryptRecords('boards', visible);
return decrypted
.map(
(local): BoardWithCount => ({
...toBoard(local),
itemCount: itemCounts.get(local.id) || 0,
})
)
.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
}, [] as BoardWithCount[]);
}
// Tags: use shared global tags from @mana/shared-stores
export { useAllTags as useAllPictureTags } from '@mana/shared-stores';
/** All image-tag associations. */
export function useAllImageTags() {
return useLiveQueryWithDefault(async () => {
return await scopedForModule<LocalImageTag, string>('picture', 'imageTags').toArray();
}, [] as LocalImageTag[]);
}
// ─── Raw Observable Queries ────────────────────────────────
export function allImages$() {
return liveQuery(async () => {
const locals = await scopedForModule<LocalImage, string>('picture', 'images').toArray();
const visible = locals.filter((img) => !img.isArchived && !img.deletedAt);
const decrypted = await decryptRecords('images', visible);
return decrypted
.map(toImage)
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
});
}
export function allBoards$() {
return liveQuery(async () => {
const locals = await scopedForModule<LocalBoard, string>('picture', 'boards').toArray();
const visible = locals.filter((b) => !b.deletedAt);
const decrypted = await decryptRecords('boards', visible);
return decrypted.map(toBoard);
});
}
// ─── Pure Helper Functions (for $derived) ─────────────────
/** Filter images by favorites only. */
export function getFavoriteImages(images: Image[]): Image[] {
return images.filter((img) => img.isFavorite);
}
/** Filter images by tag IDs using image-tag associations. */
export function getImagesByTags(
images: Image[],
imageTags: { imageId: string; tagId: string }[],
selectedTagIds: string[]
): Image[] {
if (selectedTagIds.length === 0) return images;
const imageIdsWithTags = new Set(
imageTags.filter((it) => selectedTagIds.includes(it.tagId)).map((it) => it.imageId)
);
return images.filter((img) => imageIdsWithTags.has(img.id));
}
/** Find an image by ID. */
export function findImageById(images: Image[], id: string): Image | undefined {
return images.find((img) => img.id === id);
}
/** Find a board by ID. */
export function findBoardById(boards: BoardWithCount[], id: string): BoardWithCount | undefined {
return boards.find((b) => b.id === id);
}