feat(settings): auto-subscribe imported accounts to default subs and moderated subplebbits

This commit is contained in:
Tom (plebeius.eth)
2025-03-07 19:10:28 +01:00
parent 70f640ecca
commit 0f38fb3ae7
2 changed files with 41 additions and 15 deletions
@@ -132,7 +132,6 @@ const AccountSettings = () => {
fileInput.type = 'file';
fileInput.accept = '.json';
// Handle file selection
fileInput.onchange = async (event) => {
try {
const files = (event.target as HTMLInputElement).files;
@@ -141,7 +140,6 @@ const AccountSettings = () => {
}
const file = files[0];
// Read the file content
const reader = new FileReader();
reader.onload = async (e) => {
try {
@@ -149,20 +147,40 @@ const AccountSettings = () => {
if (typeof fileContent !== 'string') {
throw new Error('File content is not a string.');
}
const newAccount = JSON.parse(fileContent);
await importAccount(fileContent);
// Store the imported account's address
if (newAccount.account?.author?.address) {
localStorage.setItem('importedAccountAddress', newAccount.account.author.address);
const accountData = JSON.parse(fileContent);
// Add subplebbit addresses to subscriptions if they exist
if (accountData.account?.subplebbits) {
const subplebbitAddresses = Object.keys(accountData.account.subplebbits);
if (!accountData.account.subscriptions) {
accountData.account.subscriptions = [];
}
const uniqueSubscriptions = [...accountData.account.subscriptions];
for (const address of subplebbitAddresses) {
if (!uniqueSubscriptions.includes(address)) {
uniqueSubscriptions.push(address);
}
}
accountData.account.subscriptions = uniqueSubscriptions;
}
// Set the new account as active before reloading
if (newAccount.account?.name) {
await setActiveAccount(newAccount.account.name);
const modifiedAccountJson = JSON.stringify(accountData);
await importAccount(modifiedAccountJson);
if (accountData.account?.author?.address) {
localStorage.setItem('importedAccountAddress', accountData.account.author.address);
}
alert(`Imported ${newAccount.account?.name}`);
if (accountData.account?.name) {
await setActiveAccount(accountData.account.name);
}
alert(`Imported ${accountData.account?.name}`);
const currentPath = location.pathname;
if (!currentPath.includes('/settings#account-settings')) {
@@ -189,7 +207,6 @@ const AccountSettings = () => {
}
};
// Trigger file selection dialog
fileInput.click();
};
+12 -3
View File
@@ -5,7 +5,6 @@ import { getAutoSubscribeAddresses, useDefaultSubplebbits } from './use-default-
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 = () => {
@@ -31,7 +30,7 @@ export const useAutoSubscribe = () => {
const storageKey = AUTO_SUBSCRIBE_KEY_PREFIX + accountAddress;
const hasAutoSubscribed = localStorage.getItem(storageKey);
if (account.subscriptions?.length > 0 || hasAutoSubscribed) {
if (hasAutoSubscribed) {
processedAccounts.add(accountAddress);
removeCheckingAccount(accountAddress);
return;
@@ -40,9 +39,19 @@ export const useAutoSubscribe = () => {
const autoSubscribeAddresses = getAutoSubscribeAddresses();
if (autoSubscribeAddresses.length) {
try {
const currentSubscriptions = account.subscriptions || [];
const mergedSubscriptions = [...currentSubscriptions];
for (const address of autoSubscribeAddresses) {
if (!mergedSubscriptions.includes(address)) {
mergedSubscriptions.push(address);
}
}
await setAccount({
...account,
subscriptions: autoSubscribeAddresses,
subscriptions: mergedSubscriptions,
});
localStorage.setItem(storageKey, 'true');
processedAccounts.add(accountAddress);