UNPKG

@oxyhq/services

Version:

OxyHQ Expo/React Native SDK — UI components, screens, and native features

245 lines (230 loc) 6.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useAssets = exports.setOxyAssetInstance = void 0; var _react = require("react"); var _assetStore = require("../stores/assetStore.js"); // Create a singleton instance for the hook let oxyInstance = null; const setOxyAssetInstance = instance => { oxyInstance = instance; }; /** * Hook for managing assets with Zustand store integration */ exports.setOxyAssetInstance = setOxyAssetInstance; const useAssets = () => { const { assets, uploadProgress, loading, errors, setAsset, setAssets, removeAsset, setUploadProgress, removeUploadProgress, addLink, removeLink, setUploading, setLinking, setDeleting, setUploadError, setLinkError, setDeleteError, clearErrors, getAssetsByApp, getAssetsByEntity, getAssetUsageCount, isAssetLinked, reset } = (0, _assetStore.useAssetStore)(); // Upload asset with progress tracking const upload = (0, _react.useCallback)(async (file, metadata) => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { clearErrors(); setUploading(true); // Upload file (progress tracking simplified for now) const result = await oxyInstance.assetUpload(file, undefined, metadata); // Update progress with final status if (result?.file) { const fileId = result.file.id; setUploadProgress(fileId, { fileId, uploaded: file.size, total: file.size, percentage: 100, status: 'complete' }); // Remove progress after a short delay setTimeout(() => { removeUploadProgress(fileId); }, 2000); } // Add asset to store if (result.file) { setAsset(result.file); return result.file; } return null; } catch (error) { setUploadError(error.message || 'Upload failed'); throw error; } finally { setUploading(false); } }, [clearErrors, setUploading, setUploadProgress, removeUploadProgress, setAsset, setUploadError]); // Link asset to entity const link = (0, _react.useCallback)(async (assetId, app, entityType, entityId) => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { clearErrors(); setLinking(true); // Auto-detect visibility for avatars and profile banners const visibility = entityType === 'avatar' || entityType === 'profile-banner' ? 'public' : undefined; const result = await oxyInstance.assetLink(assetId, app, entityType, entityId, visibility); if (result.file) { setAsset(result.file); } else { // If API doesn't return full file, update store optimistically addLink(assetId, { app, entityType, entityId, createdBy: '', // Will be filled by server createdAt: new Date().toISOString() }); } } catch (error) { setLinkError(error.message || 'Link failed'); throw error; } finally { setLinking(false); } }, [clearErrors, setLinking, setAsset, addLink, setLinkError]); // Unlink asset from entity const unlink = (0, _react.useCallback)(async (assetId, app, entityType, entityId) => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { clearErrors(); setLinking(true); const result = await oxyInstance.assetUnlink(assetId, app, entityType, entityId); if (result.file) { setAsset(result.file); } else { // Update store optimistically removeLink(assetId, app, entityType, entityId); } } catch (error) { setLinkError(error.message || 'Unlink failed'); throw error; } finally { setLinking(false); } }, [clearErrors, setLinking, setAsset, removeLink, setLinkError]); // Get asset URL const getUrl = (0, _react.useCallback)(async (assetId, variant, expiresIn) => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { const result = await oxyInstance.assetGetUrl(assetId, variant, expiresIn); return result.url; } catch (error) { throw error; } }, []); // Get asset metadata const getAsset = (0, _react.useCallback)(async assetId => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { const result = await oxyInstance.assetGet(assetId); if (result.file) { setAsset(result.file); return result.file; } throw new Error('Asset not found'); } catch (error) { throw error; } }, [setAsset]); // Delete asset const deleteAsset = (0, _react.useCallback)(async (assetId, force = false) => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { clearErrors(); setDeleting(true); await oxyInstance.assetDelete(assetId, force); removeAsset(assetId); } catch (error) { setDeleteError(error.message || 'Delete failed'); throw error; } finally { setDeleting(false); } }, [clearErrors, setDeleting, removeAsset, setDeleteError]); // Restore asset from trash const restore = (0, _react.useCallback)(async assetId => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { const result = await oxyInstance.assetRestore(assetId); if (result.file) { setAsset(result.file); } } catch (error) { throw error; } }, [setAsset]); // Get variants const getVariants = (0, _react.useCallback)(async assetId => { if (!oxyInstance) { throw new Error('OxyServices instance not configured. Call setOxyAssetInstance first.'); } try { return await oxyInstance.assetGetVariants(assetId); } catch (error) { throw error; } }, []); return { // State assets: Object.values(assets), uploadProgress, loading, errors, // Actions upload, link, unlink, getUrl, getAsset, deleteAsset, restore, getVariants, // Utility methods getAssetsByApp, getAssetsByEntity, getAssetUsageCount, isAssetLinked, // Store management clearErrors, reset }; }; exports.useAssets = useAssets; //# sourceMappingURL=useAssets.js.map