Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ adding api for file processing #105

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions apps/web/app/(app)/loading/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,53 @@ import {
CardTitle,
} from '@ultra-reporter/ui/components/card';
import { Progress } from '@ultra-reporter/ui/components/progress';
import { getData } from '@ultra-reporter/ui/data';
import {
convertToJson,
getTestResults,
} from '@ultra-reporter/utils/xml-parser';
import { Bug, MoveLeft } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { useEffect, useState } from 'react';
import { JSX, useEffect, useState } from 'react';

const LoadingPage = (): JSX.Element => {
const [progress, setProgress] = useState(0);
const [error, setError] = useState<string | null>(null);
const router = useRouter();

useEffect(() => {
const xmlContent = localStorage.getItem('xml-data');
try {
setProgress(0);
if (!xmlContent) {
throw new Error('No XML data found in the file.');
}
setProgress(25);
const jsonData = convertToJson(xmlContent);
setProgress(50);
const testResult = getTestResults(jsonData);
setProgress(75);
localStorage.setItem('json-data', JSON.stringify(getData(testResult)));
setProgress(100);
router.push('/results');
} catch (err) {
if (err instanceof Error) {
setError(`${err.message}`);
if (process.env.VERCEL_ENV !== 'production') {
console.error(`Message: ${err.message}
Stack: ${err.stack}`);
const fetchData = async () => {
try {
setProgress(25);

const response = await fetch('/api/format-data', {
body: JSON.stringify({
value: localStorage.getItem('xml-data'),
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to get formatted data');
}

setProgress(75);
const data = await response.json();

if (!data) {
throw new Error('No data received from server');
}

localStorage.setItem('json-data', JSON.stringify(data));

setProgress(100);
router.push('/results');
} catch (err) {
console.error('Error fetching data:', err);
setError(err instanceof Error ? err.message : 'Failed to load data');
}
}
};

fetchData();
}, [router]);

const handleBack = (): void => {
Expand Down
1 change: 1 addition & 0 deletions apps/web/app/(app)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { HowItWorks } from '@ultra-reporter/ui/home/how-it-works';
import { NavBar } from '@ultra-reporter/ui/home/nav-bar';
import { OpenSource } from '@ultra-reporter/ui/home/open-source';
import { Sponsor } from '@ultra-reporter/ui/home/sponsor';
import { JSX } from 'react';

const LandingPage = (): JSX.Element => {
return (
Expand Down
37 changes: 29 additions & 8 deletions apps/web/app/(app)/results/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { columns } from '@ultra-reporter/ui/data-table/table/columns';
import { NavBar } from '@ultra-reporter/ui/home/nav-bar';
import { cn } from '@ultra-reporter/utils/cn';
import { FormattedData } from '@ultra-reporter/utils/types';
import { useEffect, useState } from 'react';
import { JSX, useEffect, useState } from 'react';

const chartConfig: ChartConfig = {
total: {
Expand Down Expand Up @@ -63,13 +63,34 @@ const ResultsPage = (): JSX.Element => {
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
const resultData = localStorage.getItem('json-data') as string;
if (resultData) {
const testResult: TestResultData[] = JSON.parse(resultData);
setResult(testResult);
setFormattedData(getFormattedData(testResult));
}
setIsLoading(false);
const loadData = async () => {
try {
const resultData = localStorage.getItem('json-data');
if (resultData) {
const testResult: TestResultData[] = JSON.parse(resultData);
setResult(testResult);
setFormattedData(getFormattedData(testResult));
setIsLoading(false);
return;
}

// If no localStorage data, try to get from cookies
const response = await fetch('/api/get-formatted-data');
if (response.ok) {
const data = await response.json();
setResult(data);
setFormattedData(getFormattedData(data));
// Save to localStorage for future use
localStorage.setItem('json-data', JSON.stringify(data));
}
} catch (error) {
console.error('Error loading data:', error);
} finally {
setIsLoading(false);
}
};

loadData();
}, []);

const {
Expand Down
28 changes: 28 additions & 0 deletions apps/web/app/api/format-data/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getData } from '@ultra-reporter/ui/data';
import { getTestResults } from '@ultra-reporter/utils/xml-parser';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(request: NextRequest) {
try {
const { value } = await request.json();

if (!value) {
return NextResponse.json(
{ error: 'Please upload a file first' },
{ status: 404 }
);
}

console.log(value);
const testResult = getTestResults(value);
const formattedData = getData(testResult);

return NextResponse.json(formattedData);
} catch (error) {
console.error('Error getting formatted data:', error);
return NextResponse.json(
{ error: 'Error retrieving formatted data' },
{ status: 500 }
);
}
}
44 changes: 44 additions & 0 deletions apps/web/app/api/process-file/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { convertToJson } from '@ultra-reporter/utils/xml-parser';
import { FileReader } from 'fs/promises';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(request: NextRequest) {
try {
const formData = await request.formData();
const file = formData.get('file') as File;

if (!file) {
return NextResponse.json({ error: 'No file provided' }, { status: 400 });
}

if (!file.name.endsWith('.xml')) {
return NextResponse.json(
{ error: 'Invalid file type. Only XML files are allowed.' },
{ status: 400 }
);
}

let xmlContent: string | null = null;
const reader = new FileReader();
reader.onload = async (e) => {
xmlContent = e.target?.result as string;
};
reader.readAsText(file);

if (!xmlContent) {
return NextResponse.json(
{ error: 'Empty file content' },
{ status: 400 }
);
}

const jsonData = convertToJson(xmlContent);
return NextResponse.json(jsonData);
} catch (error) {
console.error('Error processing file:', error);
return NextResponse.json(
{ error: 'Error processing file' },
{ status: 500 }
);
}
}
2 changes: 1 addition & 1 deletion apps/web/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const RootLayout = async ({
{children}
<ScrollToTop />
<Footer />
<AnalyticsProvider />
{isProd && <AnalyticsProvider />}
</ThemeProvider>
</body>
{isProd && <GoogleAnalytics gaId='G-CNW9F6PH7P' />}
Expand Down
13 changes: 7 additions & 6 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
"react-dom": "^18.0"
},
"dependencies": {
"@next/third-parties": "^15.0.3",
"@next/third-parties": "^15.0.4",
"@tanstack/react-table": "^8.20.5",
"@ultra-reporter/analytics": "workspace:*",
"@ultra-reporter/kv": "workspace:*",
"@ultra-reporter/feature-toggle": "workspace:*",
"@ultra-reporter/ui": "workspace:*",
"@ultra-reporter/utils": "workspace:*",
"lucide-react": "^0.462.0",
"next": "15.0.3"
"lucide-react": "^0.468.0",
"next": "15.0.4"
},
"devDependencies": {
"@types/react": "^18.3.12",
"@types/react-dom": "^18",
"@types/react": "^19.0.1",
"@types/react-dom": "^19.0.1",
"@ultra-reporter/typescript-config": "workspace:*",
"postcss": "^8.4.49",
"tailwindcss": "^3.4.15"
"tailwindcss": "^3.4.16"
}
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@
"release:prepatch": "pnpm beta prepatch"
},
"devDependencies": {
"@eslint/compat": "^1.2.3",
"@next/eslint-plugin-next": "^15.0.3",
"@eslint/compat": "^1.2.4",
"@next/eslint-plugin-next": "^15.0.4",
"@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.1",
"@typescript-eslint/eslint-plugin": "^8.16.0",
"@typescript-eslint/parser": "^8.16.0",
"@typescript-eslint/eslint-plugin": "^8.17.0",
"@typescript-eslint/parser": "^8.17.0",
"@vercel/style-guide": "^6.0.0",
"eslint": "^9.16.0",
"eslint-config-next": "15.0.3",
"eslint-config-next": "15.0.4",
"eslint-config-prettier": "^9.1.0",
"eslint-config-turbo": "2.3.3",
"eslint-plugin-only-warn": "^1.1.0",
"eslint-plugin-prettier": "^5.2.1",
"globals": "^15.12.0",
"globals": "^15.13.0",
"husky": "^9.1.7",
"lerna-changelog": "^2.2.0",
"lint-staged": "^15.2.10",
"prettier": "^3.4.1",
"prettier": "^3.4.2",
"prettier-plugin-organize-imports": "^4.1.0",
"prettier-plugin-tailwindcss": "^0.6.9",
"release-it": "^17.10.0",
"release-it-pnpm": "^4.6.3",
"turbo": "^2.3.3",
"typescript": "^5.7.2",
"typescript-eslint": "^8.16.0"
"typescript-eslint": "^8.17.0"
},
"lint-staged": {
"**/*.{ts,tsx}": [
Expand Down
4 changes: 2 additions & 2 deletions packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"./client": "./src/client.tsx"
},
"dependencies": {
"@openpanel/nextjs": "^1.0.6",
"@openpanel/nextjs": "^1.0.7",
"@ultra-reporter/utils": "workspace:*",
"@vercel/functions": "^1.5.1"
},
"devDependencies": {
"@types/react": "^18.3.12",
"@types/react": "^19.0.1",
"@ultra-reporter/logger": "workspace:*",
"@ultra-reporter/typescript-config": "workspace:*"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/feature-toggle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"./provider": "./src/provider.tsx"
},
"devDependencies": {
"@types/react": "^18.3.12",
"@types/react-dom": "^18",
"@types/react": "^19.0.1",
"@types/react-dom": "^19.0.1",
"@ultra-reporter/typescript-config": "workspace:*"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/feature-toggle/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { createFlagsmithInstance } from 'flagsmith/isomorphic';
import { FlagsmithProvider, useFlags } from 'flagsmith/react';
import { IFlagsmithFeature, IFlagsmithTrait, IState } from 'flagsmith/types';
import { useRef } from 'react';
import { JSX, useRef } from 'react';
import { Flags } from './flag-list';

interface FeatureProviderProps {
Expand Down
21 changes: 21 additions & 0 deletions packages/kv/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@ultra-reporter/kv",
"description": "Ultra Reporter Redis and Rate limiter",
"version": "0.6.0",
"private": true,
"scripts": {
"lint": "eslint . --max-warnings 0"
},
"exports": {
"./redis": "./src/index.ts",
"./ratelimit": "./src/rate-limit.ts"
},
"devDependencies": {
"@ultra-reporter/typescript-config": "workspace:*"
},
"dependencies": {
"@upstash/ratelimit": "^2.0.5",
"@upstash/redis": "^1.34.3",
"server-only": "^0.0.1"
}
}
4 changes: 4 additions & 0 deletions packages/kv/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Redis } from '@upstash/redis';
import 'server-only';

export const redis = Redis.fromEnv();
10 changes: 10 additions & 0 deletions packages/kv/src/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Ratelimit } from '@upstash/ratelimit';
import 'server-only';
import { redis } from './index';

export const ratelimit = new Ratelimit({
limiter: Ratelimit.slidingWindow(5, '10s'),
redis,
analytics: true,
timeout: 10000,
});
11 changes: 11 additions & 0 deletions packages/kv/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": ["dist", "build", "node_modules"]
}
Loading
Loading