import { useEffect, useMemo, useRef, useState } from 'react';
import { ZoomConstants } from '@/zoomConstants';
import { useLiveLessonContext } from '@/context/live-lesson/use-live-lesson-context';
import { jwtDecode } from '@/auth/utils';
import { useRouter } from 'next/router';
export const useZoom = () => {
const { query } = useRouter();
const [client, setClient] = useState<any>();
const [userDetail, setUserDetail] = useState({});
const [zoomIframeLoading, setZoomIframeLoading] = useState(false);
const zoomRef = useRef<any>();
const { setIsZoomStarted } = useLiveLessonContext();
function extractZoomDetails(url: string) {
try {
const urlObj = new URL(url);
const meetingNumber = urlObj.pathname.split('/j/')[1];
const password = urlObj.searchParams.get('pwd');
return {
meetingNumber: meetingNumber,
password: password,
};
} catch (error) {
console.error('Error extracting Zoom meeting details:', error);
return {
meetingNumber: '81313548127',
password: 'dGIy4oLB8gMTjVPdYWtcV2NMUCO8Hu.1',
};
}
}
const zoomDetail = extractZoomDetails(query.zoomLink as string);
const user = useMemo(() => {
const accessToken = localStorage.getItem('accessToken');
return jwtDecode(accessToken as never);
}, []);
const payload = {
sdkKey: ZoomConstants.sdkKey,
sdkSecret: ZoomConstants.sdkSecret,
meetingNumber: zoomDetail.meetingNumber,
role: 0,
userName: user.userId,
userEmail: user.email,
password: zoomDetail.password,
};
const initializeEmbeddedClient = async () => {
if (!client) {
return;
}
const meetingSDKElement = document.getElementById('meetingSDKElement');
try {
setZoomIframeLoading(true);
client.init({
zoomAppRoot: meetingSDKElement as never,
language: 'en-US',
patchJsMedia: true,
customize: {
video: {
viewSizes: true as never,
},
},
});
await client.join({
sdkKey: payload.sdkKey,
signature: await getZoomSignature(),
meetingNumber: payload.meetingNumber.toString(),
userName: payload.userName,
userEmail: payload.userEmail,
password: payload.password as never,
zak: '',
recordingToken: '',
});
setIsZoomStarted(true);
setZoomIframeLoading(false);
setupListener();
const user = await client.getCurrentUser();
setUserDetail(user as never);
} catch (error) {
console.error('Error joining Zoom meeting:', error);
}
};
const getZoomSignature = async () => {
try {
const response = await fetch('/api/zoom', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
meetingNumber: zoomDetail.meetingNumber,
role: 0,
}),
});
const data = await response.json();
return data.meetingSignature;
} catch (error) {
console.error('Error fetching Zoom signature:', error);
}
};
const setupListener = () => {
if (!client) {
return;
}
};
useEffect(() => {
const initilizeZoomClient = async () => {
if (typeof window !== 'undefined') {
const ZoomMtgEmbedded = await (await import('@zoom/meetingsdk/embedded')).default;
zoomRef.current = ZoomMtgEmbedded;
const zoomClient = ZoomMtgEmbedded.createClient();
setClient(zoomClient);
}
};
initilizeZoomClient();
}, []);
return {
initializeEmbeddedClient,
zoomIframeLoading,
};
};
how i pass my custom function to handle user leave or end meeting or give me another way to handle leave or end meeting