From 63a6206c355ff4487ece7e2c8f92f9103d15f1ba Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Wed, 28 May 2025 08:20:21 +0800 Subject: [PATCH] Refactor get/set accounts --- src/components/account-info.jsx | 11 +- src/components/nav-menu.jsx | 4 +- src/locales/en.po | 282 ++++++++++++++++---------------- src/pages/accounts.jsx | 19 ++- src/utils/store-utils.js | 25 ++- 5 files changed, 179 insertions(+), 162 deletions(-) diff --git a/src/components/account-info.jsx b/src/components/account-info.jsx index 6593c573..ab315d15 100644 --- a/src/components/account-info.jsx +++ b/src/components/account-info.jsx @@ -27,7 +27,12 @@ import showCompose from '../utils/show-compose'; import showToast from '../utils/show-toast'; import states from '../utils/states'; import store from '../utils/store'; -import { getCurrentAccountID, updateAccount } from '../utils/store-utils'; +import { + getAccounts, + getCurrentAccountID, + saveAccounts, + updateAccount, +} from '../utils/store-utils'; import supports from '../utils/supports'; import AccountBlock from './account-block'; @@ -224,7 +229,7 @@ function AccountInfo({ info?.url ); if (isSelf && instance && infoHasEssentials) { - const accounts = store.local.getJSON('accounts'); + const accounts = getAccounts(); let updated = false; accounts.forEach((account) => { if (account.info.id === info.id && account.instanceURL === instance) { @@ -234,7 +239,7 @@ function AccountInfo({ }); if (updated) { console.log('Updated account info', info); - store.local.setJSON('accounts', accounts); + saveAccounts(accounts); } } }, [isSelf, info, instance]); diff --git a/src/components/nav-menu.jsx b/src/components/nav-menu.jsx index b7d735a8..934ce5ec 100644 --- a/src/components/nav-menu.jsx +++ b/src/components/nav-menu.jsx @@ -12,7 +12,7 @@ import { getLists } from '../utils/lists'; import safeBoundingBoxPadding from '../utils/safe-bounding-box-padding'; import states from '../utils/states'; import store from '../utils/store'; -import { getCurrentAccountID } from '../utils/store-utils'; +import { getAccounts, getCurrentAccountID } from '../utils/store-utils'; import supports from '../utils/supports'; import Avatar from './avatar'; @@ -27,7 +27,7 @@ function NavMenu(props) { const { masto, instance, authenticated } = api(); const [currentAccount, moreThanOneAccount] = useMemo(() => { - const accounts = store.local.getJSON('accounts') || []; + const accounts = getAccounts(); const acc = accounts.find((account) => account.info.id === getCurrentAccountID()) || accounts[0]; diff --git a/src/locales/en.po b/src/locales/en.po index 1927d7ba..adc4ad41 100644 --- a/src/locales/en.po +++ b/src/locales/en.po @@ -28,12 +28,12 @@ msgid "Last posted: {0}" msgstr "" #: src/components/account-block.jsx:163 -#: src/components/account-info.jsx:705 +#: src/components/account-info.jsx:710 msgid "Automated" msgstr "" #: src/components/account-block.jsx:170 -#: src/components/account-info.jsx:710 +#: src/components/account-info.jsx:715 #: src/components/status.jsx:589 msgid "Group" msgstr "" @@ -43,17 +43,17 @@ msgid "Mutual" msgstr "" #: src/components/account-block.jsx:184 -#: src/components/account-info.jsx:1870 +#: src/components/account-info.jsx:1875 msgid "Requested" msgstr "" #: src/components/account-block.jsx:188 -#: src/components/account-info.jsx:1861 +#: src/components/account-info.jsx:1866 msgid "Following" msgstr "" #: src/components/account-block.jsx:192 -#: src/components/account-info.jsx:1175 +#: src/components/account-info.jsx:1180 msgid "Follows you" msgstr "" @@ -62,52 +62,52 @@ msgid "{followersCount, plural, one {# follower} other {# followers}}" msgstr "" #: src/components/account-block.jsx:209 -#: src/components/account-info.jsx:753 +#: src/components/account-info.jsx:758 msgid "Verified" msgstr "" #. placeholder {0}: niceDateTime(createdAt, { hideTime: true, }) #. placeholder {0}: niceDateTime(createdAt, { hideTime: true, }) #: src/components/account-block.jsx:224 -#: src/components/account-info.jsx:855 +#: src/components/account-info.jsx:860 msgid "Joined <0>{0}" msgstr "" -#: src/components/account-info.jsx:58 +#: src/components/account-info.jsx:63 msgid "Forever" msgstr "" -#: src/components/account-info.jsx:398 +#: src/components/account-info.jsx:403 msgid "Unable to load account." msgstr "" -#: src/components/account-info.jsx:413 +#: src/components/account-info.jsx:418 msgid "Go to account page" msgstr "" -#: src/components/account-info.jsx:442 -#: src/components/account-info.jsx:775 -#: src/components/account-info.jsx:805 +#: src/components/account-info.jsx:447 +#: src/components/account-info.jsx:780 +#: src/components/account-info.jsx:810 msgid "Followers" msgstr "" #. js-lingui-explicit-id -#: src/components/account-info.jsx:446 -#: src/components/account-info.jsx:815 -#: src/components/account-info.jsx:834 +#: src/components/account-info.jsx:451 +#: src/components/account-info.jsx:820 +#: src/components/account-info.jsx:839 msgid "following.stats" msgstr "Following" -#: src/components/account-info.jsx:449 -#: src/components/account-info.jsx:851 +#: src/components/account-info.jsx:454 +#: src/components/account-info.jsx:856 #: src/pages/account-statuses.jsx:482 #: src/pages/search.jsx:344 #: src/pages/search.jsx:491 msgid "Posts" msgstr "" -#: src/components/account-info.jsx:457 -#: src/components/account-info.jsx:1231 +#: src/components/account-info.jsx:462 +#: src/components/account-info.jsx:1236 #: src/components/compose.jsx:2783 #: src/components/media-alt-modal.jsx:55 #: src/components/media-modal.jsx:359 @@ -117,7 +117,7 @@ msgstr "" #: src/components/status.jsx:2598 #: src/components/status.jsx:2601 #: src/pages/account-statuses.jsx:526 -#: src/pages/accounts.jsx:113 +#: src/pages/accounts.jsx:118 #: src/pages/hashtag.jsx:202 #: src/pages/list.jsx:171 #: src/pages/public.jsx:116 @@ -127,56 +127,56 @@ msgstr "" msgid "More" msgstr "" -#: src/components/account-info.jsx:469 +#: src/components/account-info.jsx:474 msgid "<0>{displayName} has indicated that their new account is now:" msgstr "" -#: src/components/account-info.jsx:614 -#: src/components/account-info.jsx:1459 +#: src/components/account-info.jsx:619 +#: src/components/account-info.jsx:1464 msgid "Handle copied" msgstr "Handle copied" -#: src/components/account-info.jsx:617 -#: src/components/account-info.jsx:1462 +#: src/components/account-info.jsx:622 +#: src/components/account-info.jsx:1467 msgid "Unable to copy handle" msgstr "Unable to copy handle" -#: src/components/account-info.jsx:623 -#: src/components/account-info.jsx:1468 +#: src/components/account-info.jsx:628 +#: src/components/account-info.jsx:1473 msgid "Copy handle" msgstr "" -#: src/components/account-info.jsx:629 +#: src/components/account-info.jsx:634 msgid "Go to original profile page" msgstr "" -#: src/components/account-info.jsx:647 +#: src/components/account-info.jsx:652 msgid "View profile image" msgstr "" -#: src/components/account-info.jsx:665 +#: src/components/account-info.jsx:670 msgid "View profile header" msgstr "" -#: src/components/account-info.jsx:681 -#: src/components/account-info.jsx:1769 -#: src/components/account-info.jsx:2295 +#: src/components/account-info.jsx:686 +#: src/components/account-info.jsx:1774 +#: src/components/account-info.jsx:2300 msgid "Edit profile" msgstr "" -#: src/components/account-info.jsx:700 +#: src/components/account-info.jsx:705 msgid "In Memoriam" msgstr "" -#: src/components/account-info.jsx:782 -#: src/components/account-info.jsx:825 +#: src/components/account-info.jsx:787 +#: src/components/account-info.jsx:830 msgid "This user has chosen to not make this information available." msgstr "This user has chosen to not make this information available." #. placeholder {0}: ( postingStats.originals / postingStats.total ).toLocaleString(i18n.locale || undefined, { style: 'percent', }) #. placeholder {1}: ( postingStats.replies / postingStats.total ).toLocaleString(i18n.locale || undefined, { style: 'percent', }) #. placeholder {2}: ( postingStats.boosts / postingStats.total ).toLocaleString(i18n.locale || undefined, { style: 'percent', }) -#: src/components/account-info.jsx:880 +#: src/components/account-info.jsx:885 msgid "{0} original posts, {1} replies, {2} boosts" msgstr "{0} original posts, {1} replies, {2} boosts" @@ -187,22 +187,22 @@ msgstr "{0} original posts, {1} replies, {2} boosts" #. placeholder {4}: postingStats.total #. placeholder {5}: postingStats.total #. placeholder {6}: postingStats.daysSinceLastPost -#: src/components/account-info.jsx:896 +#: src/components/account-info.jsx:901 msgid "{0, plural, one {{1, plural, one {Last 1 post in the past 1 day} other {Last 1 post in the past {2} days}}} other {{3, plural, one {Last {4} posts in the past 1 day} other {Last {5} posts in the past {6} days}}}}" msgstr "" #. placeholder {0}: postingStats.total #. placeholder {1}: postingStats.total -#: src/components/account-info.jsx:912 +#: src/components/account-info.jsx:917 msgid "{0, plural, one {Last 1 post in the past year(s)} other {Last {1} posts in the past year(s)}}" msgstr "" -#: src/components/account-info.jsx:937 +#: src/components/account-info.jsx:942 #: src/pages/catchup.jsx:70 msgid "Original" msgstr "" -#: src/components/account-info.jsx:941 +#: src/components/account-info.jsx:946 #: src/components/status.jsx:2382 #: src/pages/catchup.jsx:71 #: src/pages/catchup.jsx:1448 @@ -212,7 +212,7 @@ msgstr "" msgid "Replies" msgstr "" -#: src/components/account-info.jsx:945 +#: src/components/account-info.jsx:950 #: src/pages/catchup.jsx:72 #: src/pages/catchup.jsx:1450 #: src/pages/catchup.jsx:2073 @@ -220,234 +220,234 @@ msgstr "" msgid "Boosts" msgstr "" -#: src/components/account-info.jsx:951 +#: src/components/account-info.jsx:956 msgid "Post stats unavailable." msgstr "" -#: src/components/account-info.jsx:982 +#: src/components/account-info.jsx:987 msgid "View post stats" msgstr "" #. placeholder {0}: niceDateTime(lastStatusAt, { hideTime: true, }) -#: src/components/account-info.jsx:1179 +#: src/components/account-info.jsx:1184 msgid "Last post: <0>{0}" msgstr "" -#: src/components/account-info.jsx:1193 +#: src/components/account-info.jsx:1198 msgid "Muted" msgstr "" -#: src/components/account-info.jsx:1198 +#: src/components/account-info.jsx:1203 msgid "Blocked" msgstr "" -#: src/components/account-info.jsx:1207 +#: src/components/account-info.jsx:1212 msgid "Private note" msgstr "Private note" -#: src/components/account-info.jsx:1264 +#: src/components/account-info.jsx:1269 msgid "Mention <0>@{username}" msgstr "" -#: src/components/account-info.jsx:1276 +#: src/components/account-info.jsx:1281 msgid "Translate bio" msgstr "" -#: src/components/account-info.jsx:1287 +#: src/components/account-info.jsx:1292 msgid "Edit private note" msgstr "Edit private note" -#: src/components/account-info.jsx:1287 +#: src/components/account-info.jsx:1292 msgid "Add private note" msgstr "Add private note" -#: src/components/account-info.jsx:1307 +#: src/components/account-info.jsx:1312 msgid "Notifications enabled for @{username}'s posts." msgstr "Notifications enabled for @{username}'s posts." -#: src/components/account-info.jsx:1308 +#: src/components/account-info.jsx:1313 msgid " Notifications disabled for @{username}'s posts." msgstr " Notifications disabled for @{username}'s posts." -#: src/components/account-info.jsx:1320 +#: src/components/account-info.jsx:1325 msgid "Disable notifications" msgstr "Disable notifications" -#: src/components/account-info.jsx:1321 +#: src/components/account-info.jsx:1326 msgid "Enable notifications" msgstr "Enable notifications" -#: src/components/account-info.jsx:1338 +#: src/components/account-info.jsx:1343 msgid "Boosts from @{username} enabled." msgstr "Boosts from @{username} enabled." -#: src/components/account-info.jsx:1339 +#: src/components/account-info.jsx:1344 msgid "Boosts from @{username} disabled." msgstr "Boosts from @{username} disabled." -#: src/components/account-info.jsx:1350 +#: src/components/account-info.jsx:1355 msgid "Disable boosts" msgstr "Disable boosts" -#: src/components/account-info.jsx:1350 +#: src/components/account-info.jsx:1355 msgid "Enable boosts" msgstr "Enable boosts" -#: src/components/account-info.jsx:1369 +#: src/components/account-info.jsx:1374 msgid "@{username} is no longer featured on your profile." msgstr "@{username} is no longer featured on your profile." -#: src/components/account-info.jsx:1379 +#: src/components/account-info.jsx:1384 msgid "@{username} is now featured on your profile." msgstr "@{username} is now featured on your profile." -#: src/components/account-info.jsx:1387 +#: src/components/account-info.jsx:1392 msgid "Unable to unfeature @{username} on your profile." msgstr "Unable to unfeature @{username} on your profile." -#: src/components/account-info.jsx:1391 +#: src/components/account-info.jsx:1396 msgid "Unable to feature @{username} on your profile." msgstr "Unable to feature @{username} on your profile." -#: src/components/account-info.jsx:1400 +#: src/components/account-info.jsx:1405 msgid "Don't feature on profile" msgstr "Don't feature on profile" -#: src/components/account-info.jsx:1401 +#: src/components/account-info.jsx:1406 #: src/pages/hashtag.jsx:331 msgid "Feature on profile" msgstr "" -#: src/components/account-info.jsx:1410 +#: src/components/account-info.jsx:1415 msgid "Show featured profiles" msgstr "Show featured profiles" -#: src/components/account-info.jsx:1425 -#: src/components/account-info.jsx:1435 -#: src/components/account-info.jsx:2041 +#: src/components/account-info.jsx:1430 +#: src/components/account-info.jsx:1440 +#: src/components/account-info.jsx:2046 msgid "Add/Remove from Lists" msgstr "" -#: src/components/account-info.jsx:1485 +#: src/components/account-info.jsx:1490 #: src/components/status.jsx:1257 msgid "Link copied" msgstr "" -#: src/components/account-info.jsx:1488 +#: src/components/account-info.jsx:1493 #: src/components/status.jsx:1260 msgid "Unable to copy link" msgstr "" -#: src/components/account-info.jsx:1494 +#: src/components/account-info.jsx:1499 #: src/components/shortcuts-settings.jsx:1059 #: src/components/status.jsx:1266 #: src/components/status.jsx:3376 msgid "Copy" msgstr "" -#: src/components/account-info.jsx:1509 +#: src/components/account-info.jsx:1514 #: src/components/shortcuts-settings.jsx:1077 #: src/components/status.jsx:1282 msgid "Sharing doesn't seem to work." msgstr "" -#: src/components/account-info.jsx:1515 +#: src/components/account-info.jsx:1520 #: src/components/status.jsx:1288 msgid "Share…" msgstr "" -#: src/components/account-info.jsx:1535 +#: src/components/account-info.jsx:1540 msgid "Unmuted @{username}" msgstr "Unmuted @{username}" -#: src/components/account-info.jsx:1547 +#: src/components/account-info.jsx:1552 msgid "Unmute <0>@{username}" msgstr "" -#: src/components/account-info.jsx:1563 +#: src/components/account-info.jsx:1568 msgid "Mute <0>@{username}…" msgstr "" #. placeholder {0}: typeof MUTE_DURATIONS_LABELS[duration] === 'function' ? MUTE_DURATIONS_LABELS[duration]() : _(MUTE_DURATIONS_LABELS[duration]) -#: src/components/account-info.jsx:1595 +#: src/components/account-info.jsx:1600 msgid "Muted @{username} for {0}" msgstr "Muted @{username} for {0}" -#: src/components/account-info.jsx:1607 +#: src/components/account-info.jsx:1612 msgid "Unable to mute @{username}" msgstr "Unable to mute @{username}" -#: src/components/account-info.jsx:1628 +#: src/components/account-info.jsx:1633 msgid "Remove <0>@{username} from followers?" msgstr "" -#: src/components/account-info.jsx:1648 +#: src/components/account-info.jsx:1653 msgid "@{username} removed from followers" msgstr "@{username} removed from followers" -#: src/components/account-info.jsx:1660 +#: src/components/account-info.jsx:1665 msgid "Remove follower…" msgstr "" -#: src/components/account-info.jsx:1671 +#: src/components/account-info.jsx:1676 msgid "Block <0>@{username}?" msgstr "" -#: src/components/account-info.jsx:1695 +#: src/components/account-info.jsx:1700 msgid "Unblocked @{username}" msgstr "Unblocked @{username}" -#: src/components/account-info.jsx:1703 +#: src/components/account-info.jsx:1708 msgid "Blocked @{username}" msgstr "Blocked @{username}" -#: src/components/account-info.jsx:1711 +#: src/components/account-info.jsx:1716 msgid "Unable to unblock @{username}" msgstr "Unable to unblock @{username}" -#: src/components/account-info.jsx:1713 +#: src/components/account-info.jsx:1718 msgid "Unable to block @{username}" msgstr "Unable to block @{username}" -#: src/components/account-info.jsx:1723 +#: src/components/account-info.jsx:1728 msgid "Unblock <0>@{username}" msgstr "" -#: src/components/account-info.jsx:1732 +#: src/components/account-info.jsx:1737 msgid "Block <0>@{username}…" msgstr "" -#: src/components/account-info.jsx:1749 +#: src/components/account-info.jsx:1754 msgid "Report <0>@{username}…" msgstr "" -#: src/components/account-info.jsx:1805 +#: src/components/account-info.jsx:1810 msgid "Withdraw follow request?" msgstr "Withdraw follow request?" #. placeholder {0}: info.acct || info.username -#: src/components/account-info.jsx:1806 +#: src/components/account-info.jsx:1811 msgid "Unfollow @{0}?" msgstr "Unfollow @{0}?" -#: src/components/account-info.jsx:1864 +#: src/components/account-info.jsx:1869 msgid "Unfollow…" msgstr "" -#: src/components/account-info.jsx:1873 +#: src/components/account-info.jsx:1878 msgid "Withdraw…" msgstr "" -#: src/components/account-info.jsx:1880 -#: src/components/account-info.jsx:1884 +#: src/components/account-info.jsx:1885 +#: src/components/account-info.jsx:1889 #: src/pages/hashtag.jsx:264 msgid "Follow" msgstr "" -#: src/components/account-info.jsx:1981 -#: src/components/account-info.jsx:2036 -#: src/components/account-info.jsx:2170 -#: src/components/account-info.jsx:2290 +#: src/components/account-info.jsx:1986 +#: src/components/account-info.jsx:2041 +#: src/components/account-info.jsx:2175 +#: src/components/account-info.jsx:2295 #: src/components/account-sheet.jsx:38 #: src/components/compose.jsx:891 #: src/components/compose.jsx:2739 @@ -469,7 +469,7 @@ msgstr "" #: src/components/status.jsx:3100 #: src/components/status.jsx:3340 #: src/components/status.jsx:3849 -#: src/pages/accounts.jsx:40 +#: src/pages/accounts.jsx:45 #: src/pages/catchup.jsx:1584 #: src/pages/filters.jsx:225 #: src/pages/list.jsx:302 @@ -480,85 +480,85 @@ msgstr "" msgid "Close" msgstr "" -#: src/components/account-info.jsx:1986 +#: src/components/account-info.jsx:1991 msgid "Translated Bio" msgstr "" -#: src/components/account-info.jsx:2081 +#: src/components/account-info.jsx:2086 msgid "Unable to remove from list." msgstr "Unable to remove from list." -#: src/components/account-info.jsx:2082 +#: src/components/account-info.jsx:2087 msgid "Unable to add to list." msgstr "Unable to add to list." -#: src/components/account-info.jsx:2101 +#: src/components/account-info.jsx:2106 #: src/pages/lists.jsx:131 msgid "Unable to load lists." msgstr "" -#: src/components/account-info.jsx:2105 +#: src/components/account-info.jsx:2110 msgid "No lists." msgstr "" -#: src/components/account-info.jsx:2116 +#: src/components/account-info.jsx:2121 #: src/components/list-add-edit.jsx:41 #: src/pages/lists.jsx:62 msgid "New list" msgstr "" #. placeholder {0}: account?.username || account?.acct -#: src/components/account-info.jsx:2175 +#: src/components/account-info.jsx:2180 msgid "Private note about <0>@{0}" msgstr "" -#: src/components/account-info.jsx:2205 +#: src/components/account-info.jsx:2210 msgid "Unable to update private note." msgstr "Unable to update private note." -#: src/components/account-info.jsx:2228 -#: src/components/account-info.jsx:2526 +#: src/components/account-info.jsx:2233 +#: src/components/account-info.jsx:2531 msgid "Cancel" msgstr "" -#: src/components/account-info.jsx:2233 +#: src/components/account-info.jsx:2238 msgid "Save & close" msgstr "" -#: src/components/account-info.jsx:2350 +#: src/components/account-info.jsx:2355 msgid "Unable to update profile." msgstr "Unable to update profile." -#: src/components/account-info.jsx:2357 +#: src/components/account-info.jsx:2362 msgid "Header picture" msgstr "Header picture" -#: src/components/account-info.jsx:2409 +#: src/components/account-info.jsx:2414 msgid "Profile picture" msgstr "Profile picture" -#: src/components/account-info.jsx:2461 +#: src/components/account-info.jsx:2466 #: src/components/list-add-edit.jsx:106 msgid "Name" msgstr "" -#: src/components/account-info.jsx:2474 +#: src/components/account-info.jsx:2479 msgid "Bio" msgstr "" -#: src/components/account-info.jsx:2487 +#: src/components/account-info.jsx:2492 msgid "Extra fields" msgstr "" -#: src/components/account-info.jsx:2493 +#: src/components/account-info.jsx:2498 msgid "Label" msgstr "" -#: src/components/account-info.jsx:2496 +#: src/components/account-info.jsx:2501 msgid "Content" msgstr "" -#: src/components/account-info.jsx:2529 +#: src/components/account-info.jsx:2534 #: src/components/list-add-edit.jsx:152 #: src/components/shortcuts-settings.jsx:715 #: src/pages/filters.jsx:570 @@ -566,20 +566,20 @@ msgstr "" msgid "Save" msgstr "" -#: src/components/account-info.jsx:2583 +#: src/components/account-info.jsx:2588 msgid "username" msgstr "" -#: src/components/account-info.jsx:2587 +#: src/components/account-info.jsx:2592 msgid "server domain name" msgstr "" #. placeholder {0}: info.username -#: src/components/account-info.jsx:2653 +#: src/components/account-info.jsx:2658 msgid "Profiles featured by @{0}" msgstr "Profiles featured by @{0}" -#: src/components/account-info.jsx:2679 +#: src/components/account-info.jsx:2684 msgid "No featured profiles." msgstr "No featured profiles." @@ -1076,7 +1076,7 @@ msgstr "" #: src/components/generic-accounts.jsx:154 #: src/components/notification.jsx:449 -#: src/pages/accounts.jsx:45 +#: src/pages/accounts.jsx:50 #: src/pages/search.jsx:334 #: src/pages/search.jsx:367 msgid "Accounts" @@ -1983,12 +1983,12 @@ msgid "Not available in current view mode" msgstr "" #: src/components/shortcuts-settings.jsx:351 -#: src/pages/accounts.jsx:183 +#: src/pages/accounts.jsx:188 msgid "Move up" msgstr "" #: src/components/shortcuts-settings.jsx:367 -#: src/pages/accounts.jsx:198 +#: src/pages/accounts.jsx:203 msgid "Move down" msgstr "" @@ -2690,49 +2690,49 @@ msgstr "" msgid "Month" msgstr "" -#: src/pages/accounts.jsx:59 +#: src/pages/accounts.jsx:64 msgid "Current" msgstr "" -#: src/pages/accounts.jsx:105 +#: src/pages/accounts.jsx:110 msgid "Default" msgstr "" -#: src/pages/accounts.jsx:127 +#: src/pages/accounts.jsx:132 msgid "Switch to this account" msgstr "Switch to this account" -#: src/pages/accounts.jsx:136 +#: src/pages/accounts.jsx:141 msgid "Switch in new tab/window" msgstr "Switch in new tab/window" -#: src/pages/accounts.jsx:150 +#: src/pages/accounts.jsx:155 msgid "View profile…" msgstr "" -#: src/pages/accounts.jsx:168 +#: src/pages/accounts.jsx:173 msgid "Set as default" msgstr "" #. placeholder {0}: account.info.acct -#: src/pages/accounts.jsx:210 +#: src/pages/accounts.jsx:215 msgid "Log out <0>@{0}?" msgstr "" -#: src/pages/accounts.jsx:239 +#: src/pages/accounts.jsx:244 msgid "Log out…" msgstr "" #. placeholder {0}: niceDateTime(account.createdAt) -#: src/pages/accounts.jsx:246 +#: src/pages/accounts.jsx:251 msgid "Connected on {0} (<0/>)" msgstr "Connected on {0} (<0/>)" -#: src/pages/accounts.jsx:263 +#: src/pages/accounts.jsx:268 msgid "Add an existing account" msgstr "" -#: src/pages/accounts.jsx:270 +#: src/pages/accounts.jsx:275 msgid "Note: <0>Default account will always be used for first load. Switched accounts will persist during the session." msgstr "" diff --git a/src/pages/accounts.jsx b/src/pages/accounts.jsx index 4b6e6e84..8556b4ce 100644 --- a/src/pages/accounts.jsx +++ b/src/pages/accounts.jsx @@ -18,7 +18,12 @@ import { revokeAccessToken } from '../utils/auth'; import niceDateTime from '../utils/nice-date-time'; import states from '../utils/states'; import store from '../utils/store'; -import { getCurrentAccountID, setCurrentAccountID } from '../utils/store-utils'; +import { + getAccounts, + getCurrentAccountID, + saveAccounts, + setCurrentAccountID, +} from '../utils/store-utils'; const isStandalone = window.matchMedia('(display-mode: standalone)').matches; @@ -26,7 +31,7 @@ function Accounts({ onClose }) { const { t } = useLingui(); const { masto } = api(); // Accounts - const accounts = store.local.getJSON('accounts'); + const accounts = getAccounts(); const currentAccount = getCurrentAccountID(); const moreThanOneAccount = accounts.length > 1; @@ -70,7 +75,7 @@ function Accounts({ onClose }) { .fetch(); console.log('fetched account info', info); account.info = info; - store.local.setJSON('accounts', accounts); + saveAccounts(accounts); reload(); } catch (e) {} } @@ -159,7 +164,7 @@ function Accounts({ onClose }) { // Move account to the top of the list accounts.splice(i, 1); accounts.unshift(account); - store.local.setJSON('accounts', accounts); + saveAccounts(accounts); reload(); }} > @@ -174,7 +179,7 @@ function Accounts({ onClose }) { // Move account one position up accounts.splice(i, 1); accounts.splice(i - 1, 0, account); - store.local.setJSON('accounts', accounts); + saveAccounts(accounts); reload(); }} > @@ -189,7 +194,7 @@ function Accounts({ onClose }) { // Move account one position down accounts.splice(i, 1); accounts.splice(i + 1, 0, account); - store.local.setJSON('accounts', accounts); + saveAccounts(accounts); reload(); }} > @@ -229,7 +234,7 @@ function Accounts({ onClose }) { token: account.accessToken, }); accounts.splice(i, 1); - store.local.setJSON('accounts', accounts); + saveAccounts(accounts); // location.reload(); location.href = location.pathname || '/'; }} diff --git a/src/utils/store-utils.js b/src/utils/store-utils.js index ef43d331..9e1e52a6 100644 --- a/src/utils/store-utils.js +++ b/src/utils/store-utils.js @@ -1,23 +1,31 @@ import store from './store'; +export function getAccounts() { + return store.local.getJSON('accounts') || []; +} + +export function saveAccounts(accounts) { + store.local.setJSON('accounts', accounts); +} + export function getAccount(id) { - const accounts = store.local.getJSON('accounts') || []; + const accounts = getAccounts(); if (!id) return accounts[0]; return accounts.find((a) => a.info.id === id) || accounts[0]; } export function getAccountByAccessToken(accessToken) { - const accounts = store.local.getJSON('accounts') || []; + const accounts = getAccounts(); return accounts.find((a) => a.accessToken === accessToken); } export function getAccountByInstance(instance) { - const accounts = store.local.getJSON('accounts') || []; + const accounts = getAccounts(); return accounts.find((a) => a.instanceURL === instance); } export function hasAccountInInstance(instance) { - const accounts = store.local.getJSON('accounts') || []; + const accounts = getAccounts(); return accounts.some((a) => a.instanceURL === instance); } @@ -69,7 +77,7 @@ export function getCurrentAccountNS() { } export function saveAccount(account) { - const accounts = store.local.getJSON('accounts') || []; + const accounts = getAccounts(); const acc = accounts.find((a) => a.info.id === account.info.id); if (acc) { acc.info = account.info; @@ -79,13 +87,12 @@ export function saveAccount(account) { } else { accounts.push(account); } - store.local.setJSON('accounts', accounts); - setCurrentAccountID(account.info.id); + saveAccounts(accounts); } export function updateAccount(accountInfo) { // Only update if displayName or avatar or avatar_static is different - const accounts = store.local.getJSON('accounts') || []; + const accounts = getAccounts(); const acc = accounts.find((a) => a.info.id === accountInfo.id); if (acc) { if ( @@ -97,7 +104,7 @@ export function updateAccount(accountInfo) { ...acc.info, ...accountInfo, }; - store.local.setJSON('accounts', accounts); + saveAccounts(accounts); } } }