2025-03-17 23:37:08 -04:00
|
|
|
// lib/hooks/usePOWRpacks.ts
|
2025-03-13 14:02:36 -04:00
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
|
|
import { Alert } from 'react-native';
|
|
|
|
import { usePOWRPackService } from '@/components/DatabaseProvider';
|
|
|
|
import { useNDK } from '@/lib/hooks/useNDK';
|
|
|
|
import { POWRPackWithContent, POWRPackImport, POWRPackSelection } from '@/types/powr-pack';
|
2025-03-17 23:37:08 -04:00
|
|
|
import { usePackRefresh } from '@/lib/stores/libraryStore';
|
2025-03-13 14:02:36 -04:00
|
|
|
|
|
|
|
export function usePOWRPacks() {
|
|
|
|
const powrPackService = usePOWRPackService();
|
|
|
|
const { ndk } = useNDK();
|
2025-03-17 23:37:08 -04:00
|
|
|
const { refreshCount, refreshPacks, isLoading, setLoading } = usePackRefresh();
|
|
|
|
|
2025-03-13 14:02:36 -04:00
|
|
|
const [packs, setPacks] = useState<POWRPackWithContent[]>([]);
|
|
|
|
|
|
|
|
// Load all packs
|
|
|
|
const loadPacks = useCallback(async () => {
|
2025-03-17 23:37:08 -04:00
|
|
|
setLoading(true);
|
2025-03-13 14:02:36 -04:00
|
|
|
try {
|
|
|
|
const importedPacks = await powrPackService.getImportedPacks();
|
|
|
|
setPacks(importedPacks);
|
|
|
|
return importedPacks;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error loading POWR packs:', error);
|
|
|
|
return [];
|
|
|
|
} finally {
|
2025-03-17 23:37:08 -04:00
|
|
|
setLoading(false);
|
2025-03-13 14:02:36 -04:00
|
|
|
}
|
2025-03-17 23:37:08 -04:00
|
|
|
}, [powrPackService, setLoading]);
|
2025-03-13 14:02:36 -04:00
|
|
|
|
2025-03-17 23:37:08 -04:00
|
|
|
// Load packs when refreshCount changes
|
2025-03-13 14:02:36 -04:00
|
|
|
useEffect(() => {
|
|
|
|
loadPacks();
|
2025-03-17 23:37:08 -04:00
|
|
|
}, [refreshCount, loadPacks]);
|
2025-03-13 14:02:36 -04:00
|
|
|
|
|
|
|
// Fetch a pack from an naddr
|
|
|
|
const fetchPack = useCallback(async (naddr: string): Promise<POWRPackImport | null> => {
|
|
|
|
if (!ndk) {
|
|
|
|
Alert.alert('Error', 'NDK is not initialized');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await powrPackService.fetchPackFromNaddr(naddr, ndk);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching pack:', error);
|
|
|
|
Alert.alert('Error', error instanceof Error ? error.message : 'Failed to fetch pack');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, [ndk, powrPackService]);
|
|
|
|
|
|
|
|
// Import a pack with selected items
|
|
|
|
const importPack = useCallback(async (packImport: POWRPackImport, selection: POWRPackSelection) => {
|
|
|
|
try {
|
|
|
|
await powrPackService.importPack(packImport, selection);
|
2025-03-17 23:37:08 -04:00
|
|
|
// No need to call loadPacks here as the store refresh will trigger it
|
2025-03-13 14:02:36 -04:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error importing pack:', error);
|
|
|
|
Alert.alert('Error', error instanceof Error ? error.message : 'Failed to import pack');
|
|
|
|
return false;
|
|
|
|
}
|
2025-03-17 23:37:08 -04:00
|
|
|
}, [powrPackService]);
|
2025-03-13 14:02:36 -04:00
|
|
|
|
|
|
|
// Delete a pack
|
2025-03-17 20:20:16 -04:00
|
|
|
const deletePack = useCallback(async (packId: string) => {
|
2025-03-13 14:02:36 -04:00
|
|
|
try {
|
2025-03-17 20:20:16 -04:00
|
|
|
// Always delete everything
|
|
|
|
await powrPackService.deletePack(packId, false);
|
2025-03-17 23:37:08 -04:00
|
|
|
// No need to call loadPacks here as the store refresh will trigger it
|
2025-03-13 14:02:36 -04:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error deleting pack:', error);
|
|
|
|
Alert.alert('Error', error instanceof Error ? error.message : 'Failed to delete pack');
|
|
|
|
return false;
|
|
|
|
}
|
2025-03-17 23:37:08 -04:00
|
|
|
}, [powrPackService]);
|
2025-03-13 14:02:36 -04:00
|
|
|
|
|
|
|
// Helper to copy pack address to clipboard (for future implementation)
|
|
|
|
const copyPackAddress = useCallback((naddr: string) => {
|
|
|
|
// We would implement clipboard functionality here
|
|
|
|
// For now, this is a placeholder for future enhancement
|
|
|
|
console.log('Would copy to clipboard:', naddr);
|
|
|
|
return true;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
|
|
|
packs,
|
|
|
|
isLoading,
|
|
|
|
loadPacks,
|
|
|
|
fetchPack,
|
|
|
|
importPack,
|
|
|
|
deletePack,
|
2025-03-17 23:37:08 -04:00
|
|
|
copyPackAddress,
|
|
|
|
refreshPacks // Return the refresh function from the store
|
2025-03-13 14:02:36 -04:00
|
|
|
};
|
|
|
|
}
|