|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { |
| 3 | + Box, |
| 4 | + Tabs, |
| 5 | + Tab, |
| 6 | + ThemeProvider, |
| 7 | + CssBaseline, |
| 8 | + createTheme, |
| 9 | + AppBar, |
| 10 | + Toolbar, |
| 11 | + Typography, |
| 12 | + Container, |
| 13 | +} from '@material-ui/core'; |
| 14 | +import { EntityProvider } from '@backstage/plugin-catalog-react'; |
| 15 | +import { Entity } from '@backstage/catalog-model'; |
| 16 | +import { TestApiProvider } from '@backstage/test-utils'; |
| 17 | +import { discoveryApiRef, fetchApiRef } from '@backstage/core-plugin-api'; |
| 18 | +import { DemoBanner } from './DemoBanner'; |
| 19 | +import { FlagsTab } from '../src/components/FlagsTab'; |
| 20 | +import { FlagsmithOverviewCard } from '../src/components/FlagsmithOverviewCard'; |
| 21 | +import { FlagsmithUsageCard } from '../src/components/FlagsmithUsageCard'; |
| 22 | + |
| 23 | +// Mock entity with Flagsmith annotations |
| 24 | +const mockEntity: Entity = { |
| 25 | + apiVersion: 'backstage.io/v1alpha1', |
| 26 | + kind: 'Component', |
| 27 | + metadata: { |
| 28 | + name: 'demo-service', |
| 29 | + description: 'A demo service with Flagsmith feature flags integration', |
| 30 | + annotations: { |
| 31 | + 'flagsmith.com/project-id': '31465', |
| 32 | + 'flagsmith.com/org-id': '24242', |
| 33 | + }, |
| 34 | + }, |
| 35 | + spec: { |
| 36 | + type: 'service', |
| 37 | + lifecycle: 'production', |
| 38 | + owner: 'guests', |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +// Mock Discovery API (returns the MSW-intercepted URL) |
| 43 | +const mockDiscoveryApi = { |
| 44 | + getBaseUrl: async (_pluginId: string) => { |
| 45 | + // Return a URL that MSW will intercept |
| 46 | + return `${window.location.origin}/api`; |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +// Mock Fetch API (uses native fetch) |
| 51 | +const mockFetchApi = { |
| 52 | + fetch: async (url: string, init?: RequestInit) => { |
| 53 | + return fetch(url, init); |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +// Light theme similar to Backstage |
| 58 | +const theme = createTheme({ |
| 59 | + palette: { |
| 60 | + type: 'light', |
| 61 | + primary: { |
| 62 | + main: '#0AC2A3', |
| 63 | + }, |
| 64 | + secondary: { |
| 65 | + main: '#7B51FB', |
| 66 | + }, |
| 67 | + background: { |
| 68 | + default: '#f5f5f5', |
| 69 | + paper: '#ffffff', |
| 70 | + }, |
| 71 | + }, |
| 72 | + typography: { |
| 73 | + fontFamily: 'Roboto, sans-serif', |
| 74 | + }, |
| 75 | +}); |
| 76 | + |
| 77 | +interface TabPanelProps { |
| 78 | + children?: React.ReactNode; |
| 79 | + index: number; |
| 80 | + value: number; |
| 81 | +} |
| 82 | + |
| 83 | +const TabPanel = ({ children, value, index }: TabPanelProps) => ( |
| 84 | + <div role="tabpanel" hidden={value !== index}> |
| 85 | + {value === index && <Box>{children}</Box>} |
| 86 | + </div> |
| 87 | +); |
| 88 | + |
| 89 | +export const App = () => { |
| 90 | + const [tabValue, setTabValue] = useState(0); |
| 91 | + |
| 92 | + return ( |
| 93 | + <ThemeProvider theme={theme}> |
| 94 | + <CssBaseline /> |
| 95 | + <TestApiProvider |
| 96 | + apis={[ |
| 97 | + [discoveryApiRef, mockDiscoveryApi], |
| 98 | + [fetchApiRef, mockFetchApi], |
| 99 | + ]} |
| 100 | + > |
| 101 | + <EntityProvider entity={mockEntity}> |
| 102 | + <Box sx={{ flexGrow: 1 }}> |
| 103 | + {/* Demo Banner */} |
| 104 | + <DemoBanner /> |
| 105 | + |
| 106 | + {/* App Header */} |
| 107 | + <AppBar position="static" style={{ backgroundColor: '#1F1F1F' }}> |
| 108 | + <Toolbar> |
| 109 | + <Typography variant="h6" style={{ flexGrow: 1 }}> |
| 110 | + Flagsmith Backstage Plugin Demo |
| 111 | + </Typography> |
| 112 | + </Toolbar> |
| 113 | + <Tabs |
| 114 | + value={tabValue} |
| 115 | + onChange={(_e, newValue) => setTabValue(newValue)} |
| 116 | + indicatorColor="primary" |
| 117 | + textColor="inherit" |
| 118 | + style={{ backgroundColor: 'rgba(255,255,255,0.1)' }} |
| 119 | + > |
| 120 | + <Tab label="Feature Flags" /> |
| 121 | + <Tab label="Overview Card" /> |
| 122 | + <Tab label="Usage Card" /> |
| 123 | + </Tabs> |
| 124 | + </AppBar> |
| 125 | + |
| 126 | + {/* Tab Content */} |
| 127 | + <Container maxWidth="lg" style={{ marginTop: 24 }}> |
| 128 | + <TabPanel value={tabValue} index={0}> |
| 129 | + <FlagsTab /> |
| 130 | + </TabPanel> |
| 131 | + |
| 132 | + <TabPanel value={tabValue} index={1}> |
| 133 | + <Box style={{ maxWidth: 600 }}> |
| 134 | + <FlagsmithOverviewCard /> |
| 135 | + </Box> |
| 136 | + </TabPanel> |
| 137 | + |
| 138 | + <TabPanel value={tabValue} index={2}> |
| 139 | + <Box style={{ maxWidth: 600 }}> |
| 140 | + <FlagsmithUsageCard /> |
| 141 | + </Box> |
| 142 | + </TabPanel> |
| 143 | + </Container> |
| 144 | + </Box> |
| 145 | + </EntityProvider> |
| 146 | + </TestApiProvider> |
| 147 | + </ThemeProvider> |
| 148 | + ); |
| 149 | +}; |
0 commit comments