11import { useTranslation } from 'react-i18next' ;
2- import { useCallback , useEffect , useState } from 'react' ;
2+ import { useEffect , useState } from 'react' ;
33import { invoke } from '@tauri-apps/api/core' ;
44import { openUrl } from '@tauri-apps/plugin-opener' ;
55import { useNavigate } from 'react-router' ;
@@ -23,7 +23,7 @@ import {
2323} from '../../../contexts' ;
2424import { routes } from '../../../router' ;
2525import { useDeepLink , useLogout } from '../../../hooks' ;
26- import { StateDispatch , TAccountMode } from '../../../types' ;
26+ import { StateDispatch , TAccountMode , TAccountSummary } from '../../../types' ;
2727import { getAccountColor , getAccountDescription } from './utils' ;
2828
2929const IdsTimeToLive = 120 ; // sec
@@ -40,6 +40,7 @@ function Account() {
4040 accountSyncing,
4141 daemonStatus,
4242 accountMode,
43+ accountSummary,
4344 backendFlags,
4445 } = useMainState ( ) ;
4546 const dispatch = useMainDispatch ( ) as StateDispatch ;
@@ -49,29 +50,43 @@ function Account() {
4950 accountState === 'bandwidth-exceeded' ) ;
5051
5152 const [ isAccountLinking , setIsAccountLinking ] = useState ( false ) ;
52- const [ accountId , setAccountId ] = useState < string | null > ( null ) ;
5353 const [ deviceId , setDeviceId ] = useState < string | null > ( null ) ;
5454
55- const linkable = accountMode === 'api' ;
55+ // Privy and linking logic
56+ const isLoggedWithPrivy = accountMode === 'privy' ;
57+ const isDifferentCanonical =
58+ accountSummary ?. [ 'account-addr' ] !==
59+ accountSummary ?. [ 'canonical-account-addr' ] ;
60+ const hasLinkedAuthMethod = accountSummary ?. [ 'auth-methods' ] ?. some (
61+ ( it ) => it . label === 'Social login' || it . label === 'Passphrase' ,
62+ ) ;
63+
64+ const isAccountLinked =
65+ isLoggedWithPrivy || isDifferentCanonical || hasLinkedAuthMethod ;
5666
5767 const { startListening } = useDeepLink ( ) ;
5868 const { push } = useInAppNotify ( ) ;
5969
60- const getAccountId = async ( ) => {
61- const accountId = await CCache . get < string > ( 'cache-account-id' ) ;
62- if ( accountId ) {
63- setAccountId ( accountId ) ;
64- return ;
70+ const refreshAccount = async ( ) => {
71+ try {
72+ const summary = await invoke < TAccountSummary > ( 'get_account_summary' ) ;
73+ dispatch ( { type : 'set-account-summary' , summary } ) ;
74+ } catch ( err ) {
75+ console . error ( 'Failed to get account summary' , err ) ;
6576 }
6677 try {
67- const accountId = await invoke < string > ( 'get_account_id' ) ;
68- setAccountId ( accountId ) ;
69- CCache . set ( 'cache-account-id' , accountId , IdsTimeToLive ) ;
70- } catch {
71- setAccountId ( null ) ;
78+ const mode = await invoke < TAccountMode > ( 'get_account_mode' ) ;
79+ dispatch ( { type : 'set-account-mode' , mode } ) ;
80+ } catch ( err ) {
81+ console . error ( 'Failed to get account mode' , err ) ;
7282 }
7383 } ;
7484
85+ useEffect ( ( ) => {
86+ refreshAccount ( ) ;
87+ // eslint-disable-next-line react-hooks/exhaustive-deps
88+ } , [ ] ) ;
89+
7590 const getDeviceId = async ( ) => {
7691 const deviceId = await CCache . get < string > ( 'cache-device-id' ) ;
7792 if ( deviceId ) {
@@ -88,19 +103,14 @@ function Account() {
88103 } ;
89104
90105 useEffect ( ( ) => {
91- getAccountId ( ) ;
92106 getDeviceId ( ) ;
93107 } , [ ] ) ;
94108
109+ // When logged out, navigate to settings
95110 useEffect ( ( ) => {
96111 if ( ! account ) navigate ( routes . settings , { replace : true } ) ;
97112 } , [ account , navigate ] ) ;
98113
99- const refreshAccountMode = useCallback ( async ( ) => {
100- const mode = await invoke < TAccountMode > ( 'get_account_mode' ) ;
101- dispatch ( { type : 'set-account-mode' , mode } ) ;
102- } , [ dispatch ] ) ;
103-
104114 const handleAccountLink = async ( ) => {
105115 setIsAccountLinking ( true ) ;
106116
@@ -121,7 +131,7 @@ function Account() {
121131 await invoke ( 'store_deeplink_account' , {
122132 callbackUrl : deeplinkUrl ,
123133 } ) ;
124- await refreshAccountMode ( ) ;
134+ await refreshAccount ( ) ;
125135 } catch ( error ) {
126136 console . error ( 'Account login error: ' , error ) ;
127137 if ( error instanceof Error && error . message === 'Login timeout' ) {
@@ -177,7 +187,7 @@ function Account() {
177187 trailingIcon : 'open_in_new' ,
178188 onClick : handleManageSubscription ,
179189 } ,
180- ...( backendFlags . privy && linkable
190+ ...( backendFlags . privy && isAccountLinked
181191 ? [
182192 {
183193 title : t ( 'account.account-on-nym' ) ,
@@ -194,7 +204,7 @@ function Account() {
194204
195205 { backendFlags . privy && (
196206 < p className = "text-sm text-iron dark:text-bombay" >
197- { linkable
207+ { isAccountLinked
198208 ? t ( 'account.account-not-linked' )
199209 : t ( 'account.account-linked' ) }
200210 </ p >
@@ -211,9 +221,10 @@ function Account() {
211221 </ CardNewHeader >
212222 < CardNewBody className = "pb-5" >
213223 < CardNewCopyableRow
214- value = { accountId ?? '' }
215- label = { accountId ?? '' }
216- loading = { ! accountId }
224+ // Displaying canonical account address, as this is NYM's default account address
225+ value = { accountSummary ?. [ 'canonical-account-addr' ] ?? '' }
226+ label = { accountSummary ?. [ 'canonical-account-addr' ] ?? '' }
227+ loading = { ! accountSummary ?. [ 'canonical-account-addr' ] }
217228 />
218229 </ CardNewBody >
219230 </ CardNew >
0 commit comments