feat: auto subscribe new accounts to specific default subplebbits

This commit is contained in:
Tom (plebeius.eth)
2025-02-20 15:15:19 +01:00
parent 20726044e9
commit 882703b00d
10 changed files with 147 additions and 58 deletions
+64
View File
@@ -0,0 +1,64 @@
import { useEffect } from 'react';
import { useAccount, setAccount } from '@plebbit/plebbit-react-hooks';
import { useAutoSubscribeStore } from '../stores/use-auto-subscribe-store';
import { getAutoSubscribeAddresses, useDefaultSubplebbits } from './use-default-subplebbits';
const AUTO_SUBSCRIBE_KEY_PREFIX = 'seedit-auto-subscribe-done-';
// Keep track of which accounts have been processed globally
const processedAccounts = new Set<string>();
export const useAutoSubscribe = () => {
const account = useAccount();
const accountAddress = account?.author?.address;
const defaultSubplebbits = useDefaultSubplebbits();
const { addCheckingAccount, removeCheckingAccount, isCheckingAccount } = useAutoSubscribeStore();
useEffect(() => {
if (!accountAddress) return;
// Mark as checking immediately when account changes
addCheckingAccount(accountAddress);
const processAutoSubscribe = async () => {
if (!account || !defaultSubplebbits?.length) return;
if (processedAccounts.has(accountAddress)) {
removeCheckingAccount(accountAddress);
return;
}
const storageKey = AUTO_SUBSCRIBE_KEY_PREFIX + accountAddress;
const hasAutoSubscribed = localStorage.getItem(storageKey);
if (account.subscriptions?.length > 0 || hasAutoSubscribed) {
processedAccounts.add(accountAddress);
removeCheckingAccount(accountAddress);
return;
}
const autoSubscribeAddresses = getAutoSubscribeAddresses();
if (autoSubscribeAddresses.length) {
try {
await setAccount({
...account,
subscriptions: autoSubscribeAddresses,
});
localStorage.setItem(storageKey, 'true');
processedAccounts.add(accountAddress);
} catch (error) {
console.error('Auto-subscribe error:', error);
}
removeCheckingAccount(accountAddress);
}
};
processAutoSubscribe();
return () => {
if (accountAddress) removeCheckingAccount(accountAddress);
};
}, [account, accountAddress, defaultSubplebbits, addCheckingAccount, removeCheckingAccount]);
return { isCheckingSubscriptions: accountAddress ? isCheckingAccount(accountAddress) : true };
};
+10 -2
View File
@@ -12,12 +12,14 @@ export interface MultisubSubplebbit {
address: string;
tags?: string[];
features?: string[];
plebchanAutoSubscribe?: boolean;
}
let cacheSubplebbits: MultisubSubplebbit[] | null = null;
let cacheMetadata: MultisubMetadata | null = null;
let cacheAutoSubscribeAddresses: string[] | null = null;
const useDefaultSubplebbits = () => {
export const useDefaultSubplebbits = () => {
const [subplebbits, setSubplebbits] = useState<MultisubSubplebbit[]>([]);
useEffect(() => {
@@ -31,6 +33,12 @@ const useDefaultSubplebbits = () => {
// { cache: 'no-cache' }
).then((res) => res.json());
cacheSubplebbits = multisub.subplebbits;
// Cache auto-subscribe addresses when we fetch subplebbits
cacheAutoSubscribeAddresses = multisub.subplebbits
.filter((sub: MultisubSubplebbit) => sub.plebchanAutoSubscribe && sub.address)
.map((sub: MultisubSubplebbit) => sub.address);
setSubplebbits(multisub.subplebbits);
} catch (e) {
console.warn(e);
@@ -72,4 +80,4 @@ export const useMultisubMetadata = () => {
return cacheMetadata || metadata;
};
export default useDefaultSubplebbits;
export const getAutoSubscribeAddresses = () => cacheAutoSubscribeAddresses || [];
+1 -1
View File
@@ -1,7 +1,7 @@
import { useMemo } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import useThemeStore from '../stores/use-theme-store';
import useDefaultSubplebbits from './use-default-subplebbits';
import { useDefaultSubplebbits } from './use-default-subplebbits';
import { isAllView, isHomeView, isNotFoundView, isPendingPostView, isSubscriptionsView } from '../lib/utils/view-utils';
import { nsfwTags } from '../views/home/home';
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
+3 -3
View File
@@ -26,11 +26,11 @@ const useIsSubplebbitOffline = (subplebbit: Subplebbit) => {
const subplebbitOfflineStore = subplebbitOfflineState[address] || { initialLoad: true };
const loadingStartTimestamp = subplebbitsLoadingStartTimestamps[0] || 0;
const isLoading = subplebbitOfflineStore.initialLoad && (!updatedAt || Date.now() / 1000 - updatedAt >= 120 * 60) && Date.now() / 1000 - loadingStartTimestamp < 30;
const isLoading = subplebbitOfflineStore.initialLoad && (!updatedAt || Date.now() / 1000 - updatedAt >= 120 * 120) && Date.now() / 1000 - loadingStartTimestamp < 30;
const isOffline = !isLoading && ((updatedAt && updatedAt < Date.now() / 1000 - 120 * 60) || (!updatedAt && Date.now() / 1000 - loadingStartTimestamp >= 30));
const isOffline = !isLoading && ((updatedAt && updatedAt < Date.now() / 1000 - 120 * 120) || (!updatedAt && Date.now() / 1000 - loadingStartTimestamp >= 30));
const isOnline = updatedAt && Date.now() / 1000 - updatedAt < 120 * 60;
const isOnline = updatedAt && Date.now() / 1000 - updatedAt < 120 * 120;
const offlineIconClass = isLoading ? 'yellowOfflineIcon' : isOffline ? 'redOfflineIcon' : '';
const offlineTitle = isLoading
+1 -1
View File
@@ -2,7 +2,7 @@ import { useState, useEffect, useCallback } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { isAllView, isSubscriptionsView } from '../lib/utils/view-utils';
import useThemeStore from '../stores/use-theme-store';
import useDefaultSubplebbits from './use-default-subplebbits';
import { useDefaultSubplebbits } from './use-default-subplebbits';
import useInitialTheme from './use-initial-theme';
import { nsfwTags } from '../views/home/home';
import { useAccountComment } from '@plebbit/plebbit-react-hooks';