logs removed
This commit is contained in:
parent
57864e00da
commit
d041575600
@ -30,12 +30,6 @@ export class PushController {
|
||||
@Post('subscribe')
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
async subscribe(@Body() dto: SubscribeDto): Promise<{ success: boolean }> {
|
||||
console.log('Received push subscription:', {
|
||||
endpoint: dto.endpoint?.substring(0, 50) + '...',
|
||||
hasP256dh: !!dto.p256dh,
|
||||
hasAuth: !!dto.auth,
|
||||
userId: dto.userId,
|
||||
});
|
||||
await this.pushService.subscribe(dto);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@ -49,15 +49,11 @@ export class PushService implements OnModuleInit {
|
||||
}
|
||||
|
||||
async subscribe(dto: SubscribeDto): Promise<PushSubscriptionEntity> {
|
||||
this.logger.log('Subscribing push notification...');
|
||||
this.logger.debug(`Endpoint: ${dto.endpoint?.substring(0, 50)}...`);
|
||||
|
||||
const existing = await this.subscriptionRepo.findOne({
|
||||
where: { endpoint: dto.endpoint },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
this.logger.log('Subscription already exists, updating...');
|
||||
if (dto.userId && existing.userId !== dto.userId) {
|
||||
existing.userId = dto.userId;
|
||||
return this.subscriptionRepo.save(existing);
|
||||
@ -65,7 +61,6 @@ export class PushService implements OnModuleInit {
|
||||
return existing;
|
||||
}
|
||||
|
||||
this.logger.log('Creating new subscription...');
|
||||
const subscription = this.subscriptionRepo.create({
|
||||
endpoint: dto.endpoint,
|
||||
p256dh: dto.p256dh,
|
||||
@ -73,9 +68,7 @@ export class PushService implements OnModuleInit {
|
||||
userId: dto.userId ?? null,
|
||||
});
|
||||
|
||||
const saved = await this.subscriptionRepo.save(subscription);
|
||||
this.logger.log(`Subscription saved with ID: ${saved.id}`);
|
||||
return saved;
|
||||
return this.subscriptionRepo.save(subscription);
|
||||
}
|
||||
|
||||
async unsubscribe(dto: UnsubscribeDto): Promise<void> {
|
||||
|
||||
@ -6,7 +6,7 @@ import tseslint from 'typescript-eslint'
|
||||
import { defineConfig, globalIgnores } from 'eslint/config'
|
||||
|
||||
export default defineConfig([
|
||||
globalIgnores(['dist']),
|
||||
globalIgnores(['dist', 'dev-dist']),
|
||||
{
|
||||
files: ['**/*.{ts,tsx}'],
|
||||
extends: [
|
||||
|
||||
@ -32,19 +32,10 @@ export function NotificationBanner() {
|
||||
};
|
||||
|
||||
const handleSubscribe = async () => {
|
||||
console.log('handleSubscribe called');
|
||||
console.log('isSupported:', isSupported);
|
||||
console.log('isSubscribed:', isSubscribed);
|
||||
|
||||
try {
|
||||
const success = await subscribe();
|
||||
console.log('subscribe() returned:', success);
|
||||
if (success) {
|
||||
setIsVisible(false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('handleSubscribe error:', error);
|
||||
}
|
||||
};
|
||||
|
||||
if (!isVisible || isSubscribed || isDismissed) {
|
||||
|
||||
@ -40,25 +40,16 @@ export function usePushNotifications(): UsePushNotificationsReturn {
|
||||
const [permissionState, setPermissionState] = useState(getInitialPermission);
|
||||
const hasCheckedRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
console.log('usePushNotifications - isSupported:', checkPushSupport());
|
||||
console.log('usePushNotifications - permissionState:', getInitialPermission());
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isSupported || hasCheckedRef.current) return;
|
||||
hasCheckedRef.current = true;
|
||||
|
||||
console.log('Checking existing push subscription...');
|
||||
navigator.serviceWorker.ready
|
||||
.then((registration) => registration.pushManager.getSubscription())
|
||||
.then((subscription) => {
|
||||
console.log('Existing subscription:', subscription);
|
||||
setIsSubscribed(!!subscription);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error checking subscription:', error);
|
||||
});
|
||||
.catch(() => {});
|
||||
}, [isSupported]);
|
||||
|
||||
const requestPermission =
|
||||
@ -78,38 +69,26 @@ export function usePushNotifications(): UsePushNotificationsReturn {
|
||||
try {
|
||||
const permission = await requestPermission();
|
||||
if (permission !== 'granted') {
|
||||
console.log('Push permission denied');
|
||||
setIsLoading(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
const publicKey = await getVapidPublicKey();
|
||||
if (!publicKey) {
|
||||
console.error('VAPID public key not available');
|
||||
setIsLoading(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('Got VAPID public key, subscribing...');
|
||||
|
||||
// Check if service worker is already ready
|
||||
console.log('Checking service worker status...');
|
||||
const swController = navigator.serviceWorker.controller;
|
||||
console.log('Service worker controller:', swController);
|
||||
|
||||
// Wait for service worker to be ready with timeout
|
||||
const registration = await Promise.race([
|
||||
navigator.serviceWorker.ready,
|
||||
new Promise<ServiceWorkerRegistration>((_, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(new Error('Service worker ready timeout after 10s'));
|
||||
reject(new Error('Service worker ready timeout'));
|
||||
}, 10000);
|
||||
}),
|
||||
]);
|
||||
console.log('Service worker ready:', registration);
|
||||
|
||||
if (!registration.pushManager) {
|
||||
console.error('PushManager not available in service worker registration');
|
||||
setIsLoading(false);
|
||||
return false;
|
||||
}
|
||||
@ -117,31 +96,24 @@ export function usePushNotifications(): UsePushNotificationsReturn {
|
||||
let subscription = await registration.pushManager.getSubscription();
|
||||
|
||||
if (!subscription) {
|
||||
console.log('Creating new push subscription...');
|
||||
subscription = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: urlBase64ToUint8Array(publicKey),
|
||||
});
|
||||
console.log('Push subscription created');
|
||||
} else {
|
||||
console.log('Existing push subscription found');
|
||||
}
|
||||
|
||||
const subJson = subscription.toJSON();
|
||||
console.log('Subscription JSON:', subJson);
|
||||
|
||||
if (
|
||||
!subJson.endpoint ||
|
||||
!subJson.keys?.p256dh ||
|
||||
!subJson.keys?.auth
|
||||
) {
|
||||
console.error('Invalid subscription data:', subJson);
|
||||
setIsLoading(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
const success = await subscribeToPush(subJson as PushSubscriptionData);
|
||||
console.log('Subscribe to push result:', success);
|
||||
|
||||
if (success) {
|
||||
setIsSubscribed(true);
|
||||
@ -150,8 +122,7 @@ export function usePushNotifications(): UsePushNotificationsReturn {
|
||||
|
||||
setIsLoading(false);
|
||||
return success;
|
||||
} catch (error) {
|
||||
console.error('Error subscribing to push notifications:', error);
|
||||
} catch {
|
||||
setIsLoading(false);
|
||||
return false;
|
||||
}
|
||||
@ -175,8 +146,7 @@ export function usePushNotifications(): UsePushNotificationsReturn {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
setIsLoading(false);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error unsubscribing from push notifications:', error);
|
||||
} catch {
|
||||
setIsLoading(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -20,8 +20,7 @@ export async function getVapidPublicKey(): Promise<string | null> {
|
||||
}
|
||||
const data: VapidPublicKeyResponse = await response.json();
|
||||
return data.publicKey;
|
||||
} catch (error) {
|
||||
console.error('Error fetching VAPID public key:', error);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -30,39 +29,20 @@ export async function subscribeToPush(
|
||||
subscription: PushSubscriptionData,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
console.log('Sending subscription to server:', {
|
||||
endpoint: subscription.endpoint,
|
||||
hasKeys: !!subscription.keys,
|
||||
});
|
||||
|
||||
const payload = {
|
||||
endpoint: subscription.endpoint,
|
||||
p256dh: subscription.keys.p256dh,
|
||||
auth: subscription.keys.auth,
|
||||
};
|
||||
|
||||
console.log('Payload:', payload);
|
||||
console.log('API URL:', `${API_BASE_URL}/push/subscribe`);
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/push/subscribe`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
body: JSON.stringify({
|
||||
endpoint: subscription.endpoint,
|
||||
p256dh: subscription.keys.p256dh,
|
||||
auth: subscription.keys.auth,
|
||||
}),
|
||||
});
|
||||
|
||||
console.log('Subscribe response status:', response.status);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('Subscribe failed:', errorText);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error subscribing to push:', error);
|
||||
return response.ok;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -79,8 +59,7 @@ export async function unsubscribeFromPush(
|
||||
body: JSON.stringify({ endpoint }),
|
||||
});
|
||||
return response.ok;
|
||||
} catch (error) {
|
||||
console.error('Error unsubscribing from push:', error);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,22 +11,12 @@ initializeTheme()
|
||||
|
||||
const queryClient = new QueryClient()
|
||||
|
||||
// Register service worker
|
||||
const updateSW = registerSW({
|
||||
onNeedRefresh() {
|
||||
if (confirm('New content available. Reload?')) {
|
||||
updateSW(true)
|
||||
}
|
||||
},
|
||||
onOfflineReady() {
|
||||
console.log('App ready to work offline')
|
||||
},
|
||||
onRegistered(registration) {
|
||||
console.log('SW Registered:', registration?.scope)
|
||||
},
|
||||
onRegisterError(error) {
|
||||
console.error('SW registration error:', error)
|
||||
},
|
||||
})
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user