function useHashRoute(defaultKey = "login") {
const get = () => (location.hash.startsWith('#/') ? location.hash.slice(2) : defaultKey);
const [route, setRoute] = React.useState(get);
React.useEffect(() => {
const onHash = () => setRoute(get());
window.addEventListener('hashchange', onHash);
return () => window.removeEventListener('hashchange', onHash);
}, []);
const go = (key) => { location.hash = `#/${key}`; };
return [route, go];
}
const PUBLIC_ROUTES = new Set(["login", "termos", "suporte"]);
const isProtected = (r) => !PUBLIC_ROUTES.has(r);
function setChromeHidden(hidden){ document.body.classList.toggle('hide-chrome', hidden); }
function Spinner() {
return (
);
}
function Dash() {
return (
Dashboard
Contatos—
Envios—
Status—
);
}
function Config() {
return (
Configurações
Ajustes por usuário em user_settings. UI em breve.
);
}
function App() {
const { AppBootstrap, Box } = window.__CRM__;
const [route, go] = useHashRoute("login");
const [auth, setAuth] = React.useState({ loading: true, ok: false, user: null });
const [error, setError] = React.useState("");
React.useEffect(() => {
let alive = true;
(async () => {
try {
const res = await fetch("/auth/ping.php", { credentials: "include", cache: "no-store" });
if (!alive) return;
if (res.ok) {
const j = await res.json();
setAuth({ loading: false, ok: j.ok === true, user: j.user || null });
if (j.ok && route === "login") go("dashboard");
} else {
setAuth({ loading: false, ok: false, user: null });
if (isProtected(route)) go("login");
}
} catch {
if (!alive) return;
setAuth({ loading: false, ok: false, user: null });
setError("Falha ao verificar sessão.");
if (isProtected(route)) go("login");
}
})();
return () => { alive = false; };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
React.useEffect(() => {
const hidden = (route === "login") || (!auth.ok);
setChromeHidden(hidden);
}, [route, auth]);
const pages = {
login: window.LoginPage ? : ,
dashboard: ,
contacts: window.ContactsPage ? : ,
lists: window.ListsPage ? : ,
campaigns: window.CampaignsPage ? : , // <- aqui
whatsapp: WhatsAppWireframe.,
email: EmailWireframe.,
config: window.SettingsChatwootPage ? : ,
termos: Termos,
suporte: Suporte,
};
const view = pages[route] || ;
// dynamic route: lists/{id}
if (route.startsWith('lists/')) {
const id = route.split('/')[1];
if (window.ListEditPage) {
return (
);
}
return ;
}
if (route.startsWith('campaigns/')) {
const parts = route.split('/'); // ['campaigns', ':id', 'analytics'?]
const id = parts[1];
const sub = parts[2] || '';
if (sub === 'analytics' && window.AnalyticsPage) {
return (
{auth.loading ? : (
)}
);
}
}
return (
{auth.loading ? : (
{error && {error}}
{view}
)}
);
}
(() => {
const el = document.getElementById("crm-app");
ReactDOM.createRoot(el).render();
})();