Files
bedolaga-cabinet/src/store/blocking.ts
c0mrade ac8a0fc41a feat(cabinet): recoverable "service unavailable" screen when the backend is unreachable
When the backend was down the cabinet got stuck on a blank loader: the bootstrap
token refresh used bare axios with no timeout (hung forever), and every
interceptor guard required an HTTP response — so a transport-level failure had
zero handling and no error UI ever appeared.

Add a full-screen ServiceUnavailableScreen (extends the existing blocking-store
pattern) shown whenever the backend is unreachable, that auto-recovers when it
returns:
- New 'backend_unavailable' blocking type + BlockingOverlay branch; mirrors
  MaintenanceScreen, CloudWarningIcon, i18n in ru/en/zh/fa.
- api/health.ts: pingBackend() probes the root /health/unified (bypasses the
  /api baseURL + interceptor); URL derived from the origin for remote/sub-path
  deploys; 502/503/504 count as down. reportPossibleBackendDown() confirms an
  outage with a liveness probe before flipping, so a one-off blip never blanks a
  loaded app.
- Detected from the response interceptor (no-response) and the bootstrap refresh
  path; doRefresh gets a timeout so it can no longer hang.
- A transport failure during refresh now PRESERVES the session (distinguished
  from a rejected token via lastFailureWasTransport) instead of logging the user
  out, so recovery actually resumes.
- Recovery lifts the overlay + refetches for an already-loaded session, and only
  hard-reloads when the initial bootstrap never reached the backend — no lost
  form state. Manual retry + 5s auto-poll. Dev proxy for /health.
2026-06-04 12:44:27 +03:00

125 lines
3.2 KiB
TypeScript

import { create } from 'zustand';
export type BlockingType =
| 'maintenance'
| 'channel_subscription'
| 'blacklisted'
| 'account_deleted'
| 'backend_unavailable'
| null;
interface MaintenanceInfo {
message: string;
reason?: string;
}
/**
* User-facing channel subscription status returned by the blocking 403 response.
* Intentionally separate from `RequiredChannel` (api/adminChannels.ts) which
* represents the admin CRUD entity with `is_active` / `sort_order` fields.
*/
interface ChannelInfo {
channel_id: string;
channel_link?: string;
title?: string;
is_subscribed: boolean;
}
interface ChannelSubscriptionInfo {
message: string;
channel_link?: string;
channels?: ChannelInfo[];
}
interface BlacklistedInfo {
message: string;
}
interface AccountDeletedInfo {
/** Backend-provided localized message. We may override with i18n key on render. */
message: string;
/** Bot username (without @) for building the Telegram deep link client-side as fallback. */
bot_username?: string;
/** Full Telegram deep-link URL (`https://t.me/<bot>?start=revive`). Empty when bot is unconfigured. */
telegram_deep_link?: string;
}
interface BlockingState {
blockingType: BlockingType;
maintenanceInfo: MaintenanceInfo | null;
channelInfo: ChannelSubscriptionInfo | null;
blacklistedInfo: BlacklistedInfo | null;
accountDeletedInfo: AccountDeletedInfo | null;
setMaintenance: (info: MaintenanceInfo) => void;
setChannelSubscription: (info: ChannelSubscriptionInfo) => void;
setBlacklisted: (info: BlacklistedInfo) => void;
setAccountDeleted: (info: AccountDeletedInfo) => void;
/** Backend is unreachable (transport-level failure). Renders the full-screen
* ServiceUnavailableScreen. No info payload — the screen shows static copy. */
setBackendUnavailable: () => void;
clearBlocking: () => void;
}
export const useBlockingStore = create<BlockingState>((set) => ({
blockingType: null,
maintenanceInfo: null,
channelInfo: null,
blacklistedInfo: null,
accountDeletedInfo: null,
setMaintenance: (info) =>
set({
blockingType: 'maintenance',
maintenanceInfo: info,
channelInfo: null,
blacklistedInfo: null,
accountDeletedInfo: null,
}),
setChannelSubscription: (info) =>
set({
blockingType: 'channel_subscription',
channelInfo: info,
maintenanceInfo: null,
blacklistedInfo: null,
accountDeletedInfo: null,
}),
setBlacklisted: (info) =>
set({
blockingType: 'blacklisted',
blacklistedInfo: info,
maintenanceInfo: null,
channelInfo: null,
accountDeletedInfo: null,
}),
setAccountDeleted: (info) =>
set({
blockingType: 'account_deleted',
accountDeletedInfo: info,
maintenanceInfo: null,
channelInfo: null,
blacklistedInfo: null,
}),
setBackendUnavailable: () =>
set({
blockingType: 'backend_unavailable',
maintenanceInfo: null,
channelInfo: null,
blacklistedInfo: null,
accountDeletedInfo: null,
}),
clearBlocking: () =>
set({
blockingType: null,
maintenanceInfo: null,
channelInfo: null,
blacklistedInfo: null,
accountDeletedInfo: null,
}),
}));