-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate gallery game into its own component (#2738)
* separate gallery game into its own component fixes an issue where game image gets mixed up during search * add key to game component for react and remove dependency on filename in GalleryGame component
- Loading branch information
1 parent
4d1161b
commit 3014a78
Showing
2 changed files
with
73 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect, useState, useRef } from "preact/hooks"; | ||
import { loadThumbnailUrl } from "../lib/thumbnail"; | ||
|
||
type GalleryGameProps = { | ||
filename: string | ||
title: string | ||
author: string | ||
tags: string[] | ||
isNew: boolean | ||
show: boolean | ||
filter: any, | ||
setFilter: Function | ||
} | ||
|
||
export default function GalleryGame({ setFilter, filter, show, filename, title, author, tags, isNew }: GalleryGameProps) { | ||
const [thumbnailUrl, setThumbnailUrl] = useState<string | null>(null); | ||
const gameRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver(entries => { | ||
entries.forEach(async entry => { | ||
if (entry.isIntersecting) { | ||
const thumbnail = await loadThumbnailUrl(filename) | ||
setThumbnailUrl(thumbnail); | ||
} | ||
}) | ||
}, { threshold: 0.1 }); | ||
|
||
observer.observe(gameRef.current!); | ||
return () => { | ||
observer.unobserve(gameRef.current!); | ||
} | ||
}, []); | ||
|
||
if (!show) return null; | ||
return ( | ||
<div | ||
ref={gameRef} | ||
className="game" | ||
onClick={() => window.open(`/gallery/${filename}`, '_blank')} | ||
> | ||
{tags.includes("tutorial") ? ( | ||
<span class="badge tutorial">Tutorial</span> | ||
) : isNew ? ( | ||
<span class="badge new">New</span> | ||
) : null} | ||
|
||
{thumbnailUrl && <img src={thumbnailUrl} alt="thumbnail" data-loaded='true' /> } | ||
|
||
<h3>{title}</h3> | ||
<p class="author">by @{author}</p> | ||
<p class="tags" onClick={(e) => e.stopPropagation()}> | ||
|
||
{tags.map((tag) => | ||
<span class="game-tag" onClick={() => setFilter((_filter: any) => ({ ..._filter, tags: [...filter.tags, tag] }))}>#{tag} </span> | ||
)} | ||
</p> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters