Накатил человеческий дизайн
This commit is contained in:
@@ -1,22 +1,44 @@
|
||||
import { useLocalSearchParams } from "expo-router";
|
||||
import { useLocalSearchParams, useRouter } from "expo-router";
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import { Text, View } from "react-native";
|
||||
import { useApp } from "../../src/app-context";
|
||||
import { AuthLoadingView } from "../../src/components/AuthLoadingView";
|
||||
import { ScreenShell } from "../../src/components/ScreenShell";
|
||||
import { clearPendingTextIntent, loadPendingTextIntent } from "../../src/pending-intent";
|
||||
import { dialogApi } from "../../src/services";
|
||||
import { spacing } from "../../src/theme";
|
||||
import type { Consents } from "../../src/types";
|
||||
import { Button, ErrorNotice, Loading, styles } from "../../src/ui";
|
||||
import { Button, ErrorNotice, styles } from "../../src/ui";
|
||||
|
||||
export default function AuthCallbackScreen() {
|
||||
const params = useLocalSearchParams<{ code?: string; state?: string; error?: string }>();
|
||||
const app = useApp();
|
||||
const router = useRouter();
|
||||
const [error, setError] = useState<unknown>();
|
||||
const completionStarted = useRef(false);
|
||||
const retry = () => {
|
||||
|
||||
const complete = async () => {
|
||||
if (!params.code || !params.state || typeof window === "undefined") return;
|
||||
const raw = window.sessionStorage.getItem("han.pending-consents");
|
||||
if (!raw) return;
|
||||
if (!raw) throw new Error("Не найдены локально принятые согласия. Начните вход заново.");
|
||||
setError(undefined);
|
||||
void app.finishCallback(params.code, params.state, JSON.parse(raw) as Consents).catch(setError);
|
||||
const consents = JSON.parse(raw) as Consents;
|
||||
await app.finishCallback(params.code, params.state, consents);
|
||||
|
||||
const intent = loadPendingTextIntent();
|
||||
if (intent) {
|
||||
const dialog = await dialogApi.create(intent.dialogKey);
|
||||
await dialogApi.sendText(dialog.dialog_id, intent.text, intent.messageKey);
|
||||
clearPendingTextIntent();
|
||||
window.sessionStorage.removeItem("han.pending-consents");
|
||||
router.replace(`/dialogs/${dialog.dialog_id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
window.sessionStorage.removeItem("han.pending-consents");
|
||||
router.replace("/");
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (completionStarted.current) return;
|
||||
if (params.error) {
|
||||
@@ -26,19 +48,23 @@ export default function AuthCallbackScreen() {
|
||||
}
|
||||
if (!params.code || !params.state) return;
|
||||
completionStarted.current = true;
|
||||
const raw = typeof window !== "undefined" ? window.sessionStorage.getItem("han.pending-consents") : null;
|
||||
if (!raw) {
|
||||
setError(new Error("Не найдены локально принятые согласия. Начните вход заново."));
|
||||
return;
|
||||
}
|
||||
const consents = JSON.parse(raw) as Consents;
|
||||
void app.finishCallback(params.code, params.state, consents)
|
||||
.then(() => window.sessionStorage.removeItem("han.pending-consents"))
|
||||
.catch(setError);
|
||||
void complete().catch(setError);
|
||||
}, [params.code, params.state, params.error]);
|
||||
return <View style={styles.page}>
|
||||
<Text accessibilityRole="header" style={styles.title}>Завершение входа</Text>
|
||||
{!error && <Loading />}
|
||||
{error && <><ErrorNotice error={error} /><Button title="Повторить bootstrap" onPress={retry} /></>}
|
||||
</View>;
|
||||
|
||||
return (
|
||||
<ScreenShell>
|
||||
{!error ? (
|
||||
<AuthLoadingView />
|
||||
) : (
|
||||
<View style={{ flex: 1, justifyContent: "center", padding: spacing.lg, gap: spacing.lg }}>
|
||||
<Text accessibilityRole="header" style={styles.title}>Не удалось завершить вход</Text>
|
||||
<>
|
||||
<ErrorNotice error={error} />
|
||||
<Button title="Повторить" onPress={() => void complete().catch(setError)} />
|
||||
<Button title="На главную" secondary onPress={() => router.replace("/")} />
|
||||
</>
|
||||
</View>
|
||||
)}
|
||||
</ScreenShell>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user