-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ids to incoming bc messages + id clears
- Loading branch information
Showing
13 changed files
with
339 additions
and
163 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
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
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
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 |
---|---|---|
@@ -1,36 +1,81 @@ | ||
import {useEffect, useRef, useState} from 'react'; | ||
import ReactCanvasConfetti from 'react-canvas-confetti'; | ||
import {type ConfettiReactionEntry} from '../reactions/reaction'; | ||
|
||
function confettiProps(): confetti.Options { | ||
return { | ||
angle: 90, | ||
colors: [ | ||
'#26ccff', | ||
'#a25afd', | ||
'#ff5e7e', | ||
'#88ff5a', | ||
'#fcff42', | ||
'#ffa62d', | ||
'#ff36ff', | ||
], | ||
decay: 0.8, | ||
drift: 0, | ||
gravity: 1, | ||
origin: { | ||
x: Math.random(), | ||
y: Math.random(), | ||
}, | ||
particleCount: 500, | ||
scalar: 1, | ||
shapes: ['circle', 'square', 'star'], | ||
spread: 360, | ||
startVelocity: 45, | ||
ticks: 600, | ||
}; | ||
} | ||
|
||
export default function Confetti({ | ||
confettiReactions, | ||
clear, | ||
fire, | ||
}: { | ||
confettiReactions: ConfettiReactionEntry[]; | ||
clear?: any; | ||
fire?: any; | ||
}) { | ||
const [doneReactions, setDoneReactions] = useState<string[]>([]); | ||
const confettiRef = useRef<confetti.CreateTypes>(); | ||
useEffect(() => { | ||
for (const [id, confettiReaction] of confettiReactions) { | ||
if (doneReactions.includes(id)) { | ||
continue; | ||
} | ||
|
||
if (confettiReaction === 'confetti') { | ||
void confettiRef.current?.(confettiProps()); | ||
} | ||
|
||
setDoneReactions((currentDoneReactions) => [...currentDoneReactions, id]); | ||
} | ||
}, [confettiReactions, doneReactions]); | ||
|
||
useEffect(() => { | ||
if (clear) { | ||
console.log('clearing'); | ||
confettiRef.current?.reset(); | ||
} | ||
}, [clear]); | ||
|
||
useEffect(() => { | ||
if (fire) { | ||
void confettiRef.current?.(confettiProps()); | ||
} | ||
}, [fire]); | ||
|
||
export default function Confetti({fire, reset}: {fire: any; reset?: any}) { | ||
return ( | ||
<ReactCanvasConfetti | ||
resize | ||
useWorker | ||
fire={fire} // eslint-disable-line @typescript-eslint/no-unsafe-assignment | ||
reset={reset} // eslint-disable-line @typescript-eslint/no-unsafe-assignment | ||
angle={90} | ||
className="position-fixed top-0 left-0 w-screen h-screen pointer-events-none" | ||
colors={[ | ||
'#26ccff', | ||
'#a25afd', | ||
'#ff5e7e', | ||
'#88ff5a', | ||
'#fcff42', | ||
'#ffa62d', | ||
'#ff36ff', | ||
]} | ||
decay={0.8} | ||
drift={0} | ||
gravity={1} | ||
origin={{ | ||
x: Math.random(), | ||
y: Math.random(), | ||
refConfetti={(confetti) => { | ||
confettiRef.current = confetti ?? undefined; | ||
}} | ||
particleCount={500} | ||
scalar={1} | ||
shapes={['circle', 'square', 'star']} | ||
spread={360} | ||
startVelocity={45} | ||
ticks={600} | ||
className="position-fixed top-0 left-0 w-screen h-screen pointer-events-none" | ||
/> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,45 +1,33 @@ | ||
import {useCallback, useMemo} from 'react'; | ||
import {useCallback, useMemo, useState} from 'react'; | ||
import {type Handler, type Payload} from '../broadcast/use-channel-handlers'; | ||
import {type ConfettiReactionEntry} from '../reactions/reaction'; | ||
|
||
export default function useConfetti({ | ||
postMessage, | ||
onConfetti, | ||
onReset, | ||
}: { | ||
postMessage?: Handler; | ||
onConfetti?: (data: any) => void; | ||
onReset?: (data: any) => void; | ||
}): { | ||
export default function useConfetti({postMessage}: {postMessage?: Handler}): { | ||
postConfetti: () => void; | ||
postConfettiReset: () => void; | ||
handlers: Handler[]; | ||
confettiReactions: ConfettiReactionEntry[]; | ||
} { | ||
const postConfetti = useCallback(() => { | ||
postMessage?.({id: 'reaction', reaction: ['', 'confetti']}); | ||
postMessage?.({id: 'reactions', reactions: [['', 'confetti']]}); | ||
}, [postMessage]); | ||
|
||
const postConfettiReset = useCallback(() => { | ||
postMessage?.({id: 'reaction', reaction: ['', 'confetti clear']}); | ||
}, [postMessage]); | ||
const [confettiReactions, setConfettiReactions] = useState< | ||
ConfettiReactionEntry[] | ||
>([]); | ||
|
||
const handlers = useMemo<Handler[]>( | ||
() => [ | ||
(payload: Payload) => { | ||
if (payload.id === 'reaction' && payload.reaction[1] === 'confetti') { | ||
onConfetti?.({}); | ||
} | ||
}, | ||
(payload: Payload) => { | ||
if ( | ||
payload.id === 'reaction' && | ||
payload.reaction[1] === 'confetti clear' | ||
) { | ||
onReset?.({}); | ||
if (payload.id === 'reactions') { | ||
const confettiReactions = payload.reactions.filter( | ||
([, reaction]) => reaction === 'confetti', | ||
) as ConfettiReactionEntry[]; | ||
setConfettiReactions(confettiReactions); | ||
} | ||
}, | ||
], | ||
[onConfetti, onReset], | ||
[], | ||
); | ||
|
||
return {postConfetti, postConfettiReset, handlers}; | ||
return {postConfetti, handlers, confettiReactions}; | ||
} |
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
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
Oops, something went wrong.