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

Csb 2toymx/draft/vigilant framework #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@
"preview": "vite preview"
},
"dependencies": {
"d3": "^7.8.4",
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"redux-thunk": "^2.4.2",
"sass": "^1.62.1",
"use-sound": "^4.0.1",
"react-redux": "^8.0.5"
},
"devDependencies": {
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@reduxjs/toolkit": "^1.9.5",
"@types/d3": "^7.4.0",
"@types/lodash": "^4.14.195",
"@types/react": "^18.2.7",
"@vitejs/plugin-react": "^2.0.0",
"typescript": "^4.6.4",
"vite": "^3.0.0"
"vite": "^3.0.0",
"typescript": "^4.6.4"
}
}
}
64 changes: 32 additions & 32 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
.App {
display: flex;
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
.app-container {
width: 90%;
height: 90%;
border: 1px solid #ccc;
display: flex;
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
.action-panel {
width: 40%;
height: 100%;
display: flex;
flex-direction: column;
gap: 20px;
padding: 40px 15px 20px;
box-sizing: border-box;
}

.card {
padding: 2em;
.content {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
background-color: #ffffff;
}

.read-the-docs {
color: #888;
.csv-actions {
background: white;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px;
}
51 changes: 23 additions & 28 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import { useState } from "react";
import reactLogo from "./assets/react.svg";
import React, { Suspense, useState } from "react";
import "./App.css";
import { useAppState } from "./hooks";
import { Loader } from "./components";

const UploadWrapper = React.lazy(() => import("./components/UploadWrapper"));
const Filters = React.lazy(() => import("./components/Filters"));
const ChartWrapper = React.lazy(() => import("./components/ChartWrapper"));

function App() {
const [count, setCount] = useState(0);
const [csvData, setCsvData] = useState<any[]>([]);
const { isLoading, selectedTags, filteredData } = useAppState();

return (
<div className="App">
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" className="logo" alt="Vite logo" />
</a>
<a href="https://reactjs.org" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<h2>On CodeSandbox!</h2>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR.
</p>

<p>
Tip: you can use the inspector button next to address bar to click on
components in the preview and open the code in the editor!
</p>
<div className="app-container">
<div className="action-panel">
<Suspense fallback={<Loader />}>
<UploadWrapper {...{ isLoading, csvData, setCsvData }} />
</Suspense>
<Suspense fallback={<Loader />}>
<Filters data={csvData} />
</Suspense>
</div>
<div className="content">
<Suspense fallback={<Loader />}>
<ChartWrapper {...{ isLoading, selectedTags, filteredData }} />
</Suspense>
</div>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</div>
);
}
Expand Down
Binary file added src/assets/boop.mp3
Binary file not shown.
Binary file added src/assets/close-open.mp3
Binary file not shown.
Binary file added src/assets/csv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/assets/react.svg

This file was deleted.

Binary file added src/assets/remove.mp3
Binary file not shown.
Binary file added src/assets/tag.mp3
Binary file not shown.
Binary file added src/assets/toggle.mp3
Binary file not shown.
82 changes: 82 additions & 0 deletions src/components/Chart/Chart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React, { useRef, useEffect } from "react";
import * as d3 from "d3";
import { useSelector } from "react-redux";
import { RootState } from "../../store/store";

interface ChartDataItem {
date: string;
[key: string]: string | number | undefined;
}

interface ChartProps {
data: ChartDataItem[];
}

export function Chart({ data }: ChartProps) {
const ref = useRef<SVGSVGElement | null>(null);
const selectedTags = useSelector(
(state: RootState) => state.data.selectedTags
);
const valueType = useSelector((state: RootState) => state.data.valueType);

useEffect(() => {
const svg = d3.select(ref.current);
svg.selectAll("*").remove();

const width = 800;
const height = 600;
const margin = {
top: 0.1 * height,
right: 0.1 * width,
bottom: 0.1 * height,
left: 0.1 * width,
};

const xScale = d3
.scaleTime()
.domain(d3.extent(data, (d) => new Date(d.date)))
.range([margin.left, width - margin.right]);

selectedTags.forEach((tag) => {
const yScale = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d[tag]?.value ?? 0)])
.range([height - margin.bottom, margin.top]);

valueType.forEach((type) => {
const color = data[0][tag]?.color || "#000";

if (type === "mean") {
const line = d3
.line<ChartDataItem>()
.defined((d) => !isNaN(d[tag]?.value))
.x((d) => xScale(new Date(d.date)))
.y((d) => yScale(d[tag]?.value ?? 0))
.curve(d3.curveMonotoneX);

svg
.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", color)
.attr("stroke-width", 1.5)
.attr("d", line);
}

if (type === "original") {
svg
.selectAll(`circle.${tag}`)
.data(data)
.enter()
.append("circle")
.attr("cx", (d) => xScale(new Date(d.date)))
.attr("cy", (d) => yScale(d[tag]?.value ?? 0))
.attr("r", 3)
.attr("fill", color);
}
});
});
}, [data, selectedTags, valueType]);

return <svg ref={ref} width={800} height={600} fill="transparent"></svg>;
}
3 changes: 3 additions & 0 deletions src/components/Chart/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Chart } from "./Chart";

