Add reload button to credential and email pages (#541)

This commit is contained in:
Leendert de Borst
2025-01-31 12:44:14 +01:00
parent c05a47587b
commit d3caa2d0a9
5 changed files with 73 additions and 25 deletions

View File

@@ -20,8 +20,6 @@ const Header: React.FC<HeaderProps> = ({
authContext,
clientUrl,
handleRefresh,
toggleUserMenu,
isUserMenuOpen,
routes = []
}) => {
const navigate = useNavigate();
@@ -96,9 +94,7 @@ const Header: React.FC<HeaderProps> = ({
</button>
) : (
<UserMenu
handleRefresh={handleRefresh}
toggleUserMenu={toggleUserMenu}
isUserMenuOpen={isUserMenuOpen}
onRefresh={handleRefresh}
username={authContext.username}
/>
)}

View File

@@ -6,13 +6,11 @@ import { useLoading } from '../../context/LoadingContext';
interface UserMenuProps {
username: string;
onRefresh?: () => void;
showRefreshButton?: boolean;
}
export const UserMenu: React.FC<UserMenuProps> = ({
username,
onRefresh,
showRefreshButton = false
}) => {
const authContext = useAuth();
const [isUserMenuOpen, setIsUserMenuOpen] = useState(false);
@@ -54,18 +52,6 @@ export const UserMenu: React.FC<UserMenuProps> = ({
return (
<div className="relative flex items-center">
{showRefreshButton && (
<div role="status" className="px-2 flex items-center">
<div className="relative inline-flex items-center justify-center">
<button onClick={onRefresh} className="absolute p-2 hover:bg-gray-200 rounded-2xl">
<svg xmlns="http://www.w3.org/2000/svg" className="h-4 w-4 text-gray-400" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clipRule="evenodd" />
</svg>
</button>
</div>
</div>
)}
<div className="relative">
<button
ref={buttonRef}

View File

@@ -0,0 +1,29 @@
import React from 'react';
interface ReloadButtonProps {
onClick: () => void;
}
const ReloadButton: React.FC<ReloadButtonProps> = ({ onClick }) => {
return (
<div
role="status"
className="px-2 items-center"
>
<div className="relative inline-flex items-center">
<button onClick={onClick} className="absolute p-2 hover:bg-gray-200 rounded-2xl">
<svg xmlns="http://www.w3.org/2000/svg" className="h-4 w-4 text-gray-400" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clipRule="evenodd" />
</svg>
</button>
<svg aria-hidden="true" className="inline w-8 h-8 text-gray-200 dark:text-gray-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
</svg>
</div>
<span className="sr-only">Loading...</span>
</div>
);
};
export default ReloadButton;

View File

@@ -3,21 +3,24 @@ import { useDb } from '../context/DbContext';
import { Credential } from '../types/Credential';
import { Buffer } from 'buffer';
import { useNavigate } from 'react-router-dom';
import { useLoading } from '../context/LoadingContext';
import { useWebApi } from '../context/WebApiContext';
import EncryptionUtility from '../utils/EncryptionUtility';
import { VaultResponse } from '../types/webapi/VaultResponse';
import ReloadButton from '../components/ReloadButton';
/**
* Credentials list page.
*/
const CredentialsList: React.FC = () => {
const dbContext = useDb();
const webApi = useWebApi();
const [credentials, setCredentials] = useState<Credential[]>([]);
const navigate = useNavigate();
const { showLoading, hideLoading } = useLoading();
useEffect(() => {
console.log('loading credentials1');
console.log(dbContext);
if (!dbContext?.sqliteClient) return;
console.log('loading credentials2');
try {
const results = dbContext.sqliteClient.getAllCredentials();
setCredentials(results);
@@ -26,9 +29,39 @@ const CredentialsList: React.FC = () => {
}
}, [dbContext.sqliteClient]);
/**
* Retrieve latest vault and refresh the page.
*/
const onRefresh = async () => {
showLoading();
try {
// Make API call to get latest vault
const vaultResponseJson = await webApi.get('Vault') as VaultResponse;
// Get derived key from background worker
const passwordHashBase64 = await chrome.runtime.sendMessage({ type: 'GET_DERIVED_KEY' });
// Attempt to decrypt the blob
const decryptedBlob = await EncryptionUtility.symmetricDecrypt(
vaultResponseJson.vault.blob,
passwordHashBase64
);
// Initialize the SQLite context again with the newly retrieved decrypted blob
await dbContext.initializeDatabase(passwordHashBase64, decryptedBlob);
} catch (err) {
console.error('Refresh error:', err);
} finally {
hideLoading();
}
};
return (
<div>
<h2 className="text-gray-900 dark:text-white text-xl mb-4">Credentials</h2>
<div className="flex justify-between items-center">
<h2 className="text-gray-900 dark:text-white text-xl mb-4">Credentials</h2>
<ReloadButton onClick={onRefresh} />
</div>
{credentials.length === 0 ? (
<p className="text-gray-500 dark:text-gray-400">No credentials found</p>
) : (

View File

@@ -7,6 +7,7 @@ import LoadingSpinner from '../components/LoadingSpinner';
import { useMinDurationLoading } from '../hooks/useMinDurationLoading';
import EncryptionUtility from '../utils/EncryptionUtility';
import { Buffer } from 'buffer';
import ReloadButton from '../components/ReloadButton';
/**
* Emails list page.
*/
@@ -142,7 +143,10 @@ const EmailsList: React.FC = () => {
return (
<div>
<h2 className="text-gray-900 dark:text-white text-xl mb-4">Emails</h2>
<div className="flex justify-between items-center">
<h2 className="text-gray-900 dark:text-white text-xl mb-4">Emails</h2>
<ReloadButton onClick={loadEmails} />
</div>
<div className="space-y-2">
{emails.map((email) => (
<div