fix:likes;UI:find card&list

This commit is contained in:
2025-10-16 17:29:05 +02:00
parent 067e228393
commit aea324988d
6 changed files with 320 additions and 878 deletions

View File

@@ -3,6 +3,8 @@
import { Badge } from '$lib/components/badge';
import LikeButton from '$lib/components/LikeButton.svelte';
import VideoPlayer from '$lib/components/VideoPlayer.svelte';
import { Avatar, AvatarFallback } from '$lib/components/avatar';
import { MoreHorizontal, MessageCircle, Share } from '@lucide/svelte';
interface FindCardProps {
id: string;
@@ -40,14 +42,62 @@
onExplore?.(id);
}
function truncateText(text: string, maxLength: number = 120): string {
if (text.length <= maxLength) return text;
return text.slice(0, maxLength).trim() + '...';
function getUserInitials(username: string): string {
return username.slice(0, 2).toUpperCase();
}
</script>
<div class="find-card">
<div class="find-card-media">
<!-- Post Header -->
<div class="post-header">
<div class="user-info">
<Avatar class="avatar">
<AvatarFallback class="avatar-fallback">
{getUserInitials(user.username)}
</AvatarFallback>
</Avatar>
<div class="user-details">
<div class="username">@{user.username}</div>
{#if locationName}
<div class="location">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none">
<path
d="M21 10C21 17 12 23 12 23S3 17 3 10A9 9 0 0 1 12 1A9 9 0 0 1 21 10Z"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
<circle cx="12" cy="10" r="3" stroke="currentColor" stroke-width="2" />
</svg>
<span>{locationName}</span>
</div>
{/if}
</div>
</div>
<Button variant="ghost" size="sm" class="more-button">
<MoreHorizontal size={16} />
</Button>
</div>
<!-- Post Content -->
<div class="post-content">
<div class="content-header">
<h3 class="post-title">{title}</h3>
{#if category}
<Badge variant="secondary" class="category-badge">
{category}
</Badge>
{/if}
</div>
{#if description}
<p class="post-description">{description}</p>
{/if}
</div>
<!-- Media -->
<div class="post-media">
{#if media && media.length > 0}
{#if media[0].type === 'photo'}
<img
@@ -62,13 +112,13 @@
poster={media[0].thumbnailUrl}
muted={true}
autoplay={false}
controls={false}
controls={true}
class="media-video"
/>
{/if}
{:else}
<div class="no-media">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none">
<svg width="32" height="32" viewBox="0 0 24 24" fill="none">
<path
d="M21 10C21 17 12 23 12 23S3 17 3 10A9 9 0 0 1 12 1A9 9 0 0 1 21 10Z"
stroke="currentColor"
@@ -82,81 +132,134 @@
{/if}
</div>
<div class="find-card-content">
<div class="find-card-header">
<h3 class="find-card-title">{title}</h3>
{#if category}
<Badge variant="secondary" class="category-badge">
{category}
</Badge>
{/if}
<!-- Post Actions -->
<div class="post-actions">
<div class="action-buttons">
<LikeButton findId={id} {isLiked} {likeCount} size="sm" />
<Button variant="ghost" size="sm" class="action-button">
<MessageCircle size={16} />
<span>comment</span>
</Button>
<Button variant="ghost" size="sm" class="action-button">
<Share size={16} />
<span>share</span>
</Button>
</div>
{#if description}
<p class="find-card-description">
{truncateText(description)}
</p>
{/if}
<div class="find-card-meta">
<div class="location-info">
{#if locationName}
<div class="location">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none">
<path
d="M21 10C21 17 12 23 12 23S3 17 3 10A9 9 0 0 1 12 1A9 9 0 0 1 21 10Z"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
<circle cx="12" cy="10" r="3" stroke="currentColor" stroke-width="2" />
</svg>
<span class="location-text">{locationName}</span>
</div>
{/if}
<div class="author">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none">
<path
d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
<circle cx="12" cy="7" r="4" stroke="currentColor" stroke-width="2" />
</svg>
<span class="author-text">@{user.username}</span>
</div>
</div>
<div class="like-container">
<LikeButton findId={id} {isLiked} {likeCount} size="sm" />
</div>
</div>
</div>
<div class="find-card-actions">
<Button variant="default" size="sm" onclick={handleExplore} class="explore-button">
explore this find
<Button variant="outline" size="sm" onclick={handleExplore} class="explore-button">
explore
</Button>
</div>
</div>
<style>
.find-card {
display: flex;
background: white;
align-items: flex-start;
padding-top: 1rem;
border-top: 1px solid #e5e7eb;
border: 1px solid hsl(var(--border));
border-radius: 12px;
overflow: hidden;
margin-bottom: 1rem;
transition: box-shadow 0.2s ease;
}
.find-card-media {
.find-card:hover {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
/* Post Header */
.post-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem 1rem 0.75rem 1rem;
}
.user-info {
display: flex;
align-items: center;
gap: 0.75rem;
}
:global(.avatar) {
width: 40px;
height: 40px;
}
:global(.avatar-fallback) {
background: hsl(var(--primary));
color: white;
font-size: 0.875rem;
font-weight: 600;
}
.user-details {
display: flex;
flex-direction: column;
gap: 0.125rem;
}
.username {
font-weight: 600;
font-size: 0.875rem;
color: hsl(var(--foreground));
}
.location {
display: flex;
align-items: center;
gap: 0.25rem;
font-size: 0.75rem;
color: hsl(var(--muted-foreground));
}
:global(.more-button) {
color: hsl(var(--muted-foreground));
}
/* Post Content */
.post-content {
padding: 0 1rem 0.75rem 1rem;
}
.content-header {
display: flex;
align-items: flex-start;
gap: 0.5rem;
margin-bottom: 0.5rem;
flex-wrap: wrap;
}
.post-title {
font-family: 'Washington', serif;
font-size: 1.125rem;
font-weight: 600;
line-height: 1.4;
color: hsl(var(--foreground));
margin: 0;
flex: 1;
}
:global(.category-badge) {
font-size: 0.75rem;
height: 1.5rem;
flex-shrink: 0;
width: 120px;
height: 80px;
border-radius: 8px;
}
.post-description {
font-size: 0.875rem;
line-height: 1.5;
color: hsl(var(--muted-foreground));
margin: 0;
}
/* Media */
.post-media {
width: 100%;
aspect-ratio: 16/10;
overflow: hidden;
background: hsl(var(--muted));
display: flex;
align-items: center;
justify-content: center;
}
.media-image {
@@ -176,106 +279,54 @@
display: flex;
align-items: center;
justify-content: center;
background: hsl(var(--muted));
}
.find-card-content {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.find-card-header {
display: flex;
align-items: flex-start;
gap: 0.75rem;
flex-wrap: wrap;
}
.find-card-title {
font-family: 'Washington', serif;
font-size: 1.125rem;
font-weight: 600;
line-height: 1.4;
color: hsl(var(--foreground));
margin: 0;
}
:global(.category-badge) {
font-size: 0.75rem;
height: 1.5rem;
}
.find-card-description {
font-size: 0.875rem;
line-height: 1.5;
color: hsl(var(--muted-foreground));
margin: 0;
opacity: 0.5;
}
.find-card-meta {
/* Post Actions */
.post-actions {
display: flex;
align-items: center;
justify-content: space-between;
align-items: center;
padding: 0.75rem 1rem 1rem 1rem;
border-top: 1px solid hsl(var(--border));
}
.like-container {
flex-shrink: 0;
}
.location-info {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.location,
.author {
.action-buttons {
display: flex;
align-items: center;
gap: 0.5rem;
}
:global(.action-button) {
gap: 0.375rem;
font-size: 0.75rem;
color: hsl(var(--muted-foreground));
font-size: 0.875rem;
}
.location-text,
.author-text {
font-weight: 500;
}
.find-card-actions {
flex-shrink: 0;
display: flex;
align-items: center;
:global(.action-button:hover) {
color: hsl(var(--foreground));
}
:global(.explore-button) {
white-space: nowrap;
font-weight: 500;
font-size: 0.875rem;
}
/* Mobile responsive */
@media (max-width: 768px) {
.find-card {
.post-actions {
flex-direction: column;
gap: 0.75rem;
align-items: stretch;
}
.find-card-media {
.action-buttons {
justify-content: space-around;
}
:global(.explore-button) {
width: 100%;
height: 120px;
}
.find-card-header {
flex-direction: column;
gap: 0.5rem;
align-items: flex-start;
}
.find-card-actions {
align-self: stretch;
}
}
</style>

View File

@@ -40,17 +40,13 @@
}
</script>
<section class="finds-list-section">
<div class="finds-header">
<h2 class="finds-title">{title}</h2>
<div class="finds-count">
{finds.length}
{finds.length === 1 ? 'find' : 'finds'}
</div>
<section class="finds-feed">
<div class="feed-header">
<h2 class="feed-title">{title}</h2>
</div>
{#if finds.length > 0}
<div class="finds-grid">
<div class="feed-container">
{#each finds as find (find.id)}
<FindCard
id={find.id}
@@ -70,8 +66,8 @@
<div class="empty-state">
<div class="empty-icon">
<svg
width="48"
height="48"
width="64"
height="64"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
@@ -81,25 +77,34 @@
<circle cx="12" cy="10" r="3" />
</svg>
</div>
<h3 class="empty-title">No finds yet</h3>
<h3 class="empty-title">No finds discovered yet</h3>
<p class="empty-message">{emptyMessage}</p>
<div class="empty-action">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none">
<path
d="M12 5v14M5 12h14"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
/>
</svg>
<span>Start exploring to discover finds</span>
</div>
</div>
{/if}
</section>
<style>
.finds-list-section {
.finds-feed {
width: 100%;
}
.finds-header {
display: flex;
justify-content: space-between;
align-items: center;
.feed-header {
margin-bottom: 1.5rem;
padding: 0 0.5rem;
}
.finds-title {
.feed-title {
font-family: 'Washington', serif;
font-size: 1.875rem;
font-weight: 700;
@@ -107,16 +112,10 @@
color: hsl(var(--foreground));
}
.finds-count {
font-size: 0.875rem;
color: hsl(var(--muted-foreground));
font-weight: 500;
}
.finds-grid {
.feed-container {
display: flex;
flex-direction: column;
gap: 1rem;
gap: 0;
}
.empty-state {
@@ -124,44 +123,79 @@
flex-direction: column;
align-items: center;
justify-content: center;
padding: 3rem 1rem;
padding: 4rem 2rem;
text-align: center;
background: hsl(var(--card));
border-radius: 12px;
border: 1px solid hsl(var(--border));
}
.empty-icon {
margin-bottom: 1rem;
margin-bottom: 1.5rem;
color: hsl(var(--muted-foreground));
opacity: 0.5;
opacity: 0.4;
}
.empty-title {
font-size: 1.125rem;
font-size: 1.25rem;
font-weight: 600;
margin: 0 0 0.5rem 0;
margin: 0 0 0.75rem 0;
color: hsl(var(--foreground));
}
.empty-message {
font-size: 0.875rem;
color: hsl(var(--muted-foreground));
margin: 0;
margin: 0 0 1.5rem 0;
line-height: 1.5;
}
.empty-action {
display: flex;
align-items: center;
gap: 0.5rem;
color: hsl(var(--primary));
font-size: 0.875rem;
font-weight: 500;
}
/* Mobile responsive */
@media (max-width: 768px) {
.finds-header {
.feed-header {
flex-direction: column;
align-items: flex-start;
gap: 0.5rem;
gap: 0.75rem;
margin-bottom: 1rem;
padding: 0;
}
.finds-title {
.feed-title {
font-size: 1.5rem;
}
.empty-state {
padding: 2rem 1rem;
padding: 3rem 1.5rem;
}
}
/* Smooth scrolling for feed */
.feed-container {
scroll-behavior: smooth;
}
/* Add subtle animation for new posts */
:global(.find-card) {
animation: fadeInUp 0.4s ease-out;
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>

View File

@@ -19,24 +19,29 @@
class: className = ''
}: Props = $props();
// Track the source of truth - server state
let serverIsLiked = $state(isLiked);
let serverLikeCount = $state(likeCount);
// Track optimistic state during loading
let isLoading = $state(false);
let currentIsLiked = $state(isLiked);
let currentLikeCount = $state(likeCount);
let optimisticIsLiked = $state(isLiked);
let optimisticLikeCount = $state(likeCount);
// Derived state for display
let displayIsLiked = $derived(isLoading ? optimisticIsLiked : serverIsLiked);
let displayLikeCount = $derived(isLoading ? optimisticLikeCount : serverLikeCount);
async function toggleLike() {
if (isLoading) return;
const previousLiked = currentIsLiked;
const previousCount = currentLikeCount;
// Optimistic update
currentIsLiked = !currentIsLiked;
currentLikeCount += currentIsLiked ? 1 : -1;
// Set optimistic state
optimisticIsLiked = !serverIsLiked;
optimisticLikeCount = serverLikeCount + (optimisticIsLiked ? 1 : -1);
isLoading = true;
try {
const method = currentIsLiked ? 'POST' : 'DELETE';
const method = optimisticIsLiked ? 'POST' : 'DELETE';
const response = await fetch(`/api/finds/${findId}/like`, {
method,
headers: {
@@ -45,16 +50,16 @@
});
if (!response.ok) {
const error = (await response.json()) as { message?: string };
throw new Error(error.message || 'Failed to update like');
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.message || `HTTP ${response.status}`);
}
// Success - optimistic update was correct
} catch (error: unknown) {
// Revert optimistic update on error
currentIsLiked = previousLiked;
currentLikeCount = previousCount;
const result = await response.json();
// Update server state with response
serverIsLiked = result.isLiked;
serverLikeCount = result.likeCount;
} catch (error: unknown) {
console.error('Error updating like:', error);
toast.error('Failed to update like. Please try again.');
} finally {
@@ -62,10 +67,10 @@
}
}
// Update internal state when props change
// Update server state when props change (from parent component)
$effect(() => {
currentIsLiked = isLiked;
currentLikeCount = likeCount;
serverIsLiked = isLiked;
serverLikeCount = likeCount;
});
</script>
@@ -77,19 +82,19 @@
disabled={isLoading}
>
<Heart
class="h-4 w-4 transition-all duration-200 {currentIsLiked
class="h-4 w-4 transition-all duration-200 {displayIsLiked
? 'scale-110 fill-red-500 text-red-500'
: 'text-gray-500 group-hover:scale-105 group-hover:text-red-400'} {isLoading
? 'animate-pulse'
: ''}"
/>
{#if currentLikeCount > 0}
{#if displayLikeCount > 0}
<span
class="text-sm font-medium transition-colors {currentIsLiked
class="text-sm font-medium transition-colors {displayIsLiked
? 'text-red-500'
: 'text-gray-500 group-hover:text-red-400'}"
>
{currentLikeCount}
{displayLikeCount}
</span>
{/if}
</Button>