mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 21:11:13 -05:00
feat: Implement comprehensive request tracking and state management
This commit is contained in:
108
src/lib/moderation/lockMonitor.ts
Normal file
108
src/lib/moderation/lockMonitor.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* Moderation Lock Monitor
|
||||
*
|
||||
* Monitors lock expiry and provides automatic renewal prompts for moderators.
|
||||
* Prevents loss of work due to expired locks.
|
||||
*/
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import type { ModerationState } from '../moderationStateMachine';
|
||||
import type { ModerationAction } from '../moderationStateMachine';
|
||||
import { hasActiveLock, needsLockRenewal } from '../moderationStateMachine';
|
||||
import { toast } from '@/hooks/use-toast';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { logger } from '../logger';
|
||||
|
||||
/**
|
||||
* Hook to monitor lock status and warn about expiry
|
||||
*
|
||||
* @param state - Current moderation state
|
||||
* @param dispatch - State machine dispatch function
|
||||
* @param itemId - ID of the locked item (optional, for manual extension)
|
||||
*/
|
||||
export function useLockMonitor(
|
||||
state: ModerationState,
|
||||
dispatch: React.Dispatch<ModerationAction>,
|
||||
itemId?: string
|
||||
) {
|
||||
useEffect(() => {
|
||||
if (!hasActiveLock(state)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const checkInterval = setInterval(() => {
|
||||
if (needsLockRenewal(state)) {
|
||||
logger.warn('Lock expiring soon', {
|
||||
action: 'lock_expiry_warning',
|
||||
itemId,
|
||||
lockExpires: state.status === 'locked' || state.status === 'reviewing'
|
||||
? state.lockExpires
|
||||
: undefined,
|
||||
});
|
||||
|
||||
// Dispatch lock expiry warning
|
||||
dispatch({ type: 'LOCK_EXPIRED' });
|
||||
|
||||
// Show toast with extension option
|
||||
toast({
|
||||
title: 'Lock Expiring Soon',
|
||||
description: 'Your lock on this submission will expire in less than 2 minutes. Click to extend.',
|
||||
variant: 'default',
|
||||
});
|
||||
}
|
||||
}, 30000); // Check every 30 seconds
|
||||
|
||||
return () => clearInterval(checkInterval);
|
||||
}, [state, dispatch, itemId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the lock on a submission
|
||||
*
|
||||
* @param submissionId - Submission ID
|
||||
* @param dispatch - State machine dispatch function
|
||||
*/
|
||||
async function handleExtendLock(
|
||||
submissionId: string,
|
||||
dispatch: React.Dispatch<ModerationAction>
|
||||
) {
|
||||
try {
|
||||
// Call Supabase to extend lock (assumes 15 minute extension)
|
||||
const { error } = await supabase
|
||||
.from('content_submissions')
|
||||
.update({
|
||||
locked_until: new Date(Date.now() + 15 * 60 * 1000).toISOString(),
|
||||
})
|
||||
.eq('id', submissionId);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
// Update state machine with new lock time
|
||||
dispatch({
|
||||
type: 'LOCK_ACQUIRED',
|
||||
payload: { lockExpires: new Date(Date.now() + 15 * 60 * 1000).toISOString() },
|
||||
});
|
||||
|
||||
toast({
|
||||
title: 'Lock Extended',
|
||||
description: 'You have 15 more minutes to complete your review.',
|
||||
});
|
||||
|
||||
logger.info('Lock extended successfully', {
|
||||
action: 'lock_extended',
|
||||
submissionId,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to extend lock', {
|
||||
action: 'extend_lock_error',
|
||||
submissionId,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
|
||||
toast({
|
||||
title: 'Extension Failed',
|
||||
description: 'Could not extend lock. Please save your work and re-claim the item.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user