Convert Header to Typescript

This commit is contained in:
T'JAMPENS QUENTIN p2406187
2026-03-18 20:53:45 +01:00
parent 5b4078e759
commit 5787f941fd
2 changed files with 29 additions and 21 deletions
@@ -12,29 +12,36 @@ import { EventContext } from "../../contexts/Events/EventContext.js";
import { AuthContext } from "../../contexts/Auth/AuthContext.js";
import { PendingMembersContext } from "../../contexts/PendingMembers/PendingMembersContext";
interface Notification {
id: number;
content: string;
created_at: string | number;
pivot: { unread: number };
}
function Header() {
const { update, user } = useContext(AuthContext);
const { updateEvent } = useContext(EventContext);
const [notificationMenu, setnotificationMenu] = useState(false);
const [unreadNotification, setunreadNotification] = useState(false);
const [mobileMenu, setMobileMenu] = useState(false);
const [notifications, setNotifications] = useState([]);
const notificationRef = useRef(null);
const { updatePendingMembers } = useContext(PendingMembersContext);
const { update, user } = useContext(AuthContext)!;
const { updateEvent } = useContext(EventContext)!;
const { updatePendingMembers } = useContext(PendingMembersContext) as unknown as { updatePendingMembers: () => void };
const [notificationMenu, setnotificationMenu] = useState<boolean>(false);
const [unreadNotification, setunreadNotification] = useState<boolean>(false);
const [mobileMenu, setMobileMenu] = useState<boolean>(false);
const [notifications, setNotifications] = useState<Notification[]>([]);
const notificationRef = useRef<HTMLDivElement>(null);
useEffect(() => {
updateEvent();
let echoInstance = null;
let channelsToLeave = [];
let echoInstance: ReturnType<typeof initEcho> | null = null;
let channelsToLeave: string[] = [];
async function initializeHeader() {
try {
const notifData = await getUserNotifications();
const data = notifData || [];
const data: Notification[] = notifData || [];
setNotifications(data);
const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1);
setunreadNotification(hasUnread);
@@ -42,11 +49,11 @@ function Header() {
const echo = initEcho();
echoInstance = echo;
const activeChannels = [
const activeChannels: (string | null)[] = [
userCreatedListener(echo, user, setNotifications, setunreadNotification, updatePendingMembers),
userNotificationsListener(echo, user, setNotifications, setunreadNotification),
];
channelsToLeave = activeChannels.filter(name => name !== null);
channelsToLeave = activeChannels.filter((name): name is string => name !== null);
} catch (err) {
console.error("Erreur initialisation Header:", err);
@@ -55,11 +62,11 @@ function Header() {
initializeHeader();
const handleClickOutside = (event) => {
const handleClickOutside = (event: MouseEvent) => {
if (
notificationRef.current &&
!notificationRef.current.contains(event.target) &&
!event.target.closest(`.${styles.bellBtn}`)
!notificationRef.current.contains(event.target as Node) &&
!(event.target as Element).closest(`.${styles.bellBtn}`)
) {
setnotificationMenu(false);
}
@@ -70,7 +77,7 @@ function Header() {
return () => {
document.removeEventListener("mousedown", handleClickOutside);
if (echoInstance) {
channelsToLeave.forEach(chan => echoInstance.leave(chan));
channelsToLeave.forEach(chan => echoInstance!.leave(chan));
}
};
}, []);
@@ -93,7 +100,7 @@ function Header() {
}
async function deleteNotification(notificationId) {
async function deleteNotification(notificationId: number) {
try {
const result = await deleteNotificationUser(notificationId);
if (result) {
+1
View File
@@ -16,6 +16,7 @@ export type EventGroup = {
export type EventContextType = {
events: EventGroup[];
updateEvent: () => void;
};
export const EventContext = createContext<EventContextType | null>(null);