Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1x 1x 1x 1x 1x 1x 1x 1x 3x 4x 4x 9x 9x 2x 2x 2x 2x 9x 1x 9x 9x 9x 9x 9x 9x 9x 9x 4x 1x 1x 1x 1x 1x 3x 3x 3x 6x 6x 3x 3x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 3x 9x 4x 9x 9x 12x 9x 48x 36x 9x 9x 9x 9x 3x 3x 3x 3x 9x | import React, { useEffect, useState } from 'react';
import {
MyEventStatus,
MyEventSummary,
} from '@/models/event';
import { useTranslation } from 'react-i18next';
import { useAuth } from '@/contexts/AuthContext';
import { listMyEvents } from '@/services/eventService';
import { ApiError } from '@/services/api';
import { listMyTickets } from '@/services/ticketService';
import type { TicketListItem } from '@/models/ticket';
import i18n from '@/i18n';
const STATUS_OPTIONS: Array<{ value: MyEventStatus; labelKey: string }> = [
{ value: 'ACTIVE', labelKey: 'events.status.ACTIVE' },
{ value: 'IN_PROGRESS', labelKey: 'events.status.IN_PROGRESS' },
{ value: 'COMPLETED', labelKey: 'events.status.COMPLETED' },
{ value: 'CANCELED', labelKey: 'events.status.CANCELED' },
];
function getEmptyStateCopy(status: MyEventStatus): { title: string; subtitle: string } {
return {
title: i18n.t(`myEvents.empty.${status}.title`),
subtitle: i18n.t(`myEvents.empty.${status}.subtitle`),
};
}
export interface MyEventsStatusTab {
value: MyEventStatus;
label: string;
count: number;
}
export interface MyEventsViewModel {
activeStatus: MyEventStatus;
statusTabs: MyEventsStatusTab[];
hostedEvents: MyEventSummary[];
attendedEvents: MyEventSummary[];
visibleEvents: MyEventSummary[];
hostedCount: number;
attendedCount: number;
isLoading: boolean;
errorMessage: string | null;
canRetry: boolean;
emptyTitle: string;
emptySubtitle: string;
setActiveStatus: (status: MyEventStatus) => void;
reload: () => Promise<void>;
}
function getTimeValue(value: string) {
const time = new Date(value).getTime();
return Number.isNaN(time) ? 0 : time;
}
function sortVisibleEvents(
events: MyEventSummary[],
status: MyEventStatus,
): MyEventSummary[] {
const sorted = [...events];
sorted.sort((first, second) => {
const firstTime = getTimeValue(first.start_time);
const secondTime = getTimeValue(second.start_time);
if (firstTime !== secondTime) {
return status === 'ACTIVE' || status === 'IN_PROGRESS'
? firstTime - secondTime
: secondTime - firstTime;
}
Iif (first.relation !== second.relation) {
return first.relation === 'HOSTING' ? -1 : 1;
}
return first.title.localeCompare(second.title);
});
return sorted;
}
export function useMyEventsViewModel(): MyEventsViewModel {
const { token } = useAuth();
// Subscribe to locale so tab labels and empty-state copy re-render on language change.
useTranslation();
const [activeStatus, setActiveStatus] = useState<MyEventStatus>('ACTIVE');
const [hostedEvents, setHostedEvents] = useState<MyEventSummary[]>([]);
const [attendedEvents, setAttendedEvents] = useState<MyEventSummary[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const reload = React.useCallback(async () => {
if (!token) {
setHostedEvents([]);
setAttendedEvents([]);
setErrorMessage(i18n.t('myEvents.errors.loginRequired'));
setIsLoading(false);
return;
}
setIsLoading(true);
setErrorMessage(null);
const withTimeout = <T>(promise: Promise<T>, timeoutMs = 10000): Promise<T> => {
return Promise.race([
promise,
new Promise<T>((_, reject) =>
setTimeout(() => reject(new Error('Request timed out')), timeoutMs)
),
]);
};
try {
const [eventsResponse, ticketsResponse] = await Promise.all([
withTimeout(listMyEvents(token)),
withTimeout(listMyTickets(token)).catch(() => ({ items: [] })),
]);
const ticketsByEventId = new Map<string, TicketListItem>();
for (const ticket of ticketsResponse?.items ?? []) {
if (ticket?.event?.id) {
ticketsByEventId.set(ticket.event.id, ticket);
}
}
const decoratedAttendedEvents = (eventsResponse?.attended_events ?? []).map((event) => {
const ticket = ticketsByEventId.get(event.id);
if (!ticket) return event;
const badges = event.badges.some((badge) => badge.type === 'TICKET')
? event.badges
: [...event.badges, { type: 'TICKET' as const, label: i18n.t('tickets.detail.title') }];
return {
...event,
ticket_id: ticket.ticket_id,
ticket_status: ticket.status,
badges,
};
});
const nextHostedEvents = eventsResponse?.hosted_events ?? [];
setHostedEvents(nextHostedEvents);
setAttendedEvents(decoratedAttendedEvents);
} catch (error) {
console.error('Failed to load events:', error);
setHostedEvents([]);
setAttendedEvents([]);
const message = error instanceof ApiError
? error.message
: error instanceof Error
? error.message
: i18n.t('myEvents.errors.loadFailed');
setErrorMessage(message);
} finally {
setIsLoading(false);
}
}, [token]);
useEffect(() => {
void reload();
}, [token]);
const allEvents = [...hostedEvents, ...attendedEvents];
const visibleEvents = sortVisibleEvents(
allEvents.filter((event) => event.status === activeStatus),
activeStatus,
);
const statusTabs = STATUS_OPTIONS.map((statusOption) => {
const count = allEvents.filter((event) => event.status === statusOption.value).length;
return {
value: statusOption.value,
label: i18n.t(statusOption.labelKey),
count,
};
});
const hasAnyEvents = allEvents.length > 0;
let emptyTitle = i18n.t('myEvents.noEventsTitle');
let emptySubtitle = i18n.t('myEvents.noEventsSubtitle');
if (hasAnyEvents) {
const currentStatus = activeStatus;
const copy = getEmptyStateCopy(currentStatus);
emptyTitle = copy.title;
emptySubtitle = copy.subtitle;
}
return {
activeStatus,
statusTabs,
hostedEvents,
attendedEvents,
visibleEvents,
hostedCount: hostedEvents.length,
attendedCount: attendedEvents.length,
isLoading,
errorMessage,
canRetry: Boolean(token),
emptyTitle,
emptySubtitle,
setActiveStatus,
reload,
};
}
|