1"use client";
2
3import type {
4 ComponentProps,
5 HTMLAttributes,
6 VideoHTMLAttributes,
7} from "react";
8import { useEffect, useRef } from "react";
9import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
10import {
11 Carousel,
12 CarouselContent,
13 CarouselItem,
14} from "@/components/ui/carousel";
15import { cn } from "@/lib/utils";
16
17export type StoriesProps = ComponentProps<typeof Carousel>;
18
19export const Stories = ({ className, opts, ...props }: StoriesProps) => (
20 <Carousel
21 className={cn("w-full", className)}
22 opts={{
23 align: "start",
24 loop: false,
25 dragFree: true,
26 ...opts,
27 }}
28 {...props}
29 />
30);
31
32export type StoriesContentProps = ComponentProps<typeof CarouselContent>;
33
34export const StoriesContent = ({
35 className,
36 ...props
37}: StoriesContentProps) => (
38 <CarouselContent className={cn("-ml-2 gap-2", className)} {...props} />
39);
40
41export type StoryProps = HTMLAttributes<HTMLDivElement>;
42
43export const Story = ({ className, ...props }: StoryProps) => (
44 <CarouselItem className={cn("basis-0 pl-2 md:pl-4", className)}>
45 <div
46 className={cn(
47 "group relative overflow-hidden rounded-xl bg-muted/40",
48 "cursor-pointer transition-all duration-200",
49 "hover:scale-[1.02] hover:shadow-lg",
50 "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
51 className
52 )}
53 role="button"
54 tabIndex={0}
55 {...props}
56 />
57 </CarouselItem>
58);
59
60export type StoryVideoProps = VideoHTMLAttributes<HTMLVideoElement>;
61
62const tRegex = /t=(\d+(?:\.\d+)?)/;
63
64export const StoryVideo = ({ className, ...props }: StoryVideoProps) => {
65 const videoRef = useRef<HTMLVideoElement>(null);
66 const initialTimeRef = useRef<number>(0);
67
68 // Parse the initial time from the src attribute (e.g., #t=20)
69 useEffect(() => {
70 const src = (props.src ?? "") as string;
71 let initialTime = 0;
72 if (typeof src === "string") {
73 const hashIndex = src.indexOf("#");
74 if (hashIndex !== -1) {
75 const hash = src.slice(hashIndex + 1);
76 // Look for t=number or t=start,end
77 const tMatch = hash.match(tRegex);
78 if (tMatch) {
79 initialTime = Number.parseFloat(tMatch[1]);
80 }
81 }
82 }
83 initialTimeRef.current = initialTime;
84 }, [props.src]);
85
86 const handleMouseOver = () => {
87 videoRef.current?.play();
88 };
89
90 const handleMouseOut = () => {
91 if (videoRef.current) {
92 videoRef.current.pause();
93 videoRef.current.currentTime = initialTimeRef.current;
94 }
95 };
96
97 const handleFocus = () => {
98 videoRef.current?.play();
99 };
100
101 const handleBlur = () => {
102 if (videoRef.current) {
103 videoRef.current.pause();
104 videoRef.current.currentTime = initialTimeRef.current;
105 }
106 };
107
108 return (
109 <video
110 className={cn(
111 "absolute inset-0 size-full object-cover",
112 "transition-opacity duration-200",
113 "group-hover:opacity-90",
114 className
115 )}
116 loop
117 muted
118 onBlur={handleBlur}
119 onFocus={handleFocus}
120 onMouseOut={handleMouseOut}
121 onMouseOver={handleMouseOver}
122 preload="metadata"
123 ref={videoRef}
124 tabIndex={0}
125 {...props}
126 />
127 );
128};
129
130export type StoryImageProps = ComponentProps<"img"> & {
131 alt: string;
132};
133
134export const StoryImage = ({ className, alt, ...props }: StoryImageProps) => (
135 // biome-ignore lint/performance/noImgElement: "Kibo UI is framework agnostic"
136 <img
137 alt={alt}
138 className={cn(
139 "absolute inset-0 h-full w-full object-cover",
140 "transition-opacity duration-200",
141 "group-hover:opacity-90",
142 className
143 )}
144 {...props}
145 />
146);
147
148export type StoryAuthorProps = HTMLAttributes<HTMLDivElement>;
149
150export const StoryAuthor = ({
151 className,
152 children,
153 ...props
154}: StoryAuthorProps) => (
155 <div
156 className={cn(
157 "absolute right-0 bottom-0 left-0 z-10",
158 "p-3 text-white",
159 className
160 )}
161 {...props}
162 >
163 <div className="flex items-center gap-2">{children}</div>
164 </div>
165);
166
167export type StoryAuthorImageProps = ComponentProps<typeof Avatar> & {
168 src?: string;
169 name?: string;
170 fallback?: string;
171};
172
173export const StoryAuthorImage = ({
174 src,
175 fallback,
176 name,
177 className,
178 ...props
179}: StoryAuthorImageProps) => (
180 <Avatar className={cn("size-6 border border-white/20", className)} {...props}>
181 {src && <AvatarImage alt={name} src={src} />}
182 <AvatarFallback className="bg-white/10 text-white text-xs">
183 {fallback || name?.charAt(0)?.toUpperCase()}
184 </AvatarFallback>
185 </Avatar>
186);
187
188export type StoryAuthorNameProps = HTMLAttributes<HTMLSpanElement>;
189
190export const StoryAuthorName = ({
191 className,
192 ...props
193}: StoryAuthorNameProps) => (
194 <span className={cn("truncate font-medium text-sm", className)} {...props} />
195);
196
197export type StoryTitleProps = HTMLAttributes<HTMLDivElement>;
198
199export const StoryTitle = ({ className, ...props }: StoryTitleProps) => (
200 <div
201 className={cn(
202 "absolute top-0 right-0 left-0 z-10",
203 "p-3 text-white",
204 className
205 )}
206 {...props}
207 />
208);
209
210export type StoryOverlayProps = HTMLAttributes<HTMLDivElement> & {
211 side?: "top" | "bottom";
212};
213
214export const StoryOverlay = ({
215 className,
216 side = "bottom",
217 ...props
218}: StoryOverlayProps) => {
219 const positionClasses =
220 side === "top" ? "top-0 bg-gradient-to-b" : "bottom-0 bg-gradient-to-t";
221
222 return (
223 <div
224 className={cn(
225 "absolute right-0 left-0 h-10 from-black/20 to-transparent",
226 positionClasses,
227 className
228 )}
229 {...props}
230 />
231 );
232};
233