import * as React from "react"; import {useEffect, useState} from "react"; import {render} from "react-dom"; import * as keytar from 'keytar'; import {ipcRenderer} from 'electron'; import { Button, createMuiTheme, CssBaseline, IconButton, LinearProgress, makeStyles, Paper, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, TextField, ThemeProvider, useMediaQuery } from "@material-ui/core"; import {Alert, AlertTitle} from '@material-ui/lab'; import {blue} from "@material-ui/core/colors"; import {Add as AddIcon, Delete as DeleteIcon, Save as SaveIcon} from "@material-ui/icons"; const mainStyle = makeStyles((theme) => ({ root: { padding: theme.spacing(2), display: "flex", flexDirection: 'column', width: '100%', height: '100%', }, main: { flexGrow: 1, paddingTop: theme.spacing(2), paddingBottom: theme.spacing(2), }, input: { width: '100%', }, })); function CredentialRow({credential, index, onDelete, onUpdate}) { const [account, setAccount] = useState(credential?.account || ''); const [password, setPassword] = useState(credential?.password || ''); const style = mainStyle(); return ( { setAccount(e.target.value) }}/> { setPassword(e.target.value) }}/> await onUpdate(credential, index, {account, password})}> await onDelete(credential, index,)}> ); } function CredentialsTable({onError}) { const [credentials, setCredentials] = useState(null); async function loadItems() { try { const credentials = await keytar.findCredentials('docker-registry-ui'); for (const credential of credentials) { // fix for windows credential.password = credential.password.replace(/\000+/g, ''); } setCredentials(credentials); } catch (e) { onError(e.toString()); } } async function handleDelete(item, index) { // delete an item that has not been stored yet if (!item) { const newCredentials = [...credentials]; newCredentials.splice(index, 1); setCredentials(newCredentials); return; } try { await keytar.deletePassword('docker-registry-ui', item.account); await loadItems(); } catch (e) { onError(e.toString()); } } async function handleUpdate(oldCredentials, index, newCredentials) { try { await handleDelete(oldCredentials, index); await keytar.setPassword('docker-registry-ui', newCredentials.account, newCredentials.password); await loadItems(); } catch (e) { console.error("Error while updating key: ", e); onError(e.toString()); } } useEffect(() => { const load = async () => { await loadItems(); }; load(); return; }, []); if (credentials === null) { return } return ( Host of the registry including username Password { setCredentials([...credentials, null]) }} disabled={credentials.includes(null)}> {credentials.map((credential, index) => )}
); } function App() { const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)'); const [error, setError] = useState(); const classes = mainStyle(); const theme = React.useMemo( () => createMuiTheme({ palette: { type: prefersDarkMode ? 'dark' : 'light', primary: blue, }, }), [prefersDarkMode], ); return (
{error && { setError(null) }}> Error {error} }
); } render(, document.getElementById("root")); // @ts-ignore if (module.hot) { // @ts-ignore module.hot.accept(); }