mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 13:51:14 -05:00
Refactor versioning utility functions
This commit is contained in:
52
src/hooks/useVersionComparison.ts
Normal file
52
src/hooks/useVersionComparison.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import type { EntityType, VersionDiff } from '@/types/versioning';
|
||||
|
||||
/**
|
||||
* Hook to compare two versions of an entity and get the diff
|
||||
* Uses the relational version tables for type-safe comparison
|
||||
*/
|
||||
export function useVersionComparison(
|
||||
entityType: EntityType,
|
||||
fromVersionId: string | null,
|
||||
toVersionId: string | null
|
||||
) {
|
||||
const [diff, setDiff] = useState<VersionDiff | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!fromVersionId || !toVersionId || !entityType) {
|
||||
setDiff(null);
|
||||
return;
|
||||
}
|
||||
|
||||
async function fetchDiff() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// Use the database function to get diff
|
||||
const { data, error: rpcError } = await supabase.rpc('get_version_diff', {
|
||||
p_entity_type: entityType,
|
||||
p_from_version_id: fromVersionId,
|
||||
p_to_version_id: toVersionId
|
||||
});
|
||||
|
||||
if (rpcError) throw rpcError;
|
||||
|
||||
setDiff(data as VersionDiff);
|
||||
} catch (err) {
|
||||
console.error('Error fetching version diff:', err);
|
||||
setError(err instanceof Error ? err.message : 'Failed to compare versions');
|
||||
setDiff(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
fetchDiff();
|
||||
}, [entityType, fromVersionId, toVersionId]);
|
||||
|
||||
return { diff, loading, error };
|
||||
}
|
||||
Reference in New Issue
Block a user