forked from MitulSonagara/truth-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MitulSonagara:master' into name
- Loading branch information
Showing
8 changed files
with
115 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
'use client' | ||
'use client'; | ||
import { decryptMessage } from '@/lib/crypto'; | ||
import { getPrivateKey } from '@/lib/indexedDB'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const useDecryptedMessages = (messages: string[]) => { | ||
const [decryptedMessages, setDecryptedMessages] = useState<string[]>([]); | ||
const [decryptedMessages, setDecryptedMessages] = useState<(string | null)[]>([]); | ||
const [loading, setLoading] = useState<boolean[]>(new Array(messages.length).fill(true)); // Initial loading states | ||
|
||
useEffect(() => { | ||
|
||
const decryptAllMessages = async () => { | ||
const privateKey = await getPrivateKey() | ||
const privateKey = await getPrivateKey(); | ||
const newLoading = new Array(messages.length).fill(true); // Create a new loading array | ||
|
||
const decrypted = await Promise.all(messages.map(async (message, index) => { | ||
if (privateKey) { | ||
try { | ||
const decryptedMessage = decryptMessage(privateKey, message); | ||
loading[index] = false; // Set loading to false for this index | ||
newLoading[index] = false; // Set loading to false for this index | ||
return decryptedMessage; | ||
} catch { | ||
loading[index] = false; // Set loading to false even if there's an error | ||
newLoading[index] = false; // Set loading to false even if there's an error | ||
return "Error decrypting message."; | ||
} | ||
} else { | ||
loading[index] = false; // Set loading to false if no private key | ||
newLoading[index] = false; // Set loading to false if no private key | ||
return "Message is encrypted."; | ||
} | ||
})); | ||
|
||
setDecryptedMessages(decrypted); | ||
setLoading(loading); | ||
setLoading(newLoading); // Update state with the new loading array | ||
}; | ||
|
||
decryptAllMessages(); | ||
}, [messages, loading]); | ||
decryptAllMessages(); // Call the function | ||
|
||
}, []); // Empty dependency array to run only on mount | ||
|
||
return { decryptedMessages, loading }; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters