refactor count map

This commit is contained in:
mertalev 2025-10-14 16:13:07 -04:00
parent 23d2abc5d1
commit 4c6aa0568b
No known key found for this signature in database
GPG key ID: DF6ABC77AAD98C95
2 changed files with 8 additions and 8 deletions

View file

@ -79,7 +79,6 @@ export class AlbumService extends BaseService {
const album = await this.findOrFail(id, { withAssets }); const album = await this.findOrFail(id, { withAssets });
const [albumMetadataForIds] = await this.albumRepository.getMetadataForIds([album.id]); const [albumMetadataForIds] = await this.albumRepository.getMetadataForIds([album.id]);
// Determine if the album is shared (shared users or shared link)
const hasSharedUsers = album.albumUsers && album.albumUsers.length > 0; const hasSharedUsers = album.albumUsers && album.albumUsers.length > 0;
const hasSharedLink = album.sharedLinks && album.sharedLinks.length > 0; const hasSharedLink = album.sharedLinks && album.sharedLinks.length > 0;
const isShared = hasSharedUsers || hasSharedLink; const isShared = hasSharedUsers || hasSharedLink;
@ -90,7 +89,6 @@ export class AlbumService extends BaseService {
endDate: albumMetadataForIds?.endDate ?? undefined, endDate: albumMetadataForIds?.endDate ?? undefined,
assetCount: albumMetadataForIds?.assetCount ?? 0, assetCount: albumMetadataForIds?.assetCount ?? 0,
lastModifiedAssetTimestamp: albumMetadataForIds?.lastModifiedAssetTimestamp ?? undefined, lastModifiedAssetTimestamp: albumMetadataForIds?.lastModifiedAssetTimestamp ?? undefined,
// Fetch contributor counts only for shared albums
contributorCounts: isShared ? await this.albumRepository.getContributorCounts(album.id) : undefined, contributorCounts: isShared ? await this.albumRepository.getContributorCounts(album.id) : undefined,
}; };
} }

View file

@ -32,10 +32,12 @@
let isOwned = $derived(currentUser?.id == album.ownerId); let isOwned = $derived(currentUser?.id == album.ownerId);
// Build a map of contributor counts by user id; avoid casts/derived // Build a map of contributor counts by user id; avoid casts/derived
const contributorMap: Record<string, number> = Object.fromEntries( const contributorCounts: Record<string, number> = {};
(album.contributorCounts ?? []).map(({ userId, assetCount }) => [userId, assetCount]), if (album.contributorCounts) {
); for (const { userId, assetCount } of album.contributorCounts) {
const getCount = (userId: string) => contributorMap[userId] ?? 0; contributorCounts[userId] = assetCount;
}
}
onMount(async () => { onMount(async () => {
try { try {
@ -117,9 +119,9 @@
{:else} {:else}
{$t('role_editor')} {$t('role_editor')}
{/if} {/if}
{#if getCount(user.id) > 0} {#if user.id in contributorCounts}
<span>-</span> <span>-</span>
{$t('items_count', { values: { count: getCount(user.id) } })} {$t('items_count', { values: { count: contributorCounts[user.id] } })}
{/if} {/if}
</Text> </Text>
</div> </div>