mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 03:11:12 -05:00
Refactor code structure and remove redundant changes
This commit is contained in:
56
src-old/hooks/useVersionComparison.ts
Normal file
56
src-old/hooks/useVersionComparison.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user