UNPKG

@etsoo/smarterp-core

Version:
75 lines (73 loc) 3.99 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import React from "react"; import { useNavigate } from "react-router-dom"; import { ButtonPopover } from "@etsoo/materialui"; import { useRequiredAppContext } from "../../ICoreServiceApp"; import { IdentityType } from "@etsoo/appscript"; import Typography from "@mui/material/Typography"; import Stack from "@mui/material/Stack"; import Button from "@mui/material/Button"; export function AppSwitchPopover(props) { // Destruct const { appName } = props; // Route const navigate = useNavigate(); // App const app = useRequiredAppContext(); // Labels const labels = app.getLabels("more", "switchApp"); // Max items to read const maxItems = 10; // Current app const currentApp = app.settings.appId; // Layout return (_jsx(ButtonPopover, { button: (callback) => (_jsx(Typography, { variant: "h6", sx: { color: (theme) => theme.palette.primary.main, fontWeight: "700", ml: 0.5, whiteSpace: "nowrap", cursor: "pointer" }, title: labels.switchApp, onClick: (e) => callback(e.currentTarget), children: appName })), loadData: () => app.core.appApi.getMy({ maxItems, identityType: IdentityType.User }, { showLoading: false }), position: "left", children: (data) => { if (data == null) return _jsx(React.Fragment, {}); // Remove the current app const index = data.findIndex((a) => a.id === currentApp); if (index >= 0) data.splice(index, 1); return (_jsxs(Stack, { direction: "column", margin: 2, children: [data.map((appData) => (_jsx(Button, { onClick: async () => { /* // Method 1, get the login URL and redirect const tasks = appData.urls.map((u) => app.core.authApi.getLogInUrl( "APP", { showLoading: false, onError: () => false }, u.api ) ); const result = await Promise.allSettled(tasks); const success = result.find( (r) => r.status === "fulfilled" && r.value != null ) as PromiseFulfilledResult<string> | undefined; if (success) { app.clearSession(); app.loadUrlEx(success.value); } else { app.notifier.alert(app.get("networkFailure")); } */ // Method 2, get the RequestAuth, sign in and redirect const tasks = appData.urls.map((u) => app.core.authApi.getAuthRequest("APP", { showLoading: false, onError: () => false }, u.api)); const result = await Promise.allSettled(tasks); const success = result.find((r) => r.status === "fulfilled" && r.value != null); if (success) { const url = await app.core.authApi.authRequest(success.value); if (url) { app.clearSession(); app.loadUrlEx(url); return; } } app.notifier.alert(app.get("networkFailure")); }, children: app.core.getAppName(appData) }, appData.id))), (data.length === 0 || data.length === maxItems) && (_jsxs(Button, { onClick: () => navigate("./app/my"), children: [labels.more, "..."] }))] })); } })); }