Spaces:
Runtime error
Runtime error
File size: 4,918 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import React, { useState, useEffect } from "react";
import { render, Box, Text, useInput } from "ink";
import Spinner from "ink-spinner";
import { StatusBadge } from "../tui-components/StatusBadge.jsx";
import { ConfirmDialog } from "../tui-components/ConfirmDialog.jsx";
const PHASE = {
WAITING: "waiting",
POLLING: "polling",
DONE: "done",
FAILED: "failed",
CANCELLED: "cancelled",
};
let _globalDone = null;
let _globalFail = null;
function OAuthFlowApp({ provider, url, deviceCode, onCancel, onDone, onFail }) {
const [phase, setPhase] = useState(PHASE.WAITING);
const [result, setResult] = useState(null);
const [error, setError] = useState(null);
const [elapsed, setElapsed] = useState(0);
const [confirmCancel, setConfirmCancel] = useState(false);
useEffect(() => {
const id = setInterval(() => setElapsed((e) => e + 1), 1000);
return () => clearInterval(id);
}, []);
useEffect(() => {
_globalDone = (res) => {
setResult(res);
setPhase(PHASE.DONE);
onDone?.(res);
};
_globalFail = (err) => {
setError(typeof err === "string" ? err : (err?.message ?? String(err)));
setPhase(PHASE.FAILED);
onFail?.(err);
};
setPhase(PHASE.POLLING);
return () => {
_globalDone = null;
_globalFail = null;
};
}, []);
useInput((input, key) => {
if (phase === PHASE.DONE || phase === PHASE.FAILED || phase === PHASE.CANCELLED) return;
if (input === "q" || (key.ctrl && input === "c")) {
setConfirmCancel(true);
}
});
function handleCancelConfirm(yes) {
setConfirmCancel(false);
if (yes) {
setPhase(PHASE.CANCELLED);
onCancel?.();
}
}
const elapsed_str = `${Math.floor(elapsed / 60)}:${String(elapsed % 60).padStart(2, "0")}`;
if (confirmCancel) {
return (
<Box flexDirection="column" padding={1}>
<ConfirmDialog
message="Cancel OAuth authorization?"
onConfirm={handleCancelConfirm}
defaultNo
/>
</Box>
);
}
return (
<Box flexDirection="column" borderStyle="round" borderColor="cyan" paddingX={2} paddingY={1}>
<Box marginBottom={1}>
<Text bold color="cyan">
OmniRoute OAuth — {provider}
</Text>
</Box>
{url && (
<Box flexDirection="column" marginBottom={1}>
<Text>Open this URL in your browser to authorize:</Text>
<Box marginTop={0}>
<Text bold color="yellow">
{url}
</Text>
</Box>
</Box>
)}
{deviceCode && (
<Box flexDirection="column" marginBottom={1}>
<Text>
Device code:{" "}
<Text bold color="yellow">
{deviceCode}
</Text>
</Text>
</Box>
)}
<Box marginTop={1}>
{phase === PHASE.POLLING || phase === PHASE.WAITING ? (
<Box>
<Text color="green">
<Spinner type="dots" />
</Text>
<Text> Waiting for authorization... </Text>
<Text dimColor>({elapsed_str})</Text>
</Box>
) : phase === PHASE.DONE ? (
<Box>
<StatusBadge status="ok" />
<Text> Authorized: {result?.email ?? result?.account ?? "connected"}</Text>
</Box>
) : phase === PHASE.FAILED ? (
<Box>
<StatusBadge status="error" />
<Text> Failed: {error}</Text>
</Box>
) : (
<Box>
<StatusBadge status="warn" />
<Text> Cancelled.</Text>
</Box>
)}
</Box>
{(phase === PHASE.POLLING || phase === PHASE.WAITING) && (
<Box marginTop={1}>
<Text dimColor>[q] cancel</Text>
</Box>
)}
</Box>
);
}
export async function startOAuthTui({ provider, url, deviceCode }) {
return new Promise((resolve, reject) => {
let resolved = false;
function onDone(result) {
if (resolved) return;
resolved = true;
unmount();
resolve({ status: "authorized", result });
}
function onFail(err) {
if (resolved) return;
resolved = true;
unmount();
resolve({ status: "failed", error: err });
}
function onCancel() {
if (resolved) return;
resolved = true;
unmount();
resolve({ status: "cancelled" });
}
const { unmount, waitUntilExit } = render(
<OAuthFlowApp
provider={provider}
url={url}
deviceCode={deviceCode}
onDone={onDone}
onFail={onFail}
onCancel={onCancel}
/>
);
waitUntilExit()
.then(() => {
if (!resolved) resolve({ status: "exited" });
})
.catch(reject);
});
}
export function markOAuthDone(result) {
_globalDone?.(result);
}
export function markOAuthFailed(error) {
_globalFail?.(error);
}
|