Skip to content

Commit

Permalink
feat: ✨ added feature toggle support (#99)
Browse files Browse the repository at this point in the history
* feat: ✨ added feature toggle support

* fix: 🐛 fixed issue with client component use

* fix: 🐛 added missing env to workflow

* fix: 🐛 fixed failing test workflow
  • Loading branch information
WasiqB authored Nov 30, 2024
1 parent 8c8e20e commit 555a05d
Show file tree
Hide file tree
Showing 17 changed files with 403 additions and 263 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
build:
runs-on: ubuntu-latest
needs: lint
environment: Preview
env:
NEXT_PUBLIC_FLAGSMITH_ENVIRONMENT_ID: ${{ secrets.NEXT_PUBLIC_FLAGSMITH_ENVIRONMENT_ID }}

steps:
- name: Check out Git repository
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Ultra-Reporter leverages cutting-edge technologies to provide fast, efficient, a
- **[Turborepo](https://turborepo.org/)**: Fast, efficient monorepo management.
- **[ESLint](https://eslint.org/) & [Prettier](https://prettier.io/)**: Enforce consistent code style and automate code formatting.
- **[Prettier](https://prettier.io/)**: Code formatter.
- **[Flagsmith](https://www.flagsmith.com/)**: Feature flag management.
- **[Vercel](https://vercel.com/)**: Next.js is deployed on Vercel, a platform for static websites and serverless functions.

## ⏱️ What's Next?
Expand Down
1 change: 1 addition & 0 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_FLAGSMITH_ENVIRONMENT_ID=
42 changes: 24 additions & 18 deletions apps/web/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GoogleAnalytics } from '@next/third-parties/google';
import { Provider as AnalyticsProvider } from '@ultra-reporter/analytics/client';
import { getFeatureState } from '@ultra-reporter/feature-toggle/client';
import { FeatureProvider } from '@ultra-reporter/feature-toggle/provider';
import '@ultra-reporter/ui/global.css';
import { Footer } from '@ultra-reporter/ui/home/footer';
import { ScrollToTop } from '@ultra-reporter/ui/home/scroll-to-top';
Expand Down Expand Up @@ -63,33 +65,37 @@ export const metadata: Metadata = {
},
};

const RootLayout = ({
const RootLayout = async ({
children,
}: Readonly<{
children: React.ReactNode;
}>): DetailedHTMLProps<
HtmlHTMLAttributes<HTMLHtmlElement>,
HTMLHtmlElement
}>): Promise<
DetailedHTMLProps<HtmlHTMLAttributes<HTMLHtmlElement>, HTMLHtmlElement>
> => {
const featureState = await getFeatureState();
return (
<html lang='en' suppressHydrationWarning>
<head>
<link rel='icon' href='/favicon.png' sizes='any' type='image/png' />
</head>
<body className={'antialiased'}>
<ThemeProvider
attribute='class'
defaultTheme='light'
enableSystem
disableTransitionOnChange
>
{children}
<ScrollToTop />
<Footer />
<AnalyticsProvider />
</ThemeProvider>
</body>
{isProd && <GoogleAnalytics gaId='G-CNW9F6PH7P' />}
<FeatureProvider serverState={featureState}>
<>
<body className={'antialiased'}>
<ThemeProvider
attribute='class'
defaultTheme='light'
enableSystem
disableTransitionOnChange
>
{children}
<ScrollToTop />
<Footer />
<AnalyticsProvider />
</ThemeProvider>
</body>
{isProd && <GoogleAnalytics gaId='G-CNW9F6PH7P' />}
</>
</FeatureProvider>
</html>
);
};
Expand Down
3 changes: 2 additions & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"@next/third-parties": "^15.0.3",
"@tanstack/react-table": "^8.20.5",
"@ultra-reporter/analytics": "workspace:*",
"@ultra-reporter/feature-toggle": "workspace:*",
"@ultra-reporter/ui": "workspace:*",
"@ultra-reporter/utils": "workspace:*",
"lucide-react": "^0.461.0",
"lucide-react": "^0.462.0",
"next": "15.0.3"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
"@release-it-plugins/lerna-changelog": "^7.0.0",
"@stylistic/eslint-plugin-js": "^2.11.0",
"@stylistic/eslint-plugin-ts": "^2.11.0",
"@types/node": "^22.10.0",
"@types/node": "^22.10.1",
"@typescript-eslint/eslint-plugin": "^8.16.0",
"@typescript-eslint/parser": "^8.16.0",
"@vercel/style-guide": "^6.0.0",
"eslint": "^9.15.0",
"eslint": "^9.16.0",
"eslint-config-next": "15.0.3",
"eslint-config-prettier": "^9.1.0",
"eslint-config-turbo": "2.3.2",
"eslint-config-turbo": "2.3.3",
"eslint-plugin-only-warn": "^1.1.0",
"eslint-plugin-prettier": "^5.2.1",
"globals": "^15.12.0",
Expand All @@ -48,7 +48,7 @@
"prettier-plugin-tailwindcss": "^0.6.9",
"release-it": "^17.10.0",
"release-it-pnpm": "^4.6.3",
"turbo": "^2.3.2",
"turbo": "^2.3.3",
"typescript": "^5.7.2",
"typescript-eslint": "^8.16.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@openpanel/nextjs": "^1.0.6",
"@ultra-reporter/utils": "workspace:*",
"@vercel/functions": "^1.5.0"
"@vercel/functions": "^1.5.1"
},
"devDependencies": {
"@types/react": "^18.3.12",
Expand Down
25 changes: 25 additions & 0 deletions packages/feature-toggle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@ultra-reporter/feature-toggle",
"description": "Ultra Reporter feature toggles",
"version": "0.5.0",
"private": true,
"scripts": {
"lint": "eslint . --max-warnings 0"
},
"exports": {
"./client": "./src/client.ts",
"./provider": "./src/provider.tsx"
},
"devDependencies": {
"@types/react": "^18.3.12",
"@types/react-dom": "^18",
"@ultra-reporter/typescript-config": "workspace:*"
},
"peerDependencies": {
"react": "^18.0",
"react-dom": "^18.0"
},
"dependencies": {
"flagsmith": "^7.0.2"
}
}
14 changes: 14 additions & 0 deletions packages/feature-toggle/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createFlagsmithInstance } from 'flagsmith/isomorphic';
import { IState } from 'flagsmith/types';

export const getFeatureState = async (): Promise<IState<string>> => {
const flagsmith = createFlagsmithInstance();
await flagsmith.init({
evaluationContext: {
environment: {
apiKey: process.env.NEXT_PUBLIC_FLAGSMITH_ENVIRONMENT_ID,
},
},
});
return flagsmith.getState();
};
1 change: 1 addition & 0 deletions packages/feature-toggle/src/flag-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const Flags = ['sign_in_support', 'faq'];
32 changes: 32 additions & 0 deletions packages/feature-toggle/src/provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable react/react-in-jsx-scope */
'use client';

import { createFlagsmithInstance } from 'flagsmith/isomorphic';
import { FlagsmithProvider, useFlags } from 'flagsmith/react';
import { IFlagsmithFeature, IFlagsmithTrait, IState } from 'flagsmith/types';
import { useRef } from 'react';
import { Flags } from './flag-list';

interface FeatureProviderProps {
serverState: IState<string>;
children: JSX.Element;
}

export const FeatureProvider = ({
serverState,
children,
}: FeatureProviderProps): JSX.Element => {
const flagsmith = useRef(createFlagsmithInstance());
return (
<FlagsmithProvider flagsmith={flagsmith.current} serverState={serverState}>
{children}
</FlagsmithProvider>
);
};

export function getFlag(
flagId: string
): (IFlagsmithFeature & IFlagsmithTrait) | undefined {
const flags = useFlags(Flags);
return flags[flagId];
}
11 changes: 11 additions & 0 deletions packages/feature-toggle/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@ultra-reporter/typescript-config/react-library.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"],
"exclude": ["node_modules"]
}
3 changes: 2 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"class-variance-authority": "^0.7.1",
"cmdk": "1.0.4",
"embla-carousel-react": "^8.5.1",
"lucide-react": "^0.461.0",
"lucide-react": "^0.462.0",
"next": "15.0.3",
"next-themes": "^0.4.3",
"react-code-blocks": "^0.1.6",
Expand All @@ -59,6 +59,7 @@
"devDependencies": {
"@types/react": "^18.3.12",
"@types/react-dom": "^18",
"@ultra-reporter/feature-toggle": "workspace:*",
"@ultra-reporter/typescript-config": "workspace:*",
"@ultra-reporter/utils": "workspace:*",
"autoprefixer": "^10.4.20",
Expand Down
41 changes: 16 additions & 25 deletions packages/ui/src/home/faq.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
'use client';

import { getFlag } from '@ultra-reporter/feature-toggle/provider';
import { Description } from '../common/description';
import { Title } from '../common/title';
import {
Expand All @@ -7,25 +10,11 @@ import {
AccordionTrigger,
} from '../components/accordion';

const faqItems = [
{
question: 'What file formats does Ultra Reporter support?',
answer:
"Ultra Reporter currently supports only TestNG XML file formats for test results. We're working on expanding support for other formats in the future.",
},
{
question: 'How long does it take to generate a report?',
answer:
'Report generation is nearly instantaneous. Most reports are generated within seconds, depending on the size and complexity of your test results.',
},
{
question: 'Do you save the results which gets uploaded?',
answer:
'Not yet. Currently we do not save your results file which you upload to the app. It gets parsed and generates the report on runtime. We are working on adding support to save your records to a secure database in near future.',
},
];

export const FAQ = (): JSX.Element => {
export const FAQ = (): JSX.Element | null => {
const faq = getFlag('faq');
if (faq && !faq.enabled) {
return null;
}
return (
<section
id='faq'
Expand All @@ -37,12 +26,14 @@ export const FAQ = (): JSX.Element => {
</div>
<div className='mx-auto w-full max-w-[700px]'>
<Accordion type='single' collapsible className='w-full'>
{faqItems.map((item, index) => (
<AccordionItem key={index} value={`item-${index}`}>
<AccordionTrigger>{item.question}</AccordionTrigger>
<AccordionContent>{item.answer}</AccordionContent>
</AccordionItem>
))}
{JSON.parse(faq?.value).map(
(item: { question: string; answer: string }, index: number) => (
<AccordionItem key={index} value={`item-${index}`}>
<AccordionTrigger>{item.question}</AccordionTrigger>
<AccordionContent>{item.answer}</AccordionContent>
</AccordionItem>
)
)}
</Accordion>
</div>
</section>
Expand Down
12 changes: 9 additions & 3 deletions packages/ui/src/home/hero.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use client';

import { getFlag } from '@ultra-reporter/feature-toggle/provider';
import Image from 'next/image';
import { FileUpload } from '../utils/file-upload';

export const Hero = (): JSX.Element => {
const signInSupport = getFlag('sign_in_support');
return (
<section className='flex flex-col items-center gap-4 p-16'>
<div className='flex flex-col-reverse items-center justify-between gap-8 md:flex-row md:gap-12'>
Expand All @@ -13,9 +17,11 @@ export const Hero = (): JSX.Element => {
<br />
in one click
</h1>
<div className='w-full max-w-sm pt-8'>
<FileUpload />
</div>
{!signInSupport?.enabled && (
<div className='w-full max-w-sm pt-8'>
<FileUpload />
</div>
)}
</div>
<div className='w-full max-w-lg items-end'>
<Image
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@ultra-reporter/typescript-config/react-library.json",
"extends": "@ultra-reporter/typescript-config/base.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
"exclude": ["node_modules"]
}
Loading

0 comments on commit 555a05d

Please sign in to comment.