@@ -37,27 +37,6 @@ export default function CurrencyDetail() {
3737 return null ;
3838 } ) ;
3939
40- // Set default to user account if no context is set and user is available
41- useEffect ( ( ) => {
42- if ( user && ! accountContext ) {
43- const defaultContext = { type : "user" as const , id : user . id } ;
44- setAccountContext ( defaultContext ) ;
45- localStorage . setItem ( "ecurrency_account_context" , JSON . stringify ( defaultContext ) ) ;
46- }
47- } , [ user , accountContext ] ) ;
48-
49- // Save account context to localStorage whenever it changes
50- const handleAccountContextChange = ( context : { type : "user" | "group" ; id : string } | null ) => {
51- // If null is passed, default to user account
52- const finalContext = context || ( user ? { type : "user" as const , id : user . id } : null ) ;
53- setAccountContext ( finalContext ) ;
54- if ( finalContext ) {
55- localStorage . setItem ( "ecurrency_account_context" , JSON . stringify ( finalContext ) ) ;
56- } else {
57- localStorage . removeItem ( "ecurrency_account_context" ) ;
58- }
59- } ;
60-
6140 const currencyId = params ?. currencyId ;
6241
6342 const { data : currency } = useQuery ( {
@@ -104,6 +83,51 @@ export default function CurrencyDetail() {
10483 } ,
10584 } ) ;
10685
86+ // Validate account context after user and groups are loaded
87+ useEffect ( ( ) => {
88+ if ( ! user ) return ;
89+
90+ const adminGroups = groups ?. filter ( ( g : any ) => g . isAdmin ) || [ ] ;
91+
92+ // If no context is set, default to user account
93+ if ( ! accountContext ) {
94+ const defaultContext = { type : "user" as const , id : user . id } ;
95+ setAccountContext ( defaultContext ) ;
96+ localStorage . setItem ( "ecurrency_account_context" , JSON . stringify ( defaultContext ) ) ;
97+ return ;
98+ }
99+
100+ // Validate the saved context
101+ let isValid = false ;
102+
103+ if ( accountContext . type === "user" ) {
104+ // User context must match current user ID
105+ isValid = accountContext . id === user . id ;
106+ } else if ( accountContext . type === "group" ) {
107+ // Group context must be in admin groups list
108+ isValid = adminGroups . some ( ( g : any ) => g . id === accountContext . id ) ;
109+ }
110+
111+ // If invalid, reset to user account
112+ if ( ! isValid ) {
113+ const defaultContext = { type : "user" as const , id : user . id } ;
114+ setAccountContext ( defaultContext ) ;
115+ localStorage . setItem ( "ecurrency_account_context" , JSON . stringify ( defaultContext ) ) ;
116+ }
117+ } , [ user , groups , accountContext ] ) ;
118+
119+ // Save account context to localStorage whenever it changes
120+ const handleAccountContextChange = ( context : { type : "user" | "group" ; id : string } | null ) => {
121+ // If null is passed, default to user account
122+ const finalContext = context || ( user ? { type : "user" as const , id : user . id } : null ) ;
123+ setAccountContext ( finalContext ) ;
124+ if ( finalContext ) {
125+ localStorage . setItem ( "ecurrency_account_context" , JSON . stringify ( finalContext ) ) ;
126+ } else {
127+ localStorage . removeItem ( "ecurrency_account_context" ) ;
128+ }
129+ } ;
130+
107131 const { data : totalSupplyData , isLoading : totalSupplyLoading } = useQuery ( {
108132 queryKey : [ "totalSupply" , currencyId ] ,
109133 queryFn : async ( ) => {
0 commit comments