## Summary This MR introduces a major refactor of the dashboard and react-openapi integration, focusing on configurability, separation of concerns, and improved extensibility. --- ## Key Changes ### 1. OpenAPI / Admin Refactor * Extracted `ConfigContext` into a dedicated provider. * Introduced `AppProvider` to encapsulate: * Config loading * API client initialization * React Query setup * Removed internal `QueryClientProvider` from `Admin` for better composability. * `Admin` now supports both: * Standalone usage * Nested usage inside an existing provider ### 2. Resource System Improvements * Added `hidden` flag to `ResourceConfig` and overrides. * Admin UI now filters out hidden resources. * Added `useResourceByName` helper for dynamic resource access. * Improved `useResource`: * Handles undefined config safely * Adds guards for missing endpoints * Disables queries when endpoint is absent ### 3. API Client Enhancements * Added custom `paramsSerializer`: * Serializes arrays without `[]` * Ensures backend-compatible query formats ### 4. Dashboard Architecture Overhaul * Replaced hardcoded dashboard with **config-driven system**: * Introduced `ConfigurableDashboard` * Dashboard sections defined via config * New state model: * `mode` (expense/income) * `periodType` (rolling/calendar) * `comparison` * `selectedPeriodId` ### 5. Component Refactor (View / Logic Split) * Split major components into: * `.tsx` (logic/controller) * `.view.tsx` (presentation) * `.models.ts` (types) * Applied to: * Dashboard * HistoryChart * ProgressCard * LatestItems ### 6. HistoryChart Redesign * Fully rebuilt using report-driven data * Supports: * Weekly / Monthly / Yearly / FY / Full views * Rolling vs Calendar periods * Comparison mode (auto-aligned offsets) * Introduced: * Bucket merging logic * Dynamic comparison attachment ### 7. Reporting Integration * Dashboard now powered by: * `useReport` * `prepareReport` * Removes need for multiple manual API calls ### 8. UI / UX Improvements * Theme-aware color system * Dynamic accent colors per mode * Cleaner layout using section-based rendering * Improved selection and interaction in charts ### 9. Cleanup & Removals * Removed legacy components: * Old `HistoryChart` * Old `ProgressCard` * Simplified Header layout spacing --- ## Behavior Changes * Hidden resources are no longer visible in Admin UI. * Dashboard is now entirely configuration-driven. * API query params for arrays no longer use `[]`. * Resource hooks no longer crash on missing config. --- ## Risks / Considerations * Dashboard depends on correct configuration structure. * Hidden flag may unintentionally hide resources if misconfigured. * Query param serialization change must align with backend expectations. --- ## Follow-ups * Add typing improvements to remove `@ts-ignore` in `GenericForm`. * Extend dashboard config with more reusable section presets. * Add tests for report aggregation and comparison logic. --- Reviewed-on: #1 Co-authored-by: Vishesh 'ironeagle' Bangotra <aetoskia@gmail.com> Co-committed-by: Vishesh 'ironeagle' Bangotra <aetoskia@gmail.com>
170 lines
4.9 KiB
TypeScript
170 lines
4.9 KiB
TypeScript
import * as React from "react";
|
|
import { useAuth, AuthPage } from "../react-auth";
|
|
import { UploadProvider } from "./providers/UploadProvider";
|
|
import AdminLayout from "./components/AdminLayout";
|
|
import ResourceView from "./components/ResourceView";
|
|
import { getAppConfig } from "./config";
|
|
import { initializeApiClients } from "./api/client";
|
|
import { AppConfig } from "./types/config";
|
|
import { Box, Typography, Paper, CircularProgress } from "@mui/material";
|
|
import {
|
|
Routes,
|
|
Route,
|
|
useNavigate,
|
|
useParams,
|
|
} from "react-router-dom";
|
|
|
|
import { ConfigContext } from "./providers/ConfigContext";
|
|
|
|
function Dashboard({ basePath }: { basePath: string }) {
|
|
const config = React.useContext(ConfigContext);
|
|
const navigate = useNavigate();
|
|
|
|
const resources = config?.resources || [];
|
|
const visibleResources = resources.filter((res) => !res.hidden);
|
|
|
|
return (
|
|
<Box>
|
|
<Typography variant="h4" gutterBottom>
|
|
Welcome to the Admin Panel
|
|
</Typography>
|
|
<Typography variant="body1" sx={{ color: 'text.secondary' }}>
|
|
Select a resource from the sidebar to manage data.
|
|
</Typography>
|
|
|
|
<Box
|
|
sx={{
|
|
display: "grid",
|
|
gridTemplateColumns: "repeat(auto-fill, minmax(240px, 1fr))",
|
|
gap: 3,
|
|
mt: 4,
|
|
}}
|
|
>
|
|
{visibleResources.map((res) => (
|
|
<Paper
|
|
key={res.name}
|
|
sx={{
|
|
p: 3,
|
|
textAlign: "center",
|
|
cursor: 'pointer',
|
|
transition: 'transform 0.2s',
|
|
'&:hover': { transform: 'translateY(-4px)', boxShadow: 4 }
|
|
}}
|
|
onClick={() => navigate(`/admin/${res.name}`)}
|
|
>
|
|
<Typography variant="h6" color="primary">{res.pluralLabel}</Typography>
|
|
<Typography variant="body2" color="text.secondary">Manage {res.pluralLabel.toLowerCase()}</Typography>
|
|
</Paper>
|
|
))}
|
|
</Box>
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
import ProfileView from "./components/ProfileView";
|
|
|
|
function AdminApp({ basePath }: { basePath: string }) {
|
|
const { currentUser, login, logout, loading, error } = useAuth();
|
|
const config = React.useContext(ConfigContext);
|
|
const navigate = useNavigate();
|
|
|
|
const resources = config?.resources || [];
|
|
const visibleResources = resources.filter((res) => !res.hidden);
|
|
|
|
if (!currentUser) {
|
|
return (
|
|
<AuthPage
|
|
mode="login"
|
|
login={login}
|
|
register={async () => {}} // Disable registration for Admin
|
|
loading={loading}
|
|
error={error}
|
|
onSwitchMode={() => {}}
|
|
onBack={() => {}}
|
|
currentUser={null}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<AdminLayout
|
|
username={currentUser.username}
|
|
onLogout={logout}
|
|
onSelectResource={(name) => navigate(`/admin/${name}`)}
|
|
resources={visibleResources}
|
|
>
|
|
<Routes>
|
|
<Route path="/" element={<Dashboard basePath={basePath} />} />
|
|
<Route path="/profile" element={<ProfileView />} />
|
|
<Route path="/:resourceName" element={<ResourceRouteWrapper />} />
|
|
<Route path="/:resourceName/:id" element={<ResourceRouteWrapper />} />
|
|
<Route path="/:resourceName/create" element={<ResourceRouteWrapper />} />
|
|
<Route path="/:resourceName/edit/:id" element={<ResourceRouteWrapper />} />
|
|
</Routes>
|
|
</AdminLayout>
|
|
);
|
|
}
|
|
|
|
function ResourceRouteWrapper() {
|
|
const { resourceName } = useParams();
|
|
const config = React.useContext(ConfigContext);
|
|
const selectedResource = config?.resources.find((r) => r.name === resourceName);
|
|
|
|
if (!selectedResource) return <Typography>Resource not found</Typography>;
|
|
|
|
return <ResourceView config={selectedResource} />;
|
|
}
|
|
|
|
interface AdminProps {
|
|
basePath?: string;
|
|
resourceOverrides?: Record<string, any>;
|
|
profileConfig?: any;
|
|
}
|
|
|
|
export default function Admin({ basePath = "/admin", resourceOverrides = {}, profileConfig = {} }: AdminProps) {
|
|
const existingConfig = React.useContext(ConfigContext);
|
|
const [config, setConfig] = React.useState<AppConfig | null>(existingConfig);
|
|
|
|
React.useEffect(() => {
|
|
if (!existingConfig) {
|
|
getAppConfig(resourceOverrides, profileConfig).then((cfg) => {
|
|
initializeApiClients(cfg.baseUrl, cfg.authBaseUrl);
|
|
setConfig(cfg);
|
|
});
|
|
}
|
|
}, [resourceOverrides, profileConfig, existingConfig]);
|
|
|
|
if (!config) {
|
|
return (
|
|
<Box
|
|
sx={{
|
|
display: "flex",
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
height: "100vh",
|
|
}}
|
|
>
|
|
<CircularProgress />
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
const content = (
|
|
<UploadProvider>
|
|
<AdminApp basePath={basePath} />
|
|
</UploadProvider>
|
|
);
|
|
|
|
// If we have an existing config, we are already inside a Provider and QueryClient
|
|
if (existingConfig) {
|
|
return content;
|
|
}
|
|
|
|
// Fallback for standalone usage
|
|
return (
|
|
<ConfigContext.Provider value={config}>
|
|
{content}
|
|
</ConfigContext.Provider>
|
|
);
|
|
}
|