@@ -8,31 +8,58 @@ const US_CODES = ["USA", "ASM", "GUM", "PRI", "VIR", "MNP", "UMI"]; // US and te
88 * Note: The Swift code returns "UNKNOWN" if it can't determine the region.
99 */
1010export async function getStoreRegion ( ) : Promise < string > {
11- console . log ( "[Region Gate] Attempting to invoke plugin:store|get_region..." ) ;
11+ // First, check if we're running on iOS
12+ let isIOSPlatform = false ;
1213 try {
14+ console . log ( "[Region Gate] Checking if running on iOS..." ) ;
15+ const { isTauri } = await import ( "@tauri-apps/api/core" ) ;
16+ const isTauriEnv = await isTauri ( ) ;
17+
18+ if ( isTauriEnv ) {
19+ const { type } = await import ( "@tauri-apps/plugin-os" ) ;
20+ const platform = await type ( ) ;
21+ isIOSPlatform = platform === "ios" ;
22+ console . log ( "[Region Gate] Platform check:" , platform , "iOS:" , isIOSPlatform ) ;
23+ } else {
24+ console . log ( "[Region Gate] Not in Tauri environment" ) ;
25+ return "NOT_TAURI" ;
26+ }
27+ } catch ( e ) {
28+ console . error ( "[Region Gate] Error checking platform:" , e ) ;
29+ return "PLATFORM_ERROR" ;
30+ }
31+
32+ // If not on iOS, no need to call the plugin
33+ if ( ! isIOSPlatform ) {
34+ console . log ( "[Region Gate] Not on iOS, skipping region check" ) ;
35+ return "NOT_IOS" ;
36+ }
37+
38+ // We're on iOS, try to get the App Store region
39+ console . log ( "[Region Gate] On iOS, invoking store plugin..." ) ;
40+ try {
41+ // Using the same pattern as sign-in-with-apple
1342 const code = await invoke < string > ( "plugin:store|get_region" ) ;
14- console . log ( "[Region Gate] Success! Store region code:" , code ) ;
43+ console . log ( "[Region Gate] Successfully got region code:" , code ) ;
1544 return code ;
1645 } catch ( error ) {
17- console . error ( "[Region Gate] Error invoking plugin:" , error ) ;
18- console . error ( "[Region Gate] Stack trace :" , new Error ( ) . stack ) ;
46+ // Format error consistently with other plugin patterns
47+ console . error ( "[Region Gate] Error getting region :" , error ) ;
1948
20- // Return the error message for diagnosis
21- let errorMsg = "Error: " ;
2249 if ( error instanceof Error ) {
23- errorMsg += error . message ;
50+ console . error ( "[Region Gate] Error name:" , error . name ) ;
51+ console . error ( "[Region Gate] Error message:" , error . message ) ;
52+ return `ERROR:${ error . name } :${ error . message . substring ( 0 , 50 ) } ` ;
2453 } else if ( typeof error === "string" ) {
25- errorMsg += error ;
54+ return `ERROR: ${ error . substring ( 0 , 50 ) } ` ;
2655 } else {
27- errorMsg += JSON . stringify ( error ) ;
56+ try {
57+ const errorStr = JSON . stringify ( error ) . substring ( 0 , 50 ) ;
58+ return `ERROR:JSON:${ errorStr } ` ;
59+ } catch {
60+ return "ERROR:UNKNOWN" ;
61+ }
2862 }
29-
30- // Truncate if the error message is too long
31- if ( errorMsg . length > 30 ) {
32- errorMsg = errorMsg . substring ( 0 , 27 ) + "..." ;
33- }
34-
35- return errorMsg ;
3663 }
3764}
3865
0 commit comments