Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"@mocanetwork/airkit": "^1.6.0-beta.1",
"@mocanetwork/airkit": "^1.7.0",
"jose": "^6.0.13",
"react": "^19.1.0",
"react-dom": "^19.1.0",
Expand Down
61 changes: 38 additions & 23 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react";
import { useState, useEffect, useRef } from "react";
import { BrowserRouter as Router, Routes, Route, Navigate, useLocation } from "react-router-dom";
import "./App.css";
import CredentialIssuance from "./components/issuance/CredentialIssuance";
Expand All @@ -10,13 +10,31 @@ import { getEnvironmentConfig, type EnvironmentConfig } from "./config/environme
// Get partner IDs from environment variables
const ISSUER_PARTNER_ID = import.meta.env.VITE_ISSUER_PARTNER_ID || "66811bd6-dab9-41ef-8146-61f29d038a45";
const VERIFIER_PARTNER_ID = import.meta.env.VITE_VERIFIER_PARTNER_ID || "66811bd6-dab9-41ef-8146-61f29d038a45";
const enableLogging = true;

const defaultAirkitOptions: Parameters<
typeof AirService.prototype.init
>[0] = {
buildEnv: BUILD_ENV.STAGING,
enableLogging: true,
skipRehydration: false,
preloadCredential: true,
};

const ENV_OPTIONS = [
{ label: "Staging", value: BUILD_ENV.STAGING },
{ label: "Sandbox", value: BUILD_ENV.SANDBOX },
];

// Create singleton outside of component to avoid ref issues
let airServiceInstance: AirService | null = null;

const getAirService = (partnerId: string) => {
airServiceInstance ??= new AirService({
partnerId
});
return airServiceInstance;
};

// Component to get current flow title
const FlowTitle = () => {
const location = useLocation();
Expand Down Expand Up @@ -187,16 +205,16 @@ function AppRoutes({
}

function App() {
const [airService, setAirService] = useState<AirService | null>(null);
const [isInitialized, setIsInitialized] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [isLoggedIn, setIsLoggedIn] = useState(false);
const initAttempted = useRef(false);
const [userAddress, setUserAddress] = useState<string | null>(null);
const [currentEnv, setCurrentEnv] = useState<BUILD_ENV_TYPE>(
BUILD_ENV.SANDBOX
BUILD_ENV.STAGING
);
const [partnerId, setPartnerId] = useState<string>(ISSUER_PARTNER_ID);

const airService = getAirService(partnerId);
// Get environment config based on current environment
const environmentConfig = getEnvironmentConfig(currentEnv);

Expand All @@ -208,14 +226,15 @@ function App() {
}

try {
const service = new AirService({ partnerId: partnerIdToUse });
await service.init({ buildEnv: env as (typeof BUILD_ENV)[keyof typeof BUILD_ENV], enableLogging, skipRehydration: false });
setAirService(service);
await airService.init({
...defaultAirkitOptions,
buildEnv: env as (typeof BUILD_ENV)[keyof typeof BUILD_ENV]
});
setIsInitialized(true);
setIsLoggedIn(service.isLoggedIn);
setIsLoggedIn(airService.isLoggedIn);

if (service.isLoggedIn && service.loginResult) {
const result = service.loginResult;
if (airService.isLoggedIn && airService.loginResult) {
const result = airService.loginResult;
console.log("result @ initializeAirService", result);
if (result.abstractAccountAddress) {
setUserAddress(result.abstractAccountAddress || null);
Expand All @@ -242,30 +261,26 @@ function App() {
setUserAddress(null);
}
};
service.on(eventListener);
airService.on(eventListener);
} catch (err) {
console.error("Failed to initialize AIRKit service in nav bar:", err);
setIsInitialized(true); // Set to true to prevent infinite loading on error
}
};

// Re-initialize AIRKit when partner ID or environment changes
useEffect(() => {
initializeAirService(currentEnv, partnerId);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentEnv, partnerId]);

useEffect(() => {
// Only run on mount for initial load
// (the above effect will handle env and partner ID changes)
// eslint-disable-next-line react-hooks/exhaustive-deps
if (initAttempted.current) {
return;
}
initAttempted.current = true;
initializeAirService(currentEnv, partnerId);
return () => {
if (airService) {
airService.cleanUp();
}
};
}, []);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentEnv, partnerId]);

const handleLogin = async () => {
if (!airService) return;
Expand Down Expand Up @@ -316,4 +331,4 @@ function App() {
);
}

export default App;
export default App;