add oauth app connected
This commit is contained in:
@@ -7,13 +7,16 @@ import { useToast } from '@/components/id/toast-provider';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { createManagedBot, getApiErrorMessage } from '@/lib/api';
|
||||
import { useEmbeddedAuthFallback } from '@/lib/embedded-auth-bridge';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
type Step = 'name' | 'username' | 'done';
|
||||
|
||||
export function BotCreateMiniAppContent() {
|
||||
const { token, user, isPinLocked } = useAuth();
|
||||
const { token, isPinLocked, isLoading } = useAuth();
|
||||
const embeddedToken = useEmbeddedAuthFallback();
|
||||
const { showToast } = useToast();
|
||||
const effectiveToken = token ?? embeddedToken;
|
||||
|
||||
const [step, setStep] = useState<Step>('name');
|
||||
const [name, setName] = useState('');
|
||||
@@ -21,11 +24,11 @@ export function BotCreateMiniAppContent() {
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [createdBot, setCreatedBot] = useState<{ username: string; token: string; botId: string } | null>(null);
|
||||
|
||||
const canUse = Boolean(token && user && !isPinLocked);
|
||||
const canUse = Boolean(effectiveToken && !isPinLocked);
|
||||
const usernamePreview = useMemo(() => `${username.replace(/_bot$/i, '').trim()}_bot`, [username]);
|
||||
|
||||
async function handleCreate() {
|
||||
if (!canUse) return;
|
||||
if (!canUse || !effectiveToken) return;
|
||||
const trimmedName = name.trim();
|
||||
const trimmedUsername = username.replace(/_bot$/i, '').trim();
|
||||
if (!trimmedName) {
|
||||
@@ -40,7 +43,7 @@ export function BotCreateMiniAppContent() {
|
||||
|
||||
setCreating(true);
|
||||
try {
|
||||
const response = await createManagedBot({ name: trimmedName, username: trimmedUsername }, token);
|
||||
const response = await createManagedBot({ name: trimmedName, username: trimmedUsername }, effectiveToken);
|
||||
setCreatedBot({
|
||||
username: response.bot?.username ?? usernamePreview,
|
||||
token: response.token ?? '',
|
||||
@@ -61,6 +64,15 @@ export function BotCreateMiniAppContent() {
|
||||
showToast('Токен скопирован');
|
||||
}
|
||||
|
||||
if (isLoading && !effectiveToken) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center p-6 text-center text-sm text-[#667085]">
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
Загрузка...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!canUse) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center p-6 text-center text-sm text-[#667085]">
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
updateManagedBot,
|
||||
updateManagedBotProfile
|
||||
} from '@/lib/api';
|
||||
import { useEmbeddedAuthFallback } from '@/lib/embedded-auth-bridge';
|
||||
import { parseComposerMenuButtonJson } from '@/lib/bot-menu-button';
|
||||
|
||||
function FieldTextarea({
|
||||
@@ -77,7 +78,9 @@ function SectionCard({
|
||||
export function BotManageMiniAppContent() {
|
||||
const searchParams = useSearchParams();
|
||||
const botId = searchParams.get('botId') ?? '';
|
||||
const { token, user, isPinLocked } = useAuth();
|
||||
const { token, isPinLocked, isLoading } = useAuth();
|
||||
const embeddedToken = useEmbeddedAuthFallback();
|
||||
const effectiveToken = token ?? embeddedToken;
|
||||
const { showToast } = useToast();
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
@@ -94,13 +97,13 @@ export function BotManageMiniAppContent() {
|
||||
const [tokenPrefix, setTokenPrefix] = useState('');
|
||||
const [newToken, setNewToken] = useState<string | null>(null);
|
||||
|
||||
const canUse = Boolean(token && user && !isPinLocked && botId);
|
||||
const canUse = Boolean(effectiveToken && !isPinLocked && botId);
|
||||
|
||||
const loadBot = useCallback(async () => {
|
||||
if (!canUse) return;
|
||||
if (!canUse || !effectiveToken) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const bot = await fetchBot(botId, token);
|
||||
const bot = await fetchBot(botId, effectiveToken);
|
||||
setName(bot.name);
|
||||
setUsername(bot.username);
|
||||
setDescription(bot.description ?? '');
|
||||
@@ -121,7 +124,7 @@ export function BotManageMiniAppContent() {
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [botId, canUse, showToast, token]);
|
||||
}, [botId, canUse, effectiveToken, showToast]);
|
||||
|
||||
useEffect(() => {
|
||||
void loadBot();
|
||||
@@ -130,7 +133,7 @@ export function BotManageMiniAppContent() {
|
||||
const usernameWithoutSuffix = useMemo(() => username.replace(/_bot$/i, ''), [username]);
|
||||
|
||||
async function handleSaveBasic() {
|
||||
if (!canUse) return;
|
||||
if (!canUse || !effectiveToken) return;
|
||||
setSavingBasic(true);
|
||||
try {
|
||||
const bot = await updateManagedBot(
|
||||
@@ -139,7 +142,7 @@ export function BotManageMiniAppContent() {
|
||||
name: name.trim(),
|
||||
username: usernameWithoutSuffix.trim()
|
||||
},
|
||||
token
|
||||
effectiveToken
|
||||
);
|
||||
setName(bot.name);
|
||||
setUsername(bot.username);
|
||||
@@ -152,7 +155,7 @@ export function BotManageMiniAppContent() {
|
||||
}
|
||||
|
||||
async function handleSaveProfile() {
|
||||
if (!canUse) return;
|
||||
if (!canUse || !effectiveToken) return;
|
||||
setSavingProfile(true);
|
||||
try {
|
||||
const bot = await updateManagedBotProfile(
|
||||
@@ -164,7 +167,7 @@ export function BotManageMiniAppContent() {
|
||||
menuButtonUrl: menuButtonUrl.trim(),
|
||||
menuButtonText: menuButtonText.trim() || 'App'
|
||||
},
|
||||
token
|
||||
effectiveToken
|
||||
);
|
||||
setDescription(bot.description ?? '');
|
||||
setAboutText(bot.aboutText ?? '');
|
||||
@@ -183,10 +186,10 @@ export function BotManageMiniAppContent() {
|
||||
}
|
||||
|
||||
async function handleRevokeToken() {
|
||||
if (!canUse) return;
|
||||
if (!canUse || !effectiveToken) return;
|
||||
setRevoking(true);
|
||||
try {
|
||||
const response = await revokeManagedBotToken(botId, token);
|
||||
const response = await revokeManagedBotToken(botId, effectiveToken);
|
||||
setNewToken(response.token ?? null);
|
||||
setTokenPrefix(response.bot?.tokenPrefix ?? tokenPrefix);
|
||||
showToast('Токен перевыпущен');
|
||||
@@ -207,6 +210,15 @@ export function BotManageMiniAppContent() {
|
||||
return <div className="flex min-h-screen items-center justify-center p-6 text-sm text-[#667085]">Не указан botId</div>;
|
||||
}
|
||||
|
||||
if (isLoading && !effectiveToken) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center p-6 text-center text-sm text-[#667085]">
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
Загрузка...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!canUse) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center p-6 text-center text-sm text-[#667085]">
|
||||
|
||||
Reference in New Issue
Block a user