Files
5chan/src/hooks/use-default-subplebbits.ts
T

150 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-03-01 20:42:21 +01:00
import { useEffect, useMemo, useState } from 'react';
export interface MultisubMetadata {
title: string;
description: string;
createdAt: number;
updatedAt: number;
}
2024-05-04 16:56:49 +02:00
export interface MultisubSubplebbit {
2024-03-01 20:42:21 +01:00
title?: string;
address: string;
nsfw?: boolean;
2024-03-01 20:42:21 +01:00
}
2025-02-20 15:51:29 +01:00
export interface DefaultSubplebbitsState {
subplebbits: MultisubSubplebbit[];
loading: boolean;
error: Error | null;
}
let cacheSubplebbits: MultisubSubplebbit[] | null = null;
let cacheMetadata: MultisubMetadata | null = null;
2024-03-01 20:42:21 +01:00
export const useDefaultSubplebbits = () => {
2025-02-20 15:51:29 +01:00
const [state, setState] = useState<DefaultSubplebbitsState>({
subplebbits: [],
loading: true,
error: null,
});
2024-03-01 20:42:21 +01:00
useEffect(() => {
if (cacheSubplebbits) {
2025-02-20 15:51:29 +01:00
setState({
subplebbits: cacheSubplebbits,
loading: false,
error: null,
});
2024-03-01 20:42:21 +01:00
return;
}
2025-02-20 15:51:29 +01:00
2024-03-01 20:42:21 +01:00
(async () => {
try {
const multisub = await fetch('https://raw.githubusercontent.com/plebbit/lists/master/5chan-multisub.json').then((res) => {
2025-02-20 15:51:29 +01:00
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}
return res.json();
});
cacheSubplebbits = multisub.subplebbits;
2025-02-20 15:51:29 +01:00
setState({
subplebbits: multisub.subplebbits,
2025-02-20 15:51:29 +01:00
loading: false,
error: null,
});
2024-03-01 20:42:21 +01:00
} catch (e) {
console.warn(e);
2025-02-20 15:51:29 +01:00
setState((prev) => ({
...prev,
loading: false,
error: e instanceof Error ? e : new Error('Failed to fetch subplebbits'),
}));
2024-03-01 20:42:21 +01:00
}
})();
}, []);
2025-02-20 15:51:29 +01:00
// To maintain backward compatibility, return the subplebbits array directly
return cacheSubplebbits || state.subplebbits;
};
export const useDefaultSubplebbitsState = () => {
const [state, setState] = useState<DefaultSubplebbitsState>({
subplebbits: cacheSubplebbits || [],
loading: !cacheSubplebbits,
error: null,
});
useEffect(() => {
if (cacheSubplebbits) {
setState({
subplebbits: cacheSubplebbits,
loading: false,
error: null,
});
return;
}
(async () => {
try {
const multisub = await fetch('https://raw.githubusercontent.com/plebbit/lists/master/5chan-multisub.json').then((res) => {
2025-02-20 15:51:29 +01:00
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}
return res.json();
});
cacheSubplebbits = multisub.subplebbits;
2025-02-20 15:51:29 +01:00
setState({
subplebbits: multisub.subplebbits,
loading: false,
error: null,
});
} catch (e) {
console.warn(e);
setState((prev) => ({
...prev,
loading: false,
error: e instanceof Error ? e : new Error('Failed to fetch subplebbits'),
}));
}
})();
}, []);
return state;
2024-03-01 20:42:21 +01:00
};
export const useDefaultSubplebbitAddresses = () => {
const defaultSubplebbits = useDefaultSubplebbits();
return useMemo(() => defaultSubplebbits.map((subplebbit) => subplebbit.address), [defaultSubplebbits]);
2024-03-01 20:42:21 +01:00
};
export const useMultisubMetadata = () => {
const [metadata, setMetadata] = useState<MultisubMetadata | null>(null);
useEffect(() => {
if (cacheMetadata) {
return;
}
(async () => {
try {
const multisub = await fetch(
'https://raw.githubusercontent.com/plebbit/lists/master/5chan-multisub.json',
// { cache: 'no-cache' }
).then((res) => res.json());
const { title, description, createdAt, updatedAt } = multisub;
const metadata: MultisubMetadata = { title, description, createdAt, updatedAt };
cacheMetadata = metadata;
setMetadata(metadata);
} catch (e) {
console.warn(e);
}
})();
}, []);
return cacheMetadata || metadata;
};