u
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState } from 'react';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { render, Box, Text, useApp, useInput } from 'ink';
|
||||
import TextInput from 'ink-text-input';
|
||||
import chalk from 'chalk';
|
||||
@@ -48,6 +48,17 @@ const App = () => {
|
||||
const [email, setEmail] = useState('');
|
||||
const [message, setMessage] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [emails, setEmails] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (mode !== MODES.LIST) {
|
||||
return;
|
||||
}
|
||||
setEmails(null);
|
||||
googleAuth.listAllowedEmails()
|
||||
.then(setEmails)
|
||||
.catch((err) => finish(false, err.message));
|
||||
}, [mode]);
|
||||
|
||||
const finish = (success, text) => {
|
||||
if (success) {
|
||||
@@ -72,28 +83,28 @@ const App = () => {
|
||||
setMode(action);
|
||||
};
|
||||
|
||||
const handleAdd = () => {
|
||||
const handleAdd = async () => {
|
||||
try {
|
||||
const trimmed = email.trim();
|
||||
if (!trimmed || !trimmed.includes('@')) {
|
||||
finish(false, 'Invalid email address');
|
||||
return;
|
||||
}
|
||||
googleAuth.addAllowedEmail(trimmed);
|
||||
await googleAuth.addAllowedEmail(trimmed);
|
||||
finish(true, `Added ${trimmed.toLowerCase()} to google_auth`);
|
||||
} catch (err) {
|
||||
finish(false, err.message);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemove = () => {
|
||||
const handleRemove = async () => {
|
||||
try {
|
||||
const trimmed = email.trim();
|
||||
if (!trimmed) {
|
||||
finish(false, 'Email is required');
|
||||
return;
|
||||
}
|
||||
const removed = googleAuth.removeAllowedEmail(trimmed);
|
||||
const removed = await googleAuth.removeAllowedEmail(trimmed);
|
||||
finish(true, removed ? `Removed ${trimmed.toLowerCase()}` : `Email not found: ${trimmed}`);
|
||||
} catch (err) {
|
||||
finish(false, err.message);
|
||||
@@ -107,7 +118,9 @@ const App = () => {
|
||||
return e(Text, null, chalk.red(`Error: ${error}`));
|
||||
}
|
||||
if (mode === MODES.LIST) {
|
||||
const emails = googleAuth.listAllowedEmails();
|
||||
if (!emails) {
|
||||
return e(Text, null, chalk.gray('Loading...'));
|
||||
}
|
||||
if (emails.length === 0) {
|
||||
finish(true, 'No allowed emails in google_auth');
|
||||
return e(Text, null, chalk.yellow('No allowed emails...'));
|
||||
|
||||
Reference in New Issue
Block a user