This commit is contained in:
Ahmet Kilinc
2025-08-08 16:24:32 +01:00
parent 3bf2d45d6c
commit 7763e70ab0
2 changed files with 2 additions and 53 deletions

View File

@@ -1,48 +0,0 @@
import { useCallback, useEffect, useState } from 'react';
const COPIED_OTP_CODES_KEY = 'copiedOtpCodes';
export function useCopiedOtpCodes() {
const [copiedCodes, setCopiedCodes] = useState<Set<string>>(new Set());
useEffect(() => {
try {
const stored = localStorage.getItem(COPIED_OTP_CODES_KEY);
if (stored) {
setCopiedCodes(new Set(JSON.parse(stored)));
}
} catch (error) {
console.error('Failed to load copied OTP codes:', error);
}
}, []);
useEffect(() => {
try {
localStorage.setItem(COPIED_OTP_CODES_KEY, JSON.stringify([...copiedCodes]));
} catch (error) {
console.error('Failed to save copied OTP codes:', error);
}
}, [copiedCodes]);
const markAsCopied = useCallback((codeId: string) => {
setCopiedCodes((prev) => new Set([...prev, codeId]));
}, []);
const isCodeCopied = useCallback(
(codeId: string) => {
return copiedCodes.has(codeId);
},
[copiedCodes],
);
const clearAll = useCallback(() => {
setCopiedCodes(new Set());
}, []);
return {
markAsCopied,
isCodeCopied,
clearAll,
copiedCodes,
};
}

View File

@@ -1,4 +1,3 @@
import { useCopiedOtpCodes } from '@/hooks/use-copied-otp-codes';
import { useQuery, useMutation } from '@tanstack/react-query';
import { useCopyToClipboard } from './use-copy-to-clipboard';
import { useTRPC } from '@/providers/query-provider';
@@ -22,7 +21,6 @@ export interface OTPEmail {
export function useOTPEmails(connectionId: string) {
const trpc = useTRPC();
const { markAsCopied, isCodeCopied } = useCopiedOtpCodes();
const { copyToClipboard } = useCopyToClipboard();
const [copiedCodes, setCopiedCodes] = useState<Set<string>>(new Set());
@@ -77,7 +75,6 @@ export function useOTPEmails(connectionId: string) {
const handleCopyCode = useCallback(
async (otp: OTPEmail) => {
await copyToClipboard(otp?.code || '', otp.id);
markAsCopied(otp.id);
setCopiedCodes((prev) => new Set([...prev, otp.id]));
if (!otp.isCopied) {
@@ -88,7 +85,7 @@ export function useOTPEmails(connectionId: string) {
description: `${otp.service} code copied to clipboard`,
});
},
[copyToClipboard, markAsCopied, markAsCopiedMutation],
[copyToClipboard, markAsCopiedMutation],
);
const handleDeleteOTP = useCallback(
@@ -127,7 +124,7 @@ export function useOTPEmails(connectionId: string) {
handleDeleteOTP,
handleClearExpired,
hasExpiredOTPs,
isCodeCopied: (otpId: string) => copiedCodes.has(otpId) || isCodeCopied(otpId),
isCodeCopied: (otpId: string) => copiedCodes.has(otpId),
isDeletingOTP: deleteMutation.isPending,
isClearingExpired: deleteExpiredMutation.isPending,
};