fix(frontend): resolve lint blockers for gitea frontend-ci
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { renderHook, act } from '@testing-library/react';
|
||||
import { useActiveClub } from '../useActiveClub';
|
||||
import type { Session } from 'next-auth';
|
||||
|
||||
const mockUseSession = vi.fn();
|
||||
|
||||
@@ -33,15 +32,15 @@ describe('useActiveClub', () => {
|
||||
status: 'authenticated',
|
||||
});
|
||||
|
||||
(localStorage.getItem as any).mockImplementation((key: string) => {
|
||||
(localStorage.getItem as ReturnType<typeof vi.fn>).mockImplementation((key: string) => {
|
||||
return localStorageData[key] || null;
|
||||
});
|
||||
|
||||
(localStorage.setItem as any).mockImplementation((key: string, value: string) => {
|
||||
(localStorage.setItem as ReturnType<typeof vi.fn>).mockImplementation((key: string, value: string) => {
|
||||
localStorageData[key] = value;
|
||||
});
|
||||
|
||||
(localStorage.clear as any).mockImplementation(() => {
|
||||
(localStorage.clear as ReturnType<typeof vi.fn>).mockImplementation(() => {
|
||||
localStorageData = {};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
'use client';
|
||||
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useMemo } from 'react';
|
||||
|
||||
const ACTIVE_CLUB_KEY = 'activeClubId';
|
||||
|
||||
function getStoredClubId(): string | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
return localStorage.getItem(ACTIVE_CLUB_KEY);
|
||||
}
|
||||
|
||||
function determineActiveId(clubs: Record<string, string> | undefined, currentId: string | null): string | null {
|
||||
if (!clubs || Object.keys(clubs).length === 0) return null;
|
||||
|
||||
const stored = getStoredClubId();
|
||||
if (stored && clubs[stored]) return stored;
|
||||
|
||||
if (currentId && clubs[currentId]) return currentId;
|
||||
|
||||
return Object.keys(clubs)[0];
|
||||
}
|
||||
|
||||
export interface ActiveClubData {
|
||||
activeClubId: string | null;
|
||||
role: string | null;
|
||||
@@ -14,23 +30,13 @@ export interface ActiveClubData {
|
||||
|
||||
export function useActiveClub(): ActiveClubData {
|
||||
const { data: session, status } = useSession();
|
||||
const [activeClubId, setActiveClubIdState] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (status === 'authenticated' && session?.user?.clubs) {
|
||||
const clubs = session.user.clubs;
|
||||
const storedClubId = localStorage.getItem(ACTIVE_CLUB_KEY);
|
||||
|
||||
if (storedClubId && clubs[storedClubId]) {
|
||||
setActiveClubIdState(storedClubId);
|
||||
} else {
|
||||
const firstClubId = Object.keys(clubs)[0];
|
||||
if (firstClubId) {
|
||||
setActiveClubIdState(firstClubId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [session, status]);
|
||||
|
||||
const [activeClubId, setActiveClubIdState] = useState<string | null>(getStoredClubId);
|
||||
|
||||
const computedActiveId = useMemo(() => {
|
||||
if (status !== 'authenticated' || !session?.user?.clubs) return activeClubId;
|
||||
return determineActiveId(session.user.clubs, activeClubId);
|
||||
}, [session, status, activeClubId]);
|
||||
|
||||
const setActiveClub = (clubId: string) => {
|
||||
if (session?.user?.clubs && session.user.clubs[clubId]) {
|
||||
@@ -40,10 +46,10 @@ export function useActiveClub(): ActiveClubData {
|
||||
};
|
||||
|
||||
const clubs = session?.user?.clubs || null;
|
||||
const role = activeClubId && clubs ? clubs[activeClubId] : null;
|
||||
const role = computedActiveId && clubs ? clubs[computedActiveId] : null;
|
||||
|
||||
return {
|
||||
activeClubId,
|
||||
activeClubId: computedActiveId,
|
||||
role,
|
||||
clubs,
|
||||
setActiveClub,
|
||||
|
||||
Reference in New Issue
Block a user