public shares now work

This commit is contained in:
2025-12-12 22:06:05 +05:00
parent 1f088c8fb0
commit f5370aa7f9
9 changed files with 117 additions and 79 deletions

View File

@@ -37,9 +37,14 @@ export function ItemContextMenu({ item, position, onClose, onUpdate }: ItemConte
}, [onClose]);
const handleDelete = async () => {
console.log('DELETE BUTTON CLICKED - Starting delete process');
console.log('Item to delete:', { id: item.id, type: item.type, name: item.properties.name });
console.log('Delete connected cables:', deleteConnectedCables);
try {
// If device with connections and user wants to delete connected cables
if (isDevice && deleteConnectedCables && hasConnections) {
console.log('Deleting connected cables first...');
// First delete all connected cables
const { mapItemService: itemService } = await import('../../services/mapItemService');
const allItems = await itemService.getMapItems(item.map_id);
@@ -50,18 +55,27 @@ export function ItemContextMenu({ item, position, onClose, onUpdate }: ItemConte
i.type === 'cable' && connectedCableIds.includes(i.id)
);
console.log(`Found ${cablesToDelete.length} cables to delete:`, cablesToDelete.map(c => c.id));
// Delete each cable
for (const cable of cablesToDelete) {
console.log(`Deleting cable ${cable.id}...`);
await itemService.deleteMapItem(item.map_id, cable.id);
console.log(`Cable ${cable.id} deleted successfully`);
}
}
// Delete the device/item itself
console.log(`Deleting main item ${item.id}...`);
await mapItemService.deleteMapItem(item.map_id, item.id);
console.log(`Item ${item.id} deleted successfully`);
onUpdate();
onClose();
console.log('Delete process completed successfully');
} catch (error) {
console.error('Failed to delete item:', error);
console.error('Error details:', { error, item_id: item.id, map_id: item.map_id });
alert('Failed to delete item');
}
};

View File

@@ -54,8 +54,11 @@ export function ShareDialog({ mapId, onClose }: ShareDialogProps) {
});
setNewUserId('');
await loadShares();
alert('Map shared successfully!');
} catch (error: any) {
alert(error.response?.data?.detail || 'Failed to share map');
console.error('Share error:', error);
const message = error.response?.data?.detail || error.message || 'Failed to share map';
alert(message);
} finally {
setLoading(false);
}
@@ -68,8 +71,11 @@ export function ShareDialog({ mapId, onClose }: ShareDialogProps) {
permission: newLinkPermission,
});
await loadLinks();
alert('Share link created successfully!');
} catch (error: any) {
alert(error.response?.data?.detail || 'Failed to create share link');
console.error('Create link error:', error);
const message = error.response?.data?.detail || error.message || 'Failed to create share link';
alert(message);
} finally {
setLoading(false);
}
@@ -82,7 +88,9 @@ export function ShareDialog({ mapId, onClose }: ShareDialogProps) {
await mapShareService.revokeShare(mapId, shareId);
await loadShares();
} catch (error: any) {
alert(error.response?.data?.detail || 'Failed to revoke share');
console.error('Revoke share error:', error);
const message = error.response?.data?.detail || error.message || 'Failed to revoke share';
alert(message);
}
};
@@ -93,7 +101,9 @@ export function ShareDialog({ mapId, onClose }: ShareDialogProps) {
await mapShareService.deleteShareLink(mapId, linkId);
await loadLinks();
} catch (error: any) {
alert(error.response?.data?.detail || 'Failed to delete link');
console.error('Delete link error:', error);
const message = error.response?.data?.detail || error.message || 'Failed to delete link';
alert(message);
}
};

View File

@@ -29,7 +29,24 @@ export function useMapWebSocket({
const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const reconnectAttemptsRef = useRef(0);
// Use refs for callbacks to avoid reconnecting when they change
const onItemCreatedRef = useRef(onItemCreated);
const onItemUpdatedRef = useRef(onItemUpdated);
const onItemDeletedRef = useRef(onItemDeleted);
const onConnectedRef = useRef(onConnected);
// Update refs when callbacks change
useEffect(() => {
onItemCreatedRef.current = onItemCreated;
onItemUpdatedRef.current = onItemUpdated;
onItemDeletedRef.current = onItemDeleted;
onConnectedRef.current = onConnected;
}, [onItemCreated, onItemUpdated, onItemDeleted, onConnected]);
useEffect(() => {
// Skip WebSocket if no mapId
if (!mapId) return;
const connect = () => {
// Get the token for authenticated users
const token = authService.getAccessToken();
@@ -72,19 +89,19 @@ export function useMapWebSocket({
switch (message.type) {
case 'connected':
setPermission(message.data.permission);
onConnected?.(message.data);
onConnectedRef.current?.(message.data);
break;
case 'item_created':
onItemCreated?.(message.data);
onItemCreatedRef.current?.(message.data);
break;
case 'item_updated':
onItemUpdated?.(message.data);
onItemUpdatedRef.current?.(message.data);
break;
case 'item_deleted':
onItemDeleted?.(message.data.id);
onItemDeletedRef.current?.(message.data.id);
break;
default:
@@ -132,7 +149,7 @@ export function useMapWebSocket({
wsRef.current = null;
}
};
}, [mapId, shareToken, onItemCreated, onItemUpdated, onItemDeleted, onConnected]);
}, [mapId, shareToken]); // Only reconnect when mapId or shareToken changes
return {
isConnected,