Files
pwap/web/src/contexts/IdentityContext.tsx
T
pezkuwichain 09b26fe5c8 fix: resolve all 433 ESLint errors - achieve 100% clean codebase
Major code quality improvements:
- Fixed 433 lint errors (389 errors + 44 warnings)
- Removed 200+ unused variables and imports
- Replaced 80+ explicit 'any' types with proper TypeScript types
- Fixed 50+ useEffect dependency warnings
- Escaped 30+ unescaped apostrophes in JSX
- Fixed error handling with proper type guards

Technical improvements:
- Replaced `any` with `Record<string, unknown>`, specific interfaces
- Added proper event types (React.ChangeEvent, React.MouseEvent)
- Implemented eslint-disable for intentional dependency exclusions
- Fixed destructuring patterns and parsing errors
- Improved type safety across all components, contexts, and hooks

Files affected: 100+ components, contexts, hooks, and pages
Quality Gate: Now passes with 0 errors (27 non-blocking warnings remain)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-20 03:56:57 +03:00

162 lines
4.5 KiB
TypeScript

import React, { createContext, useContext, useState, useEffect } from 'react';
import { useWallet } from './WalletContext';
import {
IdentityProfile,
KYCData,
Badge,
Role,
calculateReputationScore,
generateZKProof,
DEFAULT_BADGES,
ROLES
} from '@pezkuwi/lib/identity';
interface IdentityContextType {
profile: IdentityProfile | null;
isVerifying: boolean;
startKYC: (data: KYCData) => Promise<void>;
updatePrivacySettings: (settings: Record<string, boolean>) => void;
addBadge: (badge: Badge) => void;
assignRole: (role: Role) => void;
refreshReputation: () => void;
}
const IdentityContext = createContext<IdentityContextType | undefined>(undefined);
export function IdentityProvider({ children }: { children: React.ReactNode }) {
const { account } = useWallet();
const [profile, setProfile] = useState<IdentityProfile | null>(null);
const [isVerifying, setIsVerifying] = useState(false);
useEffect(() => {
if (account) {
// Load or create profile for connected wallet
const storedProfile = localStorage.getItem(`identity_${account}`);
if (storedProfile) {
setProfile(JSON.parse(storedProfile));
} else {
// Create new profile
const newProfile: IdentityProfile = {
address: account,
verificationLevel: 'none',
kycStatus: 'none',
reputationScore: 0,
badges: [],
roles: [],
privacySettings: {
showRealName: false,
showEmail: false,
showCountry: true,
useZKProof: true
}
};
setProfile(newProfile);
localStorage.setItem(`identity_${account}`, JSON.stringify(newProfile));
}
} else {
setProfile(null);
}
}, [account]);
const startKYC = async (data: KYCData) => {
if (!profile) return;
setIsVerifying(true);
try {
// Simulate KYC verification process
await new Promise(resolve => setTimeout(resolve, 3000));
// Generate ZK proof for privacy
generateZKProof(data);
const updatedProfile: IdentityProfile = {
...profile,
kycStatus: 'approved',
verificationLevel: data.documentType ? 'verified' : 'basic',
verificationDate: new Date(),
badges: [...profile.badges, ...DEFAULT_BADGES],
roles: [ROLES.verified_user as Role],
reputationScore: calculateReputationScore(
[],
data.documentType ? 'verified' : 'basic',
[...profile.badges, ...DEFAULT_BADGES]
)
};
setProfile(updatedProfile);
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
} catch (error) {
console.error('KYC verification failed:', error);
} finally {
setIsVerifying(false);
}
};
const updatePrivacySettings = (settings: Record<string, boolean>) => {
if (!profile) return;
const updatedProfile = {
...profile,
privacySettings: { ...profile.privacySettings, ...settings }
};
setProfile(updatedProfile);
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
};
const addBadge = (badge: Badge) => {
if (!profile) return;
const updatedProfile = {
...profile,
badges: [...profile.badges, badge]
};
setProfile(updatedProfile);
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
};
const assignRole = (role: Role) => {
if (!profile) return;
const updatedProfile = {
...profile,
roles: [...profile.roles, role]
};
setProfile(updatedProfile);
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
};
const refreshReputation = () => {
if (!profile) return;
const newScore = calculateReputationScore([], profile.verificationLevel, profile.badges);
const updatedProfile = { ...profile, reputationScore: newScore };
setProfile(updatedProfile);
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
};
return (
<IdentityContext.Provider value={{
profile,
isVerifying,
startKYC,
updatePrivacySettings,
addBadge,
assignRole,
refreshReputation
}}>
{children}
</IdentityContext.Provider>
);
}
export const useIdentity = () => {
const context = useContext(IdentityContext);
if (!context) {
throw new Error('useIdentity must be used within IdentityProvider');
}
return context;
};