impr: remove individual if conditions for badges in posts

This commit is contained in:
James Xylight committed 2025-07-30 15:39:10 -07:00
1 parent e987628cc1
commit f733b57f60
4 files changed
+99 -93

No files matched your search

+83 -75
View File
@@ -1,4 +1,14 @@
<script lang="ts" module>
type BadgeType =
| 'nsfw'
| 'saved'
| 'featured'
| 'deleted'
| 'removed'
| 'locked'
| 'moderator'
| 'admin'
export interface Tag {
content: string
color?: string
@@ -56,11 +66,11 @@
import { settings, type View } from '$lib/settings.svelte'
import type { Community, Person, SubscribedType } from 'lemmy-js-client'
import { Badge, Popover } from 'mono-svelte'
import Material from 'mono-svelte/materials/Material.svelte'
import {
Bookmark,
ExclamationTriangle,
Icon,
LockClosed,
Megaphone,
PaperAirplane,
Pencil,
@@ -70,7 +80,64 @@
} from 'svelte-hero-icons'
import CommunityHeader from '../community/CommunityHeader.svelte'
import ShieldIcon from '../moderation/ShieldIcon.svelte'
import Material from 'mono-svelte/materials/Material.svelte'
const badgeToData: Map<
BadgeType,
{
icon: IconSource
color: 'red-subtle' | 'yellow-subtle' | 'green-subtle'
label: string
}
> = new Map([
[
'nsfw',
{
icon: ExclamationTriangle,
color: 'red-subtle',
label: t.get('post.badges.nsfw'),
},
],
[
'saved',
{
icon: Bookmark,
color: 'yellow-subtle',
label: t.get('post.badges.saved'),
},
],
[
'featured',
{
icon: Megaphone,
color: 'green-subtle',
label: t.get('post.badges.featured'),
},
],
[
'removed',
{
icon: Trash,
color: 'green-subtle',
label: t.get('post.badges.removed'),
},
],
[
'deleted',
{
icon: Trash,
color: 'red-subtle',
label: t.get('post.badges.deleted'),
},
],
[
'locked',
{
icon: Lock,
color: 'yellow-subtle',
label: t.get('post.badges.locked'),
},
],
])
interface Props {
community?: Community
@@ -83,17 +150,7 @@
read?: boolean
edited?: string
view?: View
// Badges
badges?: {
nsfw: boolean
saved: boolean
featured: boolean
deleted: boolean
removed: boolean
locked: boolean
moderator: boolean
admin: boolean
}
badges?: Record<BadgeType, boolean>
tags?: Tag[]
style?: string
titleClass?: string
@@ -196,10 +253,7 @@
/>
{/if}
<div
class="flex flex-row gap-1.5 items-center
no-list-margin {view == 'compact' && showCommunity
? 'min-[480px]:mx-2'
: ''}"
class="flex flex-row gap-1.5 items-center no-list-margin"
style="grid-area: stats;"
>
{#if user}
@@ -209,7 +263,7 @@
src={PaperAirplane}
size="12"
micro
class="rotate-180 text-slate-400 dark:text-zinc-600 max-[480px]:hidden"
class="rotate-180 text-slate-400 dark:text-zinc-600 max-[480px]:hidden ml-2"
/>
{/if}
<UserLink
@@ -269,66 +323,20 @@
</a>
{/each}
{/if}
{#if badges.nsfw}
<Badge label={$t('post.badges.nsfw')} color="red-subtle" allowIconOnly>
{#each Object.keys(badges)
// filter by ones that are true
.filter(i => badges[i as BadgeType] == true)
// get from predetermined map
.map(i => badgeToData.get(i as BadgeType))
// remove null
.filter(i => i != undefined) as badge}
<Badge label={badge.label} color={badge.color} allowIconOnly>
{#snippet icon()}
<Icon src={ExclamationTriangle} size="14" micro />
<Icon src={badge.icon} micro size="14" />
{/snippet}
{$t('post.badges.nsfw')}
{badge.label}
</Badge>
{/if}
{#if badges.saved}
<Badge
label={$t('post.badges.saved')}
color="yellow-subtle"
allowIconOnly
>
{#snippet icon()}
<Icon src={Bookmark} micro size="14" />
{/snippet}
{$t('post.badges.saved')}
</Badge>
{/if}
{#if badges.locked}
<Badge
label={$t('post.badges.locked')}
color="yellow-subtle"
allowIconOnly
>
{#snippet icon()}
<Icon src={LockClosed} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.locked')}</span>
</Badge>
{/if}
{#if badges.removed}
<Badge label={$t('post.badges.removed')} color="red-subtle" allowIconOnly>
{#snippet icon()}
<Icon src={Trash} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.removed')}</span>
</Badge>
{/if}
{#if badges.deleted}
<Badge label={$t('post.badges.deleted')} color="red-subtle" allowIconOnly>
{#snippet icon()}
<Icon src={Trash} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.deleted')}</span>
</Badge>
{/if}
{#if badges.featured}
<Badge
label={$t('post.badges.featured')}
color="green-subtle"
allowIconOnly
>
{#snippet icon()}
<Icon src={Megaphone} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.featured')}</span>
</Badge>
{/if}
{/each}
{@render extraBadges?.()}
</div>
</header>
@@ -50,7 +50,6 @@
let hasMore = $state(true)
async function loadMore() {
console.log(hasMore, loading)
if (!hasMore || loading) return
try {
@@ -183,7 +182,7 @@
{#snippet item(row)}
<li
data-index={row}
style={row < 7 ? `--anim-delay: ${row * 50}ms` : ''}
style={row < 9 ? `--anim-delay: ${row * 50}ms` : ''}
class={[
'relative post-container px-3 sm:px-6',
row < 9 && 'pop-in opacity-0',
@@ -270,6 +269,6 @@
.pop-in {
animation: popIn 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards
var(--anim-delay);
var(--anim-delay, 0ms);
}
</style>