Skip to content
Merged
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
10 changes: 10 additions & 0 deletions app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
--radius-xl: calc(var(--radius) + 4px);

--animate-shimmer: shimmer 2s ease-in-out infinite;
--animate-indeterminate: indeterminate 1.5s ease-in-out infinite;
}

@keyframes shimmer {
Expand All @@ -56,6 +57,15 @@
}
}

@keyframes indeterminate {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(400%);
}
}

:root {
--radius: 0.625rem;
--background: oklch(1 0 0);
Expand Down
57 changes: 49 additions & 8 deletions app/ycode/api/collections/[id]/items/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { getCollectionById } from '@/lib/repositories/collectionRepository';
import { clearAllCache } from '@/lib/services/cacheService';
import { setValuesByFieldName } from '@/lib/repositories/collectionItemValueRepository';
import { getFieldsByCollectionId } from '@/lib/repositories/collectionFieldRepository';
import { findStatusFieldId } from '@/lib/collection-field-utils';
import { getAssetsByIds } from '@/lib/repositories/assetRepository';
import { findStatusFieldId, isAssetFieldType, isMultipleAssetField } from '@/lib/collection-field-utils';
import type { StatusAction } from '@/lib/collection-field-utils';
import { noCache } from '@/lib/api-response';
import type { CollectionItemWithValues, CollectionField } from '@/types';

// Disable caching for this route
export const dynamic = 'force-dynamic';
Expand All @@ -22,6 +24,7 @@ export const revalidate = 0;
* - sortBy: string (optional) - Field ID to sort by, or 'manual', 'random', 'none'
* - sortOrder: 'asc' | 'desc' (optional, default: 'asc') - Sort order
* - offset: number (optional) - Number of items to skip
* - includeAssets: 'true' (optional) - Include referenced assets in the response
*/
export async function GET(
request: NextRequest,
Expand All @@ -39,6 +42,7 @@ export async function GET(
const sortOrder = (searchParams.get('sortOrder') || 'asc') as 'asc' | 'desc';
const offsetParam = searchParams.get('offset');
const filtersParam = searchParams.get('filters');
const includeAssets = searchParams.get('includeAssets') === 'true';

// Calculate offset (use explicit offset if provided, otherwise calculate from page)
const offset = offsetParam ? parseInt(offsetParam, 10) : (page - 1) * limit;
Expand Down Expand Up @@ -146,14 +150,18 @@ export async function GET(
items = items.slice(offset, offset + limit);
}

return noCache({
data: {
items,
total,
page,
limit,
// Optionally resolve referenced assets for the returned items
const responseData: Record<string, unknown> = { items, total, page, limit };

if (includeAssets) {
const assetIds = extractAssetIdsFromItems(items, allFields);
if (assetIds.length > 0) {
const assetsMap = await getAssetsByIds(assetIds, false);
responseData.referencedAssets = Object.values(assetsMap);
}
});
}

return noCache({ data: responseData });
} catch (error) {
console.error('Error fetching collection items:', error);
return noCache(
Expand All @@ -163,6 +171,39 @@ export async function GET(
}
}

/** Extract unique asset IDs from item values based on asset-type fields. */
function extractAssetIdsFromItems(
items: CollectionItemWithValues[],
fields: CollectionField[],
): string[] {
const assetFieldIds = fields.filter(f => isAssetFieldType(f.type)).map(f => f.id);
const multiAssetFieldIds = new Set(
fields.filter(f => isMultipleAssetField(f)).map(f => f.id),
);

if (assetFieldIds.length === 0) return [];

const ids = new Set<string>();

for (const item of items) {
for (const fieldId of assetFieldIds) {
const value = item.values[fieldId];
if (!value) continue;

if (multiAssetFieldIds.has(fieldId)) {
const arr = Array.isArray(value) ? value : [];
for (const v of arr) {
if (typeof v === 'string' && v) ids.add(v);
}
} else if (typeof value === 'string') {
ids.add(value);
}
}
}

return Array.from(ids);
}

/**
* POST /ycode/api/collections/[id]/items
* Create a new item with field values (draft)
Expand Down
Loading