Quick fixes to lang plus mmap realtime

This commit is contained in:
AlacrisDevs
2026-02-08 23:30:09 +02:00
parent f2384bceb8
commit ce80dc6d75
5 changed files with 438 additions and 242 deletions

View File

@@ -250,3 +250,50 @@ export async function deleteMapShape(supabase: SupabaseClient, shapeId: string):
if (error) throw error;
}
// ── Realtime ──
export interface RealtimeMapPayload<T> {
event: 'INSERT' | 'UPDATE' | 'DELETE';
new: T;
old: Partial<T>;
}
export function subscribeToMapLayers(
supabase: SupabaseClient,
layerIds: string[],
onPinChange: (payload: RealtimeMapPayload<MapPin>) => void,
onShapeChange: (payload: RealtimeMapPayload<MapShape>) => void,
) {
const layerIdSet = new Set(layerIds);
const channel = supabase.channel(`map:${layerIds.join(',')}`);
channel
.on('postgres_changes', { event: '*', schema: 'public', table: 'map_pins' },
(payload) => {
const pin = (payload.new ?? payload.old) as Partial<MapPin>;
const lid = pin.layer_id ?? (payload.old as Partial<MapPin>)?.layer_id;
if (lid && !layerIdSet.has(lid)) return;
onPinChange({
event: payload.eventType as 'INSERT' | 'UPDATE' | 'DELETE',
new: payload.new as MapPin,
old: payload.old as Partial<MapPin>,
});
}
)
.on('postgres_changes', { event: '*', schema: 'public', table: 'map_shapes' },
(payload) => {
const shape = (payload.new ?? payload.old) as Partial<MapShape>;
const lid = shape.layer_id ?? (payload.old as Partial<MapShape>)?.layer_id;
if (lid && !layerIdSet.has(lid)) return;
onShapeChange({
event: payload.eventType as 'INSERT' | 'UPDATE' | 'DELETE',
new: payload.new as MapShape,
old: payload.old as Partial<MapShape>,
});
}
)
.subscribe();
return channel;
}