All files / app/(tabs) bug.tsx

0% Statements 0/37
0% Branches 0/11
0% Functions 0/6
0% Lines 0/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                                                                                                                                             
import React, { useRef } from 'react';
import { View, StyleSheet, SafeAreaView } from 'react-native';
import { WebView, WebViewMessageEvent } from 'react-native-webview';
import { useRouter } from 'expo-router';
import { AppColors } from '@/constants/theme';
 
// Remote URL for the test page (loaded from server)
// Add cache-busting query param to force reload
const TEST_PAGE_URL = `https://wellnuo.smartlaunchhub.com/test-bridge.html?v=${Date.now()}`;
 
export default function BugScreen() {
  const router = useRouter();
  const webViewRef = useRef<WebView>(null);
 
  // Handle messages from WebView
  const handleMessage = (event: WebViewMessageEvent) => {
    try {
      const data = JSON.parse(event.nativeEvent.data);
 
      switch (data.action) {
        case 'NAVIGATE':
          handleNavigation(data.screen);
          break;
        case 'NATIVE_FEATURE':
          handleNativeFeature(data.feature);
          break;
        case 'CUSTOM':
          handleCustomMessage(data.payload);
          break;
        default:
          // Unknown action
          break;
      }
    } catch (error) {
      // Silently ignore parsing errors
    }
  };
 
  // Navigate to different screens
  const handleNavigation = (screen: string) => {
    switch (screen) {
      case 'beneficiaries':
        router.push('/(tabs)');
        break;
      case 'chat':
        router.push('/(tabs)/chat');
        break;
      case 'profile':
        router.push('/(tabs)/profile');
        break;
      default:
        // Send error back to WebView
        sendToWebView({ error: `Unknown screen: ${screen}` });
    }
  };
 
  // Handle native feature requests
  const handleNativeFeature = (feature: string) => {
    switch (feature) {
      case 'bluetooth':
        // TODO: Implement Bluetooth scanning screen
        sendToWebView({
          status: 'not_implemented',
          message: 'Bluetooth scanning will be implemented here'
        });
        break;
      case 'camera':
        // TODO: Implement camera
        sendToWebView({
          status: 'not_implemented',
          message: 'Camera will be implemented here'
        });
        break;
      default:
        sendToWebView({ error: `Unknown feature: ${feature}` });
    }
  };
 
  // Handle custom messages
  const handleCustomMessage = (payload: any) => {
    // Echo back with confirmation
    sendToWebView({
      status: 'received',
      echo: payload,
      processedAt: Date.now()
    });
  };
 
  // Send message TO WebView
  const sendToWebView = (data: object) => {
    const script = `
      window.dispatchEvent(new MessageEvent('message', {
        data: '${JSON.stringify(data)}'
      }));
      true;
    `;
    webViewRef.current?.injectJavaScript(script);
  };
 
  return (
    <SafeAreaView style={styles.container}>
      <WebView
        ref={webViewRef}
        source={{ uri: TEST_PAGE_URL }}
        style={styles.webview}
        onMessage={handleMessage}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        startInLoadingState={true}
        scalesPageToFit={true}
        cacheEnabled={false}
        incognito={true}
      />
    </SafeAreaView>
  );
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: AppColors.background,
  },
  webview: {
    flex: 1,
  },
});