Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add particles to category cards #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/(pages)/explore/categories/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MAX_CONTENT_WIDTH } from '../../../utils/constants'
import { fetchTokenCategoryPages } from '../../../../contentful/tokenCategoryPage'
import DataDisclaimer from '../../../components/explore/DataDisclaimer'
import ExploreCategories from '../../../components/explore/ExploreCategories'
import { fetchContentfulTokenPages } from '../../../../contentful/tokenPage'

export const metadata: Metadata = {
title: 'Explore Listed Token Categories on Mango Markets',
Expand All @@ -17,12 +18,19 @@ function ExploreCategoriesFallback() {
}

async function ExploreCategoriesPage() {
const categoryPages = await fetchTokenCategoryPages({
const categoryPagesPromise = fetchTokenCategoryPages({
preview: draftMode().isEnabled,
})
const tokensPromise = fetchContentfulTokenPages({
preview: draftMode().isEnabled,
})
const [categoryPages, tokens] = await Promise.all([
categoryPagesPromise,
tokensPromise,
])
return categoryPages && categoryPages?.length ? (
<Suspense fallback={<ExploreCategoriesFallback />}>
<ExploreCategories categoryPages={categoryPages} />
<ExploreCategories categoryPages={categoryPages} tokens={tokens} />
<div className={`px-6 lg:px-20 ${MAX_CONTENT_WIDTH} mx-auto pb-10`}>
<DataDisclaimer />
</div>
Expand Down
115 changes: 115 additions & 0 deletions app/components/explore/CategoryTokenParticles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React, { useEffect, useState } from 'react'
import Particles from 'react-tsparticles'
import { Engine } from 'tsparticles-engine'

const createEmitters = (tokenSymbols: string[]) => {
return [
{
autoPlay: true,
fill: true,
life: {
wait: true,
duration: 1,
},
rate: {
quantity: 1,
delay: { min: 6, max: 12 },
},
startCount: 0,
particles: {
shape: {
type: 'images',
options: {
images: tokenSymbols.map((sym) => ({
src: `/icons/tokens/${sym.toLowerCase()}.svg`,
width: 48,
height: 48,
})),
},
},
rotate: {
value: 0,
random: true,
direction: 'clockwise',
animation: {
enable: true,
speed: 15,
sync: false,
},
},
lineLinked: {
enable: false,
},
opacity: {
value: 0.4,
},
size: {
value: 16,
random: false,
},
move: {
speed: 1,
random: false,
outMode: 'destroy',
},
},
position: {
y: 0,
},
},
]
}

const CategoryTokenParticles = ({
tokenSymbols,
id,
}: {
tokenSymbols: string[]
id: string
}) => {
const [mounted, setMounted] = useState(false)
const [emitters, setEmitters] = useState<any>()

useEffect(() => {
setMounted(true)
setEmitters(createEmitters(tokenSymbols))
}, [])

// Delay rendering until the library is fully initialized
if (!mounted) return null

// const emitters = createEmitters(tokenSymbols)

return (
<Particles
id={`tsparticles-${id}`}
init={async (engine: Engine) => {
// On library initialization
console.log('Particles library initialized:', engine)
}}
options={{
fullScreen: false,
particles: {
move: {
angle: 10,
attract: {
rotate: {
x: 600,
y: 1200,
},
},
direction: 'bottom',
enable: true,
speed: 0.1,
},
opacity: {
value: 0,
},
},
emitters: emitters,
}}
/>
)
}

export default CategoryTokenParticles
34 changes: 28 additions & 6 deletions app/components/explore/ExploreCategories.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use client'

import { MagnifyingGlassIcon } from '@heroicons/react/20/solid'
import { ChangeEvent, useMemo, useState } from 'react'
import { ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react'
import Input from '../forms/Input'
import { TokenCategoryPage } from '../../../contentful/tokenCategoryPage'
import { MAX_CONTENT_WIDTH } from '../../utils/constants'
import Link from 'next/link'
import NoResults from './NoResults'
import PageHeader from './PageHeader'
import CardImage from '../shared/CardImage'
import { TokenPage } from '../../../contentful/tokenPage'
import CategoryTokenParticles from './CategoryTokenParticles'
import { tsParticles } from 'tsparticles-engine'
import { loadFull } from 'tsparticles'

const generateSearchTerm = (item: TokenCategoryPage, searchValue: string) => {
const normalizedSearchValue = searchValue.toLowerCase()
Expand Down Expand Up @@ -39,8 +42,10 @@ const startSearch = (items: TokenCategoryPage[], searchValue: string) => {

const ExploreCategories = ({
categoryPages,
tokens,
}: {
categoryPages: TokenCategoryPage[]
tokens: TokenPage[]
}) => {
const [searchString, setSearchString] = useState('')

Expand All @@ -54,6 +59,14 @@ const ExploreCategories = ({
setSearchString(e.target.value)
}

const particlesInit = useCallback(async () => {
await loadFull(tsParticles)
}, [])

useEffect(() => {
particlesInit()
}, [particlesInit])

return (
<>
<PageHeader title="Explore listed token categories" />
Expand All @@ -76,19 +89,28 @@ const ExploreCategories = ({
<div className="grid grid-cols-4 gap-6">
{filteredCategories.map((cat) => {
const { category, slug } = cat
const categoryTokenSymbols = tokens
?.filter((token) => token.tags.includes(category))
.map((t) => t.symbol)
return (
<div
className="col-span-4 sm:col-span-2 lg:col-span-1 border border-th-bkg-3 rounded-xl group relative"
key={slug}
>
<Link href={`/explore/categories/${slug}`}>
<div className="overflow-hidden rounded-t-xl">
<CardImage
customImagePath={`/images/categories/${slug}-small.png`}
<div className="overflow-hidden rounded-t-xl bg-[url('/images/categories/placeholder.png')] bg-cover bg-center">
<CategoryTokenParticles
tokenSymbols={categoryTokenSymbols}
id={slug}
/>
</div>
<div className="px-4 py-3">
<div className="px-4 py-3 flex items-center justify-between">
<p className="text-th-fgd-2 font-display">{category}</p>
{categoryTokenSymbols?.length ? (
<p className="text-th-fgd-4 text-sm">
{categoryTokenSymbols.length}
</p>
) : null}
</div>
</Link>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/components/shared/CardImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CardImage = ({ customImagePath }: { customImagePath?: string }) => {
? '/images/new/cube-bg.png'
: '/images/new/cube-bg-light.png'

if (!mounted) return <div className="h-[200px] lg:h-[140px] bg-th-bkg-2" />
if (!mounted) return <div className="h-[200px] lg:h-[180px] bg-th-bkg-2" />
return (
<div
className={`h-[200px] lg:h-[180px] ${
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
"react-flip-numbers": "^3.0.8",
"react-i18next": "^13.5.0",
"react-slick": "^0.30.1",
"react-tsparticles": "2.2.4",
"recharts": "^2.10.0",
"slick-carousel": "^1.8.1"
"slick-carousel": "^1.8.1",
"tsparticles": "2.2.4"
},
"devDependencies": {
"@testing-library/react": "^14.1.2",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.tsbuildinfo

Large diffs are not rendered by default.

Loading
Loading