summaryrefslogtreecommitdiff
path: root/src/lib/components/VideoCard.svelte
blob: 61bf9751de41286bc303a547b3bf42455ab737f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<script lang="ts">
  import { formatDuration, formatViews, getBestThumbnail, type VideoThumbnail } from '$lib/api/youtube';

  interface Props {
    videoId: string;
    title: string;
    author: string;
    authorId: string;
    thumbnails: VideoThumbnail[];
    viewCount: number;
    publishedText: string;
    lengthSeconds: number;
  }

  let {
    videoId,
    title,
    author,
    authorId,
    thumbnails,
    viewCount,
    publishedText,
    lengthSeconds
  }: Props = $props();

  const thumbnail = $derived(getBestThumbnail(thumbnails));
  const duration = $derived(formatDuration(lengthSeconds));
  const views = $derived(formatViews(viewCount));
</script>

<a href="/watch/{videoId}" class="video-card">
  <div class="thumbnail">
    <img src={thumbnail} alt={title} loading="lazy" />
    <span class="duration">{duration}</span>
  </div>
  <div class="info">
    <h3 class="title">{title}</h3>
    <span
      class="author"
      role="link"
      tabindex="0"
      onclick={(e) => { e.preventDefault(); e.stopPropagation(); window.location.href = `/channel/${authorId}`; }}
      onkeydown={(e) => { if (e.key === 'Enter') { e.preventDefault(); window.location.href = `/channel/${authorId}`; } }}
    >
      {author}
    </span>
    <div class="meta">
      <span>{views}</span>
      <span class="separator">•</span>
      <span>{publishedText}</span>
    </div>
  </div>
</a>

<style>
  .video-card {
    display: block;
    text-decoration: none;
    color: inherit;
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.15s ease;
  }

  .video-card:hover {
    transform: translateY(-2px);
  }

  .thumbnail {
    position: relative;
    aspect-ratio: 16 / 9;
    background: var(--bg-secondary);
    border-radius: 8px;
    overflow: hidden;
  }

  .thumbnail img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .duration {
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 500;
  }

  .info {
    padding: 0.75rem 0;
  }

  .title {
    font-size: 0.95rem;
    font-weight: 500;
    line-height: 1.3;
    margin: 0 0 0.5rem;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    color: var(--text-primary);
  }

  .author {
    font-size: 0.85rem;
    color: var(--text-secondary);
    text-decoration: none;
    display: block;
    margin-bottom: 0.25rem;
    cursor: pointer;
  }

  .author:hover {
    color: var(--text-primary);
  }

  .meta {
    font-size: 0.8rem;
    color: var(--text-muted);
  }

  .separator {
    margin: 0 0.25rem;
  }
</style>