mirror of
https://github.com/plebbit/seedit.git
synced 2026-06-11 09:35:54 -04:00
feat: auto subscribe new accounts to specific default subplebbits
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { Link, useLocation, useParams } from 'react-router-dom';
|
||||
import { createAccount, setActiveAccount, useAccount, useAccounts } from '@plebbit/plebbit-react-hooks';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import styles from './account-bar.module.css';
|
||||
import { createAccount, setActiveAccount, useAccount, useAccounts } from '@plebbit/plebbit-react-hooks';
|
||||
import { isSettingsView, isSubmitView, isSubplebbitView } from '../../lib/utils/view-utils';
|
||||
import { useAutoSubscribe } from '../../hooks/use-auto-subscribe';
|
||||
import styles from './account-bar.module.css';
|
||||
import SearchBar from '../search-bar';
|
||||
|
||||
const AccountBar = () => {
|
||||
@@ -17,6 +18,8 @@ const AccountBar = () => {
|
||||
const isInSubmitView = isSubmitView(location.pathname);
|
||||
const isInSettingsView = isSettingsView(location.pathname);
|
||||
|
||||
useAutoSubscribe();
|
||||
|
||||
const [searchVisible, setSearchVisible] = useState(false);
|
||||
const toggleSearchVisible = () => setSearchVisible(!searchVisible);
|
||||
const searchBarRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
@@ -7,12 +7,6 @@ const Over18Warning = () => {
|
||||
const { t } = useTranslation();
|
||||
const contentOptionsStore = useContentOptionsStore();
|
||||
|
||||
const hasUnhiddenAnyNsfwCommunity =
|
||||
!contentOptionsStore.hideAdultCommunities ||
|
||||
!contentOptionsStore.hideGoreCommunities ||
|
||||
!contentOptionsStore.hideAntiCommunities ||
|
||||
!contentOptionsStore.hideVulgarCommunities;
|
||||
|
||||
const handleAcceptWarning = () => {
|
||||
contentOptionsStore.setHideAdultCommunities(false);
|
||||
contentOptionsStore.setHideGoreCommunities(false);
|
||||
|
||||
38
src/hooks/use-auto-subscribe.ts
Normal file
38
src/hooks/use-auto-subscribe.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useAccount, setAccount } from '@plebbit/plebbit-react-hooks';
|
||||
import { getAutoSubscribeAddresses } from './use-default-subplebbits';
|
||||
|
||||
const AUTO_SUBSCRIBE_KEY_PREFIX = 'seedit-auto-subscribe-done-';
|
||||
const AUTO_SUBSCRIBE_DELAY = 200;
|
||||
|
||||
export const useAutoSubscribe = () => {
|
||||
const account = useAccount();
|
||||
const accountAddress = account?.author?.address;
|
||||
const hasExistingSubscriptions = account?.subscriptions?.length > 0;
|
||||
|
||||
useEffect(() => {
|
||||
if (!accountAddress || hasExistingSubscriptions) return;
|
||||
|
||||
const storageKey = AUTO_SUBSCRIBE_KEY_PREFIX + accountAddress;
|
||||
const hasAutoSubscribed = localStorage.getItem(storageKey);
|
||||
|
||||
if (!hasAutoSubscribed) {
|
||||
const initialDelay = setTimeout(() => {
|
||||
const autoSubscribeAddresses = getAutoSubscribeAddresses();
|
||||
|
||||
if (autoSubscribeAddresses.length) {
|
||||
const newSubscriptions = Array.from(new Set([...(account.subscriptions || []), ...autoSubscribeAddresses]));
|
||||
|
||||
setAccount({
|
||||
...account,
|
||||
subscriptions: newSubscriptions,
|
||||
});
|
||||
|
||||
localStorage.setItem(storageKey, 'true');
|
||||
}
|
||||
}, AUTO_SUBSCRIBE_DELAY);
|
||||
|
||||
return () => clearTimeout(initialDelay);
|
||||
}
|
||||
}, [accountAddress, account, hasExistingSubscriptions]);
|
||||
};
|
||||
@@ -14,10 +14,12 @@ export interface MultisubSubplebbit {
|
||||
address: string;
|
||||
tags?: string[];
|
||||
features?: string[];
|
||||
seeditAutoSubscribe?: boolean;
|
||||
}
|
||||
|
||||
let cacheSubplebbits: MultisubSubplebbit[] | null = null;
|
||||
let cacheMetadata: MultisubMetadata | null = null;
|
||||
let cacheAutoSubscribeAddresses: string[] | null = null;
|
||||
|
||||
export const useDefaultSubplebbits = () => {
|
||||
const [subplebbits, setSubplebbits] = useState<MultisubSubplebbit[]>([]);
|
||||
@@ -33,6 +35,12 @@ export 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.seeditAutoSubscribe && sub.address)
|
||||
.map((sub: MultisubSubplebbit) => sub.address);
|
||||
|
||||
setSubplebbits(multisub.subplebbits);
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
@@ -116,3 +124,5 @@ const getUniqueTags = (multisub: any) => {
|
||||
export const useDefaultSubplebbitTags = (subplebbits: any) => {
|
||||
return useMemo(() => getUniqueTags(subplebbits), [subplebbits]);
|
||||
};
|
||||
|
||||
export const getAutoSubscribeAddresses = () => cacheAutoSubscribeAddresses || [];
|
||||
|
||||
@@ -442,7 +442,7 @@ const SubplebbitSettings = () => {
|
||||
resetSubplebbitSettingsStore();
|
||||
navigate(`/p/${createdSubplebbit?.address}/`);
|
||||
}
|
||||
}, [createdSubplebbit, navigate, resetSubplebbitSettingsStore, account]);
|
||||
}, [createdSubplebbit, navigate, resetSubplebbitSettingsStore, account, subscribe]);
|
||||
|
||||
useEffect(() => {
|
||||
resetSubplebbitSettingsStore();
|
||||
|
||||
Reference in New Issue
Block a user