Update account info if there's name or avatar change

This commit is contained in:
Lim Chee Aun 2023-06-28 17:38:01 +08:00
parent 33720d9694
commit c61e9bb61c
2 changed files with 26 additions and 0 deletions

View file

@ -12,6 +12,7 @@ import shortenNumber from '../utils/shorten-number';
import showToast from '../utils/show-toast'; import showToast from '../utils/show-toast';
import states, { hideAllModals } from '../utils/states'; import states, { hideAllModals } from '../utils/states';
import store from '../utils/store'; import store from '../utils/store';
import { updateAccount } from '../utils/store-utils';
import AccountBlock from './account-block'; import AccountBlock from './account-block';
import Avatar from './avatar'; import Avatar from './avatar';
@ -483,6 +484,12 @@ function RelatedActions({ info, instance, authenticated }) {
} }
}, [info, authenticated]); }, [info, authenticated]);
useEffect(() => {
if (info && isSelf) {
updateAccount(info);
}
}, [info, isSelf]);
const loading = relationshipUIState === 'loading'; const loading = relationshipUIState === 'loading';
const menuInstanceRef = useRef(null); const menuInstanceRef = useRef(null);

View file

@ -34,6 +34,25 @@ export function saveAccount(account) {
store.session.set('currentAccount', account.info.id); store.session.set('currentAccount', account.info.id);
} }
export function updateAccount(accountInfo) {
// Only update if displayName or avatar or avatar_static is different
const accounts = store.local.getJSON('accounts') || [];
const acc = accounts.find((a) => a.info.id === accountInfo.id);
if (acc) {
if (
acc.info.displayName !== accountInfo.displayName ||
acc.info.avatar !== accountInfo.avatar ||
acc.info.avatar_static !== accountInfo.avatar_static
) {
acc.info = {
...acc.info,
...accountInfo,
};
store.local.setJSON('accounts', accounts);
}
}
}
let currentInstance = null; let currentInstance = null;
export function getCurrentInstance() { export function getCurrentInstance() {
if (currentInstance) return currentInstance; if (currentInstance) return currentInstance;