mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
feat(settings): auto-subscribe imported accounts to default subs and moderated subplebbits
This commit is contained in:
@@ -132,7 +132,6 @@ const AccountSettings = () => {
|
|||||||
fileInput.type = 'file';
|
fileInput.type = 'file';
|
||||||
fileInput.accept = '.json';
|
fileInput.accept = '.json';
|
||||||
|
|
||||||
// Handle file selection
|
|
||||||
fileInput.onchange = async (event) => {
|
fileInput.onchange = async (event) => {
|
||||||
try {
|
try {
|
||||||
const files = (event.target as HTMLInputElement).files;
|
const files = (event.target as HTMLInputElement).files;
|
||||||
@@ -141,7 +140,6 @@ const AccountSettings = () => {
|
|||||||
}
|
}
|
||||||
const file = files[0];
|
const file = files[0];
|
||||||
|
|
||||||
// Read the file content
|
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = async (e) => {
|
reader.onload = async (e) => {
|
||||||
try {
|
try {
|
||||||
@@ -149,20 +147,40 @@ const AccountSettings = () => {
|
|||||||
if (typeof fileContent !== 'string') {
|
if (typeof fileContent !== 'string') {
|
||||||
throw new Error('File content is not a string.');
|
throw new Error('File content is not a string.');
|
||||||
}
|
}
|
||||||
const newAccount = JSON.parse(fileContent);
|
|
||||||
await importAccount(fileContent);
|
|
||||||
|
|
||||||
// Store the imported account's address
|
const accountData = JSON.parse(fileContent);
|
||||||
if (newAccount.account?.author?.address) {
|
|
||||||
localStorage.setItem('importedAccountAddress', newAccount.account.author.address);
|
// 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
|
const modifiedAccountJson = JSON.stringify(accountData);
|
||||||
if (newAccount.account?.name) {
|
await importAccount(modifiedAccountJson);
|
||||||
await setActiveAccount(newAccount.account.name);
|
|
||||||
|
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;
|
const currentPath = location.pathname;
|
||||||
if (!currentPath.includes('/settings#account-settings')) {
|
if (!currentPath.includes('/settings#account-settings')) {
|
||||||
@@ -189,7 +207,6 @@ const AccountSettings = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Trigger file selection dialog
|
|
||||||
fileInput.click();
|
fileInput.click();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import { getAutoSubscribeAddresses, useDefaultSubplebbits } from './use-default-
|
|||||||
|
|
||||||
const AUTO_SUBSCRIBE_KEY_PREFIX = 'seedit-auto-subscribe-done-';
|
const AUTO_SUBSCRIBE_KEY_PREFIX = 'seedit-auto-subscribe-done-';
|
||||||
|
|
||||||
// Keep track of which accounts have been processed globally
|
|
||||||
const processedAccounts = new Set<string>();
|
const processedAccounts = new Set<string>();
|
||||||
|
|
||||||
export const useAutoSubscribe = () => {
|
export const useAutoSubscribe = () => {
|
||||||
@@ -31,7 +30,7 @@ export const useAutoSubscribe = () => {
|
|||||||
const storageKey = AUTO_SUBSCRIBE_KEY_PREFIX + accountAddress;
|
const storageKey = AUTO_SUBSCRIBE_KEY_PREFIX + accountAddress;
|
||||||
const hasAutoSubscribed = localStorage.getItem(storageKey);
|
const hasAutoSubscribed = localStorage.getItem(storageKey);
|
||||||
|
|
||||||
if (account.subscriptions?.length > 0 || hasAutoSubscribed) {
|
if (hasAutoSubscribed) {
|
||||||
processedAccounts.add(accountAddress);
|
processedAccounts.add(accountAddress);
|
||||||
removeCheckingAccount(accountAddress);
|
removeCheckingAccount(accountAddress);
|
||||||
return;
|
return;
|
||||||
@@ -40,9 +39,19 @@ export const useAutoSubscribe = () => {
|
|||||||
const autoSubscribeAddresses = getAutoSubscribeAddresses();
|
const autoSubscribeAddresses = getAutoSubscribeAddresses();
|
||||||
if (autoSubscribeAddresses.length) {
|
if (autoSubscribeAddresses.length) {
|
||||||
try {
|
try {
|
||||||
|
const currentSubscriptions = account.subscriptions || [];
|
||||||
|
|
||||||
|
const mergedSubscriptions = [...currentSubscriptions];
|
||||||
|
|
||||||
|
for (const address of autoSubscribeAddresses) {
|
||||||
|
if (!mergedSubscriptions.includes(address)) {
|
||||||
|
mergedSubscriptions.push(address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await setAccount({
|
await setAccount({
|
||||||
...account,
|
...account,
|
||||||
subscriptions: autoSubscribeAddresses,
|
subscriptions: mergedSubscriptions,
|
||||||
});
|
});
|
||||||
localStorage.setItem(storageKey, 'true');
|
localStorage.setItem(storageKey, 'true');
|
||||||
processedAccounts.add(accountAddress);
|
processedAccounts.add(accountAddress);
|
||||||
|
|||||||
Reference in New Issue
Block a user