UNPKG

code-craft-studio

Version:

A comprehensive QR code and barcode scanning/generation library for React. Works with or without Capacitor. Supports 22+ QR data types and 14+ barcode formats (EAN, UPC, Code 128, etc.), with customizable designs, analytics, and React components. Provider

95 lines 5.76 kB
import { useState, useEffect, useCallback, useMemo } from 'react'; import { getPlatform, platformDetector } from '../platforms'; export function useCodeCraftStudio(options = {}) { const { autoInitialize = true, onError } = options; const [platform, setPlatform] = useState(null); const [isReady, setIsReady] = useState(false); const [error, setError] = useState(null); // Platform detection const isCapacitor = useMemo(() => platformDetector.isCapacitor(), []); const isWeb = useMemo(() => platformDetector.isWeb(), []); const isNative = useMemo(() => platformDetector.isNative(), []); // Initialize platform useEffect(() => { if (!autoInitialize) return; let mounted = true; const init = async () => { try { const platformAdapter = await getPlatform(); if (mounted) { setPlatform(platformAdapter); setIsReady(true); } } catch (err) { const error = err instanceof Error ? err : new Error('Failed to initialize platform'); if (mounted) { setError(error); onError === null || onError === void 0 ? void 0 : onError(error); } } }; init(); return () => { mounted = false; }; }, [autoInitialize, onError]); // Method wrappers with error handling const wrapMethod = useCallback((method) => { return async (...args) => { if (!platform) { throw new Error('Platform not initialized. Make sure useCodeCraftStudio is properly initialized.'); } try { return await method.apply(platform, args); } catch (err) { const error = err instanceof Error ? err : new Error('Operation failed'); setError(error); onError === null || onError === void 0 ? void 0 : onError(error); throw error; } }; }, [platform, onError]); // Wrapped methods const scanQRCode = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.scanQRCode.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const generateQRCode = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.generateQRCode.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const validateQRData = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.validateQRData.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const scanBarcode = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.scanBarcode.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const generateBarcode = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.generateBarcode.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const validateBarcode = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.validateBarcode.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const saveToHistory = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.saveToHistory.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const getHistory = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.getHistory.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const clearHistory = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.clearHistory.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const getAnalytics = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.getAnalytics.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const exportCode = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.exportCode.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const checkPermissions = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.checkPermissions.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); const requestPermissions = useMemo(() => wrapMethod((platform === null || platform === void 0 ? void 0 : platform.requestPermissions.bind(platform)) || (async () => { throw new Error('Not initialized'); })), [platform, wrapMethod]); return { // Platform info isReady, platform, capabilities: (platform === null || platform === void 0 ? void 0 : platform.capabilities) || null, isCapacitor, isWeb, isNative, // Methods scanQRCode, generateQRCode, validateQRData, scanBarcode, generateBarcode, validateBarcode, saveToHistory, getHistory, clearHistory, getAnalytics, exportCode, checkPermissions, requestPermissions, // Error state error }; } //# sourceMappingURL=useCodeCraftStudio.js.map