mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 16:11:12 -05:00
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { supabase } from '@/lib/supabaseClient';
|
|
import type { EntityType, VersionDiff } from '@/types/versioning';
|
|
import { handleError } from '@/lib/errorHandler';
|
|
|
|
/**
|
|
* 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) {
|
|
handleError(err, {
|
|
action: 'Compare Versions',
|
|
metadata: { entityType, fromVersionId, toVersionId }
|
|
});
|
|
setError(err instanceof Error ? err.message : 'Failed to compare versions');
|
|
setDiff(null);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
fetchDiff();
|
|
}, [entityType, fromVersionId, toVersionId]);
|
|
|
|
return { diff, loading, error };
|
|
}
|