export default Chart;
Empty file.
24 changes: 24 additions & 0 deletions src/components/ChartWrapper/ChartWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import { Loader, Chart } from "../";

interface ChartWrapperProps {
isLoading: boolean;
selectedTags: string[];
filteredData: any[];
}

export const ChartWrapper: React.FC<ChartWrapperProps> = ({
isLoading,
selectedTags,
filteredData,
}) => {
if (isLoading) {
return <Loader />;
}

if (selectedTags?.length > 0 && filteredData.length > 0) {
return <Chart data={filteredData} />;
}

return null;
};
3 changes: 3 additions & 0 deletions src/components/ChartWrapper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ChartWrapper } from "./ChartWrapper";

export default ChartWrapper;
33 changes: 33 additions & 0 deletions src/components/CsvActions/CsvActions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
import { useDispatch } from "react-redux";
import useSound from "use-sound";
import removeSound from "../../assets/remove.mp3";
import { removeCsv } from "../../store/slices/dataSlice";

interface CsvActionsProps {
csvLogo: string;
setCsvData: any;
}

export const CsvActions: React.FC<CsvActionsProps> = ({
csvLogo,
setCsvData,
}) => {
const dispatch = useDispatch();
const [playRemoveSound] = useSound(removeSound, {
volume: 0.1,
});

const onRemoveCsv = () => {
playRemoveSound();
setCsvData([]);
dispatch(removeCsv());
};

return (
<div className="csv-actions">
<img src={csvLogo} alt="csv logo" />
<button onClick={onRemoveCsv}>Удалить</button>
</div>
);
};
3 changes: 3 additions & 0 deletions src/components/CsvActions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CsvActions } from "./CsvActions";

export default CsvActions;
65 changes: 65 additions & 0 deletions src/components/FileUploader/FileUploader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { ChangeEvent, useRef, useCallback } from "react";
import { useDispatch } from "react-redux";
import Papa from "papaparse";
import useSound from "use-sound";
import boopSound from "../../assets/boop.mp3";
import closeOpenSound from "../../assets/close-open.mp3";
import { setLoading } from "../../store/slices/dataSlice";

interface FileUploaderProps {
onFileParsed: (data: any[]) => void;
}

export const FileUploader: React.FC<FileUploaderProps> = ({ onFileParsed }) => {
const dispatch = useDispatch();
const fileInputRef = useRef<HTMLInputElement>(null);
const [playUploadSound] = useSound(boopSound, { volume: 0.1 });
const [playChangeSound] = useSound(closeOpenSound, {
playbackRate: 4,
volume: 0.1,
});

const handleFileChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];

if (!file) return;

dispatch(setLoading(true));

const dataBuffer: any[] = [];

Papa.parse(file, {
header: true,
step: (results) => dataBuffer.push(results.data),
complete: () => {
onFileParsed(dataBuffer);
dispatch(setLoading(false));
},
});

playChangeSound();
},
[dispatch, onFileParsed, playChangeSound]
);

const handleButtonClick = useCallback(() => {
playUploadSound();
fileInputRef.current?.click();
}, [playUploadSound]);

return (
<>
<input
type="file"
accept=".csv"
ref={fileInputRef}
hidden
onChange={handleFileChange}
/>
<button type="button" onClick={handleButtonClick}>
Загрузить CSV
</button>
</>
);
};
3 changes: 3 additions & 0 deletions src/components/FileUploader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { FileUploader } from "./FileUploader";

export default FileUploader;
Loading