This commit is contained in:
2026-06-24 16:52:08 +02:00
commit 5abeb4fd48
53 changed files with 276551 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
NEXT_PUBLIC_BACKEND_ORIGIN=
+36
View File
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
+1
View File
@@ -0,0 +1 @@
## This is the Next.js frontend for our Data Science project.
Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

+23
View File
@@ -0,0 +1,23 @@
import type { Metadata } from 'next'
import { Providers } from './providers'
import { Flex } from '@chakra-ui/react'
export const metadata: Metadata = {
title: 'HSL Bike Helper',
description: 'An app that makes prediction based on Open Data from the Helsinki Bike System.',
}
const RootLayout = ({ children, }: Readonly<{ children: React.ReactNode }>) => {
return (
<html lang='en'>
<body>
<Providers>
<Flex justifyContent='center' width='100%' padding={4} minH='calc(100vh - 72px)'>
{children}
</Flex>
</Providers>
</body>
</html>
)
}
export default RootLayout
+26
View File
@@ -0,0 +1,26 @@
import type { MetadataRoute } from 'next'
const manifest = (): MetadataRoute.Manifest => {
return {
name: 'HSL Bike Helper',
short_name: 'HSL',
description: 'An app that makes prediction based on Open Data from the Helsinki Bike System.',
start_url: '/',
display: 'standalone',
background_color: '#ffffff',
theme_color: '#000000',
icons: [
{
src: '/icon-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: '/icon-512x512.png',
sizes: '512x512',
type: 'image/png',
},
],
}
}
export default manifest
+78
View File
@@ -0,0 +1,78 @@
'use client'
import { InputField, Wrapper } from '@/components'
import { Box, Button, Flex, FormLabel, Select, Text } from '@chakra-ui/react'
import { Field, Form, Formik } from 'formik'
import { useState } from 'react'
interface Response {
timestamp: string
station: string
departingCount: number
returningCount: number
bikeAtStationCount: number
increasing: boolean
}
const Home: React.FC = () => {
const [predictionData, setPredictionData] = useState<Response>()
return (
<Box w='100%'>
<Wrapper>
<Formik
initialValues={{ station: 'Kamppi (M)', timestamp: '' }}
onSubmit={async ({ station, timestamp }, { setErrors }) => {
const response: Response = await (await fetch(`${process.env.NEXT_PUBLIC_BACKEND_ORIGIN}/app/predict?timestamp=${timestamp}&station=${station}`)).json()
setPredictionData(response)
}}
>
{({ isSubmitting, values }) => (
<Form>
<Flex flexDir='column' alignItems='center' mt={4}>
<Field name='station'>
{/* @ts-ignore */}
{({ field }) => (
<>
<FormLabel htmlFor='station'>Station</FormLabel>
<Select id='station' placeholder='' textAlign='center' {...field} >
<option value='Kamppi (M)'>Kamppi (M)</option>
<option value='Rautatientori - itä'>Rautatientori - itä</option>
</Select>
</>
)}
</Field>
<FormLabel htmlFor='timestamp' mt={4} mb={0}>Timestamp</FormLabel>
<InputField name='timestamp' label='' placeholder='YYYY-MM-DD HH:MM:SS' textAlign='center' />
<Button type='submit' mt={4} disabled={isSubmitting} isLoading={isSubmitting}>Predict</Button>
{
predictionData &&
<Box mt={4}>
<Text>Timestamp: {predictionData.timestamp}</Text>
<Text>Station: {predictionData.station}</Text>
<Text>Predicted Number of Bikes Arriving at Station: {predictionData.returningCount}</Text>
<Text>Predicted Number of Bikes Departing from Station: {predictionData.departingCount}</Text>
<Text>Predicted Number of Bikes at Station: {predictionData.bikeAtStationCount}</Text>
<Text>Predicted Bike Count Status: {predictionData.increasing ? 'Decreasing' : 'Increasing'}</Text>
<Text>
In need of more bikes: &nbsp;
{
predictionData.bikeAtStationCount > 10 ? 'No' :
predictionData.bikeAtStationCount > 5 ? 'Yes, but not urgently.' :
'Yes, urgently.'
}
</Text>
</Box>
}
</Flex>
</Form>
)}
</Formik>
</Wrapper>
</Box>
)
}
export default Home
+9
View File
@@ -0,0 +1,9 @@
'use client'
import { ChakraProvider } from '@chakra-ui/react'
export const Providers = ({ children }: { children: React.ReactNode }) => {
return (
<ChakraProvider>{children}</ChakraProvider>
)
}
+18
View File
@@ -0,0 +1,18 @@
'use client'
import { FormControl, FormErrorMessage, FormLabel, Input, InputProps } from '@chakra-ui/react'
import { useField } from 'formik'
type Props = React.InputHTMLAttributes<HTMLInputElement> & { name: string, label: string, isTextArea?: boolean } & InputProps
export const InputField: React.FC<Props> = ({size: _, isTextArea=false, ...props}) => {
const [field, { error }] = useField(props)
const { label, placeholder } = props
return (
<FormControl isInvalid={!!error}>
<FormLabel htmlFor={field.name}>{label}</FormLabel>
<Input {...field} {...props} id={field.name} placeholder={placeholder} />
{error ? <FormErrorMessage>{error}</FormErrorMessage> : null}
</FormControl>
)
}
+36
View File
@@ -0,0 +1,36 @@
'use client'
import { FormControl, FormErrorMessage, FormLabel, Textarea, TextareaProps } from '@chakra-ui/react'
import autosize from 'autosize'
import { useField } from 'formik'
import { useRef, useEffect } from 'react'
type Props = React.TextareaHTMLAttributes<HTMLTextAreaElement> & { name: string, label: string } & TextareaProps
export const TextareaField: React.FC<Props> = (props): JSX.Element => {
const [field, { error }] = useField(props)
const { label, placeholder } = props
// https://github.com/chakra-ui/chakra-ui/issues/670
const ref: any = useRef()
useEffect(() => {
const current = ref.current
autosize(current)
return () => {
autosize.destroy(current)
}
}, [])
return (
<FormControl isInvalid={!!error}>
<FormLabel htmlFor={field.name}>{label}</FormLabel>
<Textarea
{...field}
{...props}
ref={ref}
id={field.name}
placeholder={placeholder}
/>
{error ? <FormErrorMessage>{error}</FormErrorMessage> : null}
</FormControl>
)
}
+75
View File
@@ -0,0 +1,75 @@
'use client'
// ChatGPT-generated
import { Line } from 'react-chartjs-2'
import { Chart as ChartJS, TimeScale, LinearScale, PointElement, LineElement, Tooltip, Legend } from 'chart.js'
import 'chartjs-adapter-date-fns'
import { Entry } from '@/constants'
// Register required chart components
ChartJS.register(TimeScale, LinearScale, PointElement, LineElement, Tooltip, Legend)
export const TripCountChart = ({ data }: { data: Entry[] }) => {
const chartData = {
labels: data.map((entry) => entry.time),
datasets: [
{
label: 'Trip Count',
data: data.map((entry) => entry.trip_count),
fill: false,
borderColor: 'rgba(75,192,192,1)',
tension: 0.1,
segment: {
borderColor: (ctx: any) => {
const dataIndex = ctx.p0DataIndex // Starting index of the segment
const totalPoints = data.length
const threeQuarterIndex = Math.floor(totalPoints * 0.75)
// Use a different color after 3/4 of the x-axis
return dataIndex >= threeQuarterIndex
? 'rgba(255,99,132,1)' // Different color after 3/4
: 'rgba(75,192,192,1)' // Default color for the first 3/4
},
},
},
],
}
const options = {
scales: {
x: {
type: 'time',
time: {
unit: 'hour',
tooltipFormat: 'PPpp',
displayFormats: {
hour: 'MMM d, yyyy HH:mm',
},
},
title: {
display: true,
text: 'Time (Hour)',
},
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Trip Count',
},
},
},
plugins: {
tooltip: {
callbacks: {
label: function (context: any) {
return `Trip Count: ${context.raw}`
},
},
},
},
}
return <Line data={chartData} options={options as any} />
}
export default TripCountChart
+14
View File
@@ -0,0 +1,14 @@
import { Box } from '@chakra-ui/react'
interface Props {
children?: React.ReactNode
variant?: 'small' | 'regular'
}
export const Wrapper: React.FC<Props> = ({ children, variant = 'regular' }) => {
return (
<Box w='100%' maxW={variant == 'small' ? 400 : 800} mt={8} mx='auto'>
{children}
</Box>
)
}
+4
View File
@@ -0,0 +1,4 @@
export * from './Wrapper'
export * from './TripCountChart'
export * from './InputField'
export * from './TextareaField'
+1
View File
@@ -0,0 +1 @@
export * from './sampleTripCountData'
File diff suppressed because it is too large Load Diff
+9
View File
@@ -0,0 +1,9 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
NEXT_PUBLIC_BACKEND_ORIGIN: string;
}
}
}
export {}
+4
View File
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}
export default nextConfig
+2603
View File
File diff suppressed because it is too large Load Diff
+36
View File
@@ -0,0 +1,36 @@
{
"name": "hsl-frontend",
"version": "1.0.0",
"private": true,
"scripts": {
"env:generate": "gen-env-types .env -o env.d.ts -e .",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@chakra-ui/next-js": "^2.2.0",
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"autosize": "^6.0.1",
"chart.js": "^4.4.4",
"chartjs-adapter-date-fns": "^3.0.0",
"date-fns": "^4.1.0",
"formik": "^2.4.6",
"framer-motion": "^11.5.4",
"next": "14.2.9",
"react": "^18",
"react-chartjs-2": "^5.2.0",
"react-dom": "^18"
},
"devDependencies": {
"@types/autosize": "^4.0.3",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"gen-env-types": "^1.3.4",
"typescript": "^5"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

+39
View File
@@ -0,0 +1,39 @@
{
"compilerOptions": {
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": [
"./*"
]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}