2024-03-01 20:42:21 +01:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
2024-05-17 14:55:41 +02:00
|
|
|
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;
|
|
|
|
|
tags?: string[];
|
|
|
|
|
features?: string[];
|
2025-10-17 23:30:35 +02:00
|
|
|
autoSubscribe5Chan?: boolean;
|
2025-02-28 22:42:48 +01:00
|
|
|
lowUptime?: 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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-17 14:55:41 +02:00
|
|
|
let cacheSubplebbits: MultisubSubplebbit[] | null = null;
|
|
|
|
|
let cacheMetadata: MultisubMetadata | null = null;
|
2025-02-20 15:15:19 +01:00
|
|
|
let cacheAutoSubscribeAddresses: string[] | null = null;
|
2024-03-01 20:42:21 +01:00
|
|
|
|
2025-02-20 15:15:19 +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(() => {
|
2024-05-17 14:55:41 +02:00
|
|
|
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 {
|
2025-02-20 15:51:29 +01:00
|
|
|
const multisub = await fetch('https://raw.githubusercontent.com/plebbit/temporary-default-subplebbits/master/multisub.json').then((res) => {
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
return res.json();
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-28 22:42:48 +01:00
|
|
|
const filteredSubplebbits = multisub.subplebbits.filter((sub: MultisubSubplebbit) => !sub.lowUptime);
|
|
|
|
|
|
|
|
|
|
cacheSubplebbits = filteredSubplebbits;
|
2025-02-20 15:15:19 +01:00
|
|
|
|
|
|
|
|
// Cache auto-subscribe addresses when we fetch subplebbits
|
2025-02-28 22:42:48 +01:00
|
|
|
cacheAutoSubscribeAddresses = filteredSubplebbits
|
2025-10-17 23:30:35 +02:00
|
|
|
.filter((sub: MultisubSubplebbit) => sub.autoSubscribe5Chan && sub.address)
|
2025-02-20 15:15:19 +01:00
|
|
|
.map((sub: MultisubSubplebbit) => sub.address);
|
|
|
|
|
|
2025-02-20 15:51:29 +01:00
|
|
|
setState({
|
2025-02-28 22:42:48 +01:00
|
|
|
subplebbits: filteredSubplebbits,
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-28 22:42:48 +01:00
|
|
|
export const getAutoSubscribeAddresses = () => cacheAutoSubscribeAddresses || [];
|
|
|
|
|
|
2025-02-20 15:51:29 +01:00
|
|
|
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/temporary-default-subplebbits/master/multisub.json').then((res) => {
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
return res.json();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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();
|
2025-02-03 13:08:24 +01:00
|
|
|
return useMemo(() => defaultSubplebbits.map((subplebbit) => subplebbit.address), [defaultSubplebbits]);
|
2024-03-01 20:42:21 +01:00
|
|
|
};
|
|
|
|
|
|
2024-05-17 14:55:41 +02: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/temporary-default-subplebbits/master/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;
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-28 22:42:48 +01:00
|
|
|
const getUniqueTags = (multisub: any) => {
|
|
|
|
|
const allTags = new Set<string>();
|
|
|
|
|
Object.values(multisub).forEach((sub: any) => {
|
|
|
|
|
if (sub?.tags?.length) {
|
|
|
|
|
sub.tags.forEach((tag: string) => allTags.add(tag));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return Array.from(allTags).sort();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useDefaultSubplebbitTags = (subplebbits: any) => {
|
|
|
|
|
return useMemo(() => getUniqueTags(subplebbits), [subplebbits]);
|
|
|
|
|
};
|