Files
han-app/codebase/backend/frontend-test-site/app/dialogs/index.tsx
T

63 lines
2.3 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { useRouter } from "expo-router";
import React, { useEffect, useRef } from "react";
import { Text, View } from "react-native";
import { useApp } from "../../src/app-context";
import { AppHeader } from "../../src/components/AppHeader";
import { ChatScreenHeader } from "../../src/components/ChatScreenHeader";
import { GuestAuthGate } from "../../src/components/GuestAuthGate";
import { ScreenShell } from "../../src/components/ScreenShell";
import {
bindPendingFileIntent,
bindPendingTextIntent,
loadPendingTextIntent,
pendingDialogKey,
} from "../../src/pending-intent";
import { dialogApi } from "../../src/services";
import { ErrorNotice, Loading, styles } from "../../src/ui";
import { spacing } from "../../src/theme";
export default function DialogsScreen() {
const app = useApp();
const router = useRouter();
const requestKey = useRef(pendingDialogKey() ?? crypto.randomUUID());
const chat = useQuery({
queryKey: ["current-dialog", requestKey.current],
queryFn: () => dialogApi.create(requestKey.current),
enabled: app.authStatus === "authenticated",
});
useEffect(() => {
if (chat.data) {
const intent = loadPendingTextIntent();
if (intent && !intent.dialogId) bindPendingTextIntent(intent, chat.data.dialog_id);
bindPendingFileIntent(chat.data.dialog_id);
router.replace(`/dialogs/${chat.data.dialog_id}`);
}
}, [chat.data, router]);
if (app.authStatus !== "authenticated") {
return (
<ScreenShell>
<ChatScreenHeader title="Чат" subtitle="Требуется вход" />
<GuestAuthGate
icon="message-circle"
title="Чат доступен после входа"
description="Авторизуйтесь, чтобы переписываться с оператором и получать ответы."
/>
</ScreenShell>
);
}
return (
<ScreenShell>
<AppHeader />
<View style={{ flex: 1, padding: spacing.lg, justifyContent: "center" }}>
<Text accessibilityRole="header" style={styles.title}>Открываем чат…</Text>
{chat.isLoading && <Loading />}
{chat.error && <ErrorNotice error={chat.error} retry={() => void chat.refetch()} />}
</View>
</ScreenShell>
);
}