11'use client'
22
3- import { useEffect , useRef , useState } from 'react'
3+ import { useState } from 'react'
44import { FaYoutube } from 'react-icons/fa'
55
66import AutoHeightImage from '@/components/AutoHeightImage'
77import { SlackMessageAttachmentLayout } from '@/components/Slack/SlackMessageItem/components/SlackMessageAttachment/SlackMessageAttachmentLayout'
88import { MessageYoutubeAttachmentItem } from '@/types/schema'
9+ import { useRefEffect } from '@toss/react'
910
1011interface SlackMessageYoutubeAttachmentProps {
1112 attachment : MessageYoutubeAttachmentItem
@@ -14,29 +15,26 @@ interface SlackMessageYoutubeAttachmentProps {
1415export const SlackMessageYoutubeAttachment = ( {
1516 attachment,
1617} : SlackMessageYoutubeAttachmentProps ) => {
17- const containerRef = useRef < HTMLDivElement > ( null )
1818 const [ playing , setPlaying ] = useState ( false )
1919
20- useEffect ( ( ) => {
21- const container = containerRef . current
22- if ( ! container ) {
23- return
24- }
20+ const containerRef = useRefEffect < HTMLDivElement > (
21+ ( container ) => {
22+ const handleResize = ( ) => {
23+ const aspectRatio = attachment . thumbWidth / attachment . thumbHeight
24+ const { width } = container . getBoundingClientRect ( )
25+ const height = Math . floor ( width / aspectRatio )
26+ container . style . height = `${ height } px`
27+ }
2528
26- const handleResize = ( ) => {
27- const aspectRatio = attachment . thumbWidth / attachment . thumbHeight
28- const { width } = container . getBoundingClientRect ( )
29- const height = Math . floor ( width / aspectRatio )
30- container . style . height = `${ height } px`
31- }
29+ window . addEventListener ( 'resize' , handleResize )
30+ handleResize ( )
3231
33- window . addEventListener ( 'resize' , handleResize )
34- handleResize ( )
35-
36- return ( ) => {
37- window . removeEventListener ( 'resize' , handleResize )
38- }
39- } , [ attachment . thumbWidth , attachment . thumbHeight ] )
32+ return ( ) => {
33+ window . removeEventListener ( 'resize' , handleResize )
34+ }
35+ } ,
36+ [ attachment . thumbWidth , attachment . thumbHeight ]
37+ )
4038
4139 return (
4240 < SlackMessageAttachmentLayout >
0 commit comments