Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/TKOaly/info-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Jokauppi committed Mar 26, 2024
2 parents 00c9680 + 902318c commit 4f68483
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/app/(infoscreen)/@ilotalo/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getIlotaloEvents } from '@/server/ilotaloEvents';
import Image from 'next/image';
import { Fragment } from 'react';
import { EventBox } from './EventBox';
import PohinaFactor from './pohinaFactor';

const IlotaloEvents = async () => {
const events = await getIlotaloEvents();
Expand All @@ -27,6 +28,7 @@ const IlotaloEvents = async () => {
</h2>
<div className="scrollbar-none -mt-20 flex min-h-0 w-full overflow-y-auto">
<div className="flex min-h-0 w-full flex-col pt-14">
<PohinaFactor />
{events &&
Object.entries(events).map(
([relativeWeek, events]) => (
Expand Down
51 changes: 51 additions & 0 deletions src/app/(infoscreen)/@ilotalo/pohinaFactor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Chip from '@/components/Chip';
import I18n from '@/components/I18n/I18n';
import { getPohinaFactor } from '@/server/pohinaFactor';
import { differenceInMinutes, isWithinInterval, setHours } from 'date-fns';

const PohinaFactor = async () => {
const { pohinaFactor, lastActivity } = await getPohinaFactor();

const differenceMin = differenceInMinutes(new Date(), lastActivity);
const differenceHours = Math.floor(differenceMin / 60);
const differenceDays = Math.floor(differenceMin / 60 / 24);

const differenceText =
differenceMin < 60
? `Aktiivisuutta havaittu ${differenceMin} min sitten // Activity detected ${differenceMin} min ago`
: differenceHours < 24
? `Aktiivisuutta havaittu ${differenceHours} h sitten // Activity detected ${differenceHours} h ago`
: `Aktiivisuutta havaittu ${differenceDays} pv sitten // Activity detected ${differenceDays} d ago`;

return (
!isWithinInterval(new Date(), {
start: setHours(new Date(), 7),
end: setHours(new Date(), 18),
}) && (
<>
<h3 className="p-2 text-2xl font-bold">
<I18n>Pöhinäkerroin // Pöhinäfactor</I18n>
</h3>
<hr className="w-full" />
<div className="p-4 pb-0">
<div className="flex gap-2">
{pohinaFactor !== undefined && (
<Chip className="bg-org-matlu-secondary text-grey-100">
<I18n>
{`Pöhinäkerroin: ${pohinaFactor.toString()} // Pöhinäfactor: ${pohinaFactor.toString()}`}
</I18n>
</Chip>
)}
{lastActivity && (
<Chip className="bg-org-matlu-secondary text-grey-100">
<I18n>{differenceText}</I18n>
</Chip>
)}
</div>
</div>
</>
)
);
};

export default PohinaFactor;
2 changes: 1 addition & 1 deletion src/app/(infoscreen)/@restaurants/Restaurant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type RestaurantMenuProps = {

export const Restaurant = ({ restaurant }: RestaurantMenuProps) => (
<div className="flex min-h-0 w-full flex-col items-center gap-y-4">
<h1 className="-mb-4 text-4xl">{restaurant.name}</h1>
<h1 className="-mb-4 text-4xl">Unicafe {restaurant.name}</h1>
{restaurant.lunchHours && (
<p className="text-xl">
{/suljettu/i.test(restaurant.lunchHours)
Expand Down
3 changes: 0 additions & 3 deletions src/app/(infoscreen)/@restaurants/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const Restaurants = async () => {
className="flex-col bg-green-unari px-8 pt-3 font-gabarito"
>
<CloseRestaurants restaurants={restaurants} />
<div className="flex w-full justify-center py-2">
<h1 className="text-5xl">Unicafe</h1>
</div>
<div
className={`grid size-full ${'grid-cols-' + Math.min(restaurants.length, 4)} gap-8`}
>
Expand Down
6 changes: 6 additions & 0 deletions src/app/RefetchIntervals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { revalidateTKOalyEvents } from '@/server/TKOalyEvents';
import { revalidateIlotaloEvents } from '@/server/ilotaloEvents';
import { revalidateLectures } from '@/server/lectures';
import { revalidatePohinaFactor } from '@/server/pohinaFactor';
import { revalidateRestaurants } from '@/server/restaurants';
import { useEffect } from 'react';

Expand All @@ -17,6 +18,10 @@ export const RefetchIntervals = () => {
async () => await revalidateIlotaloEvents(),
15 * 60 * 1000
);
const pohinaFactorInterval = setInterval(
async () => await revalidatePohinaFactor(),
5 * 60 * 1000
);
const restaurantsInterval = setInterval(
async () => await revalidateRestaurants(),
60 * 60 * 1000
Expand All @@ -29,6 +34,7 @@ export const RefetchIntervals = () => {
return () => {
clearInterval(TKOalyEventsInterval);
clearInterval(ilotaloEventsInterval);
clearInterval(pohinaFactorInterval);
clearInterval(restaurantsInterval);
clearInterval(lecturesInterval);
};
Expand Down
45 changes: 45 additions & 0 deletions src/server/pohinaFactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use server';

import { max } from 'date-fns';
import { revalidateTag } from 'next/cache';
import { GET } from './wrappers';

const ENTRYPOINT =
'https://klusteri.network/api/events/latest?sensors=[%22klusteri/ac/reed-1%22,%22klusteri/ac/reed-2%22]';

type SensorData = {
sensors: {
[key: number]: {
id: number;
name: string;
created_at: string;
updated_at: string;
};
};
events: {
[key: number]: {
id: string;
sensor_id: number;
type: string;
timestamp: string;
};
};
pohinaFactor: number;
};

const fetchTag = 'pohinaFactor';

export const getPohinaFactor = async () =>
await GET<SensorData>(ENTRYPOINT, {
next: {
tags: [fetchTag],
revalidate: 5 * 60,
},
}).then(({ events, pohinaFactor }) => ({
lastActivity: max(
Object.values(events).map((event) => new Date(event.timestamp))
),
pohinaFactor,
}));

export const revalidatePohinaFactor = async () => revalidateTag(fetchTag);

0 comments on commit 4f68483

Please sign in to comment.