2024-03-01 20:42:21 +01:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
2026-01-28 16:51:27 +08:00
|
|
|
import directoriesData from '../data/5chan-directories.json';
|
2024-03-01 20:42:21 +01:00
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export interface DirectoriesMetadata {
|
2024-05-17 14:55:41 +02:00
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
createdAt: number;
|
|
|
|
|
updatedAt: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export interface DirectoryCommunity {
|
2024-03-01 20:42:21 +01:00
|
|
|
title?: string;
|
|
|
|
|
address: string;
|
2025-11-03 22:06:18 +01:00
|
|
|
nsfw?: boolean;
|
2024-03-01 20:42:21 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export interface DirectoriesData {
|
2025-11-03 22:24:31 +01:00
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
createdAt: number;
|
|
|
|
|
updatedAt: number;
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: DirectoryCommunity[];
|
2025-11-03 22:24:31 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export interface DirectoriesState {
|
|
|
|
|
communities: DirectoryCommunity[];
|
2025-02-20 15:51:29 +01:00
|
|
|
loading: boolean;
|
|
|
|
|
error: Error | null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-11 21:27:18 +08:00
|
|
|
const GITHUB_URL = 'https://raw.githubusercontent.com/bitsocialhq/lists/master/5chan-directories.json';
|
2026-01-28 16:51:27 +08:00
|
|
|
const LOCALSTORAGE_KEY = '5chan-directories-cache';
|
|
|
|
|
const LOCALSTORAGE_TIMESTAMP_KEY = '5chan-directories-cache-timestamp';
|
2025-11-03 22:24:31 +01:00
|
|
|
const CACHE_MAX_AGE_MS = 60 * 60 * 1000; // 1 hour
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
let cacheCommunities: DirectoryCommunity[] | null = null;
|
|
|
|
|
let cacheMetadata: DirectoriesMetadata | null = null;
|
2024-03-01 20:42:21 +01:00
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
const getFromLocalStorage = (): DirectoriesData | null => {
|
2025-11-03 22:24:31 +01:00
|
|
|
try {
|
|
|
|
|
const cached = localStorage.getItem(LOCALSTORAGE_KEY);
|
|
|
|
|
const timestamp = localStorage.getItem(LOCALSTORAGE_TIMESTAMP_KEY);
|
|
|
|
|
if (cached && timestamp) {
|
|
|
|
|
const age = Date.now() - parseInt(timestamp, 10);
|
|
|
|
|
if (age < CACHE_MAX_AGE_MS) {
|
|
|
|
|
return JSON.parse(cached);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('Failed to read from localStorage:', e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
const saveToLocalStorage = (data: DirectoriesData) => {
|
2025-11-03 22:24:31 +01:00
|
|
|
try {
|
|
|
|
|
localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(data));
|
|
|
|
|
localStorage.setItem(LOCALSTORAGE_TIMESTAMP_KEY, Date.now().toString());
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('Failed to save to localStorage:', e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
const fetchDirectoriesData = async (): Promise<DirectoriesData> => {
|
2025-11-03 22:24:31 +01:00
|
|
|
try {
|
|
|
|
|
const response = await fetch(GITHUB_URL);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
// Save successful fetch to localStorage
|
|
|
|
|
saveToLocalStorage(data);
|
|
|
|
|
return data;
|
|
|
|
|
} catch (e) {
|
2026-01-28 16:51:27 +08:00
|
|
|
console.warn('Failed to fetch directories from GitHub, using vendored fallback:', e);
|
2025-11-03 22:24:31 +01:00
|
|
|
// Fall back to vendored file
|
2026-01-28 16:51:27 +08:00
|
|
|
return directoriesData as DirectoriesData;
|
2025-11-03 22:24:31 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export const useDirectories = () => {
|
2025-11-25 22:24:48 +01:00
|
|
|
// Use vendored data as initial state to prevent theme flash on first load
|
|
|
|
|
// This ensures NSFW status is known synchronously before first render
|
2026-01-28 16:51:27 +08:00
|
|
|
const [state, setState] = useState<DirectoriesState>({
|
|
|
|
|
communities: (directoriesData as DirectoriesData).communities,
|
2025-02-20 15:51:29 +01:00
|
|
|
loading: true,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
2024-03-01 20:42:21 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-01-28 16:51:27 +08:00
|
|
|
if (cacheCommunities) {
|
2025-02-20 15:51:29 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: cacheCommunities,
|
2025-02-20 15:51:29 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
2024-03-01 20:42:21 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2025-02-20 15:51:29 +01:00
|
|
|
|
2025-11-04 14:59:22 +01:00
|
|
|
let isMounted = true;
|
|
|
|
|
|
2024-03-01 20:42:21 +01:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
2025-11-03 22:24:31 +01:00
|
|
|
// Check localStorage first
|
|
|
|
|
const cachedData = getFromLocalStorage();
|
|
|
|
|
if (cachedData) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = cachedData.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2025-11-03 22:24:31 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: cachedData.communities,
|
2025-11-03 22:24:31 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
2025-11-04 14:59:22 +01:00
|
|
|
}
|
|
|
|
|
// Still try to fetch fresh data in background (don't await)
|
2026-01-28 16:51:27 +08:00
|
|
|
fetchDirectoriesData()
|
2025-11-04 14:59:22 +01:00
|
|
|
.then((data) => {
|
|
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = data.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: data.communities,
|
2025-11-04 14:59:22 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.warn('Background fetch failed:', e);
|
|
|
|
|
});
|
2025-11-03 22:24:31 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2025-02-20 15:51:29 +01:00
|
|
|
|
2025-11-03 22:24:31 +01:00
|
|
|
// No cache, fetch fresh data
|
2026-01-28 16:51:27 +08:00
|
|
|
const directories = await fetchDirectoriesData();
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = directories.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: directories.communities,
|
2025-11-04 14:59:22 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-01 20:42:21 +01:00
|
|
|
} catch (e) {
|
2026-01-28 16:51:27 +08:00
|
|
|
console.warn('Failed to load directories:', e);
|
2025-11-03 22:24:31 +01:00
|
|
|
// Fallback to vendored data
|
2026-01-28 16:51:27 +08:00
|
|
|
const fallbackData = directoriesData as DirectoriesData;
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = fallbackData.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: fallbackData.communities,
|
2025-11-04 14:59:22 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-01 20:42:21 +01:00
|
|
|
}
|
|
|
|
|
})();
|
2025-11-04 14:59:22 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted = false;
|
|
|
|
|
};
|
2024-03-01 20:42:21 +01:00
|
|
|
}, []);
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
// Always prefer cacheCommunities (module-level, stable reference) when available
|
|
|
|
|
// Only use state.communities during initial load before cache is populated
|
2025-11-07 12:49:58 +01:00
|
|
|
// This ensures a stable reference for memoization in consuming hooks
|
2026-01-28 16:51:27 +08:00
|
|
|
return cacheCommunities || state.communities;
|
2025-02-20 15:51:29 +01:00
|
|
|
};
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export const useDirectoriesState = () => {
|
2025-11-25 22:24:48 +01:00
|
|
|
// Use vendored data as fallback to prevent theme flash on first load
|
2026-01-28 16:51:27 +08:00
|
|
|
const [state, setState] = useState<DirectoriesState>({
|
|
|
|
|
communities: cacheCommunities || (directoriesData as DirectoriesData).communities,
|
|
|
|
|
loading: !cacheCommunities,
|
2025-02-20 15:51:29 +01:00
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-01-28 16:51:27 +08:00
|
|
|
if (cacheCommunities) {
|
2025-02-20 15:51:29 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: cacheCommunities,
|
2025-02-20 15:51:29 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 14:59:22 +01:00
|
|
|
let isMounted = true;
|
|
|
|
|
|
2025-02-20 15:51:29 +01:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
2025-11-03 22:24:31 +01:00
|
|
|
// Check localStorage first
|
|
|
|
|
const cachedData = getFromLocalStorage();
|
|
|
|
|
if (cachedData) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = cachedData.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2025-11-03 22:24:31 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: cachedData.communities,
|
2025-11-03 22:24:31 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
2025-11-04 14:59:22 +01:00
|
|
|
}
|
|
|
|
|
// Still try to fetch fresh data in background (don't await)
|
2026-01-28 16:51:27 +08:00
|
|
|
fetchDirectoriesData()
|
2025-11-04 14:59:22 +01:00
|
|
|
.then((data) => {
|
|
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = data.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: data.communities,
|
2025-11-04 14:59:22 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.warn('Background fetch failed:', e);
|
|
|
|
|
});
|
2025-11-03 22:24:31 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2025-02-20 15:51:29 +01:00
|
|
|
|
2025-11-03 22:24:31 +01:00
|
|
|
// No cache, fetch fresh data
|
2026-01-28 16:51:27 +08:00
|
|
|
const directories = await fetchDirectoriesData();
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = directories.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: directories.communities,
|
2025-11-04 14:59:22 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-02-20 15:51:29 +01:00
|
|
|
} catch (e) {
|
2026-01-28 16:51:27 +08:00
|
|
|
console.warn('Failed to load directories:', e);
|
2025-11-03 22:24:31 +01:00
|
|
|
// Fallback to vendored data
|
2026-01-28 16:51:27 +08:00
|
|
|
const fallbackData = directoriesData as DirectoriesData;
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
cacheCommunities = fallbackData.communities;
|
2025-11-04 14:59:22 +01:00
|
|
|
setState({
|
2026-01-28 16:51:27 +08:00
|
|
|
communities: fallbackData.communities,
|
2025-11-04 14:59:22 +01:00
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-02-20 15:51:29 +01:00
|
|
|
}
|
|
|
|
|
})();
|
2025-11-04 14:59:22 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted = false;
|
|
|
|
|
};
|
2025-02-20 15:51:29 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return state;
|
2024-03-01 20:42:21 +01:00
|
|
|
};
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export const useDirectoryAddresses = () => {
|
|
|
|
|
const directories = useDirectories();
|
|
|
|
|
return useMemo(() => directories.map((community) => community.address), [directories]);
|
2024-03-01 20:42:21 +01:00
|
|
|
};
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
export const useDirectoriesMetadata = () => {
|
|
|
|
|
const [metadata, setMetadata] = useState<DirectoriesMetadata | null>(null);
|
2024-05-17 14:55:41 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (cacheMetadata) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-11-04 14:59:22 +01:00
|
|
|
|
|
|
|
|
let isMounted = true;
|
|
|
|
|
|
2024-05-17 14:55:41 +02:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
2025-11-03 22:24:31 +01:00
|
|
|
// Check localStorage first
|
|
|
|
|
const cachedData = getFromLocalStorage();
|
|
|
|
|
if (cachedData) {
|
2026-01-28 16:51:27 +08:00
|
|
|
const metadata: DirectoriesMetadata = {
|
2025-11-03 22:24:31 +01:00
|
|
|
title: cachedData.title,
|
|
|
|
|
description: cachedData.description,
|
|
|
|
|
createdAt: cachedData.createdAt,
|
|
|
|
|
updatedAt: cachedData.updatedAt,
|
|
|
|
|
};
|
|
|
|
|
cacheMetadata = metadata;
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
|
|
|
|
setMetadata(metadata);
|
|
|
|
|
}
|
2025-11-03 22:24:31 +01:00
|
|
|
// Still try to fetch fresh data in background (don't await)
|
2026-01-28 16:51:27 +08:00
|
|
|
fetchDirectoriesData()
|
2025-11-04 14:59:22 +01:00
|
|
|
.then((data) => {
|
|
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
const freshMetadata: DirectoriesMetadata = {
|
2025-11-04 14:59:22 +01:00
|
|
|
title: data.title,
|
|
|
|
|
description: data.description,
|
|
|
|
|
createdAt: data.createdAt,
|
|
|
|
|
updatedAt: data.updatedAt,
|
|
|
|
|
};
|
|
|
|
|
cacheMetadata = freshMetadata;
|
|
|
|
|
setMetadata(freshMetadata);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.warn('Background metadata fetch failed:', e);
|
|
|
|
|
});
|
2025-11-03 22:24:31 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No cache, fetch fresh data
|
2026-01-28 16:51:27 +08:00
|
|
|
const directories = await fetchDirectoriesData();
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
const metadata: DirectoriesMetadata = {
|
|
|
|
|
title: directories.title,
|
|
|
|
|
description: directories.description,
|
|
|
|
|
createdAt: directories.createdAt,
|
|
|
|
|
updatedAt: directories.updatedAt,
|
2025-11-04 14:59:22 +01:00
|
|
|
};
|
|
|
|
|
cacheMetadata = metadata;
|
|
|
|
|
setMetadata(metadata);
|
|
|
|
|
}
|
2024-05-17 14:55:41 +02:00
|
|
|
} catch (e) {
|
2025-11-03 22:24:31 +01:00
|
|
|
console.warn('Failed to load metadata, using vendored fallback:', e);
|
|
|
|
|
// Fallback to vendored data
|
2026-01-28 16:51:27 +08:00
|
|
|
const fallbackData = directoriesData as DirectoriesData;
|
2025-11-04 14:59:22 +01:00
|
|
|
if (isMounted) {
|
2026-01-28 16:51:27 +08:00
|
|
|
const metadata: DirectoriesMetadata = {
|
2025-11-04 14:59:22 +01:00
|
|
|
title: fallbackData.title,
|
|
|
|
|
description: fallbackData.description,
|
|
|
|
|
createdAt: fallbackData.createdAt,
|
|
|
|
|
updatedAt: fallbackData.updatedAt,
|
|
|
|
|
};
|
|
|
|
|
cacheMetadata = metadata;
|
|
|
|
|
setMetadata(metadata);
|
|
|
|
|
}
|
2024-05-17 14:55:41 +02:00
|
|
|
}
|
|
|
|
|
})();
|
2025-11-04 14:59:22 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted = false;
|
|
|
|
|
};
|
2024-05-17 14:55:41 +02:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return cacheMetadata || metadata;
|
|
|
|
|
};
|