diff --git a/package-lock.json b/package-lock.json index 1a61ea51..d2d9bd42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "i18next": "^21.9.2", "js-yaml": "^4.1.0", "json-rpc-2.0": "^1.4.1", + "luxon": "^3.4.3", "memory-cache": "^0.2.0", "minecraft-ping-js": "^1.0.2", "next": "^12.3.1", @@ -4098,6 +4099,14 @@ "node": ">=10" } }, + "node_modules/luxon": { + "version": "3.4.3", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.4.3.tgz", + "integrity": "sha512-tFWBiv3h7z+T/tDaoxA8rqTxy1CHV6gHS//QdaH4pulbq/JuBSGgQspQQqcgnwdAx6pNI7cmvz5Sv/addzHmUg==", + "engines": { + "node": ">=12" + } + }, "node_modules/memory-cache": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/memory-cache/-/memory-cache-0.2.0.tgz", diff --git a/package.json b/package.json index f6e39a97..6a33b5ed 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "i18next": "^21.9.2", "js-yaml": "^4.1.0", "json-rpc-2.0": "^1.4.1", + "luxon": "^3.4.3", "memory-cache": "^0.2.0", "minecraft-ping-js": "^1.0.2", "next": "^12.3.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c84dfffb..6ff7151a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,6 +35,9 @@ dependencies: json-rpc-2.0: specifier: ^1.4.1 version: 1.5.1 + luxon: + specifier: ^3.4.3 + version: 3.4.3 memory-cache: specifier: ^0.2.0 version: 0.2.0 @@ -2646,6 +2649,11 @@ packages: dependencies: yallist: 4.0.0 + /luxon@3.4.3: + resolution: {integrity: sha512-tFWBiv3h7z+T/tDaoxA8rqTxy1CHV6gHS//QdaH4pulbq/JuBSGgQspQQqcgnwdAx6pNI7cmvz5Sv/addzHmUg==} + engines: {node: '>=12'} + dev: false + /memory-cache@0.2.0: resolution: {integrity: sha512-OcjA+jzjOYzKmKS6IQVALHLVz+rNTMPoJvCztFaZxwG14wtAW7VRZjwTQu06vKCYOxh4jVnik7ya0SXTB0W+xA==} dev: false diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx index 7b93b005..2a1f4c74 100644 --- a/src/pages/_app.jsx +++ b/src/pages/_app.jsx @@ -12,6 +12,7 @@ import { ColorProvider } from "utils/contexts/color"; import { ThemeProvider } from "utils/contexts/theme"; import { SettingsProvider } from "utils/contexts/settings"; import { TabProvider } from "utils/contexts/tab"; +import { EventProvider, ShowDateProvider } from "utils/contexts/calendar"; function MyApp({ Component, pageProps }) { return ( @@ -28,7 +29,11 @@ function MyApp({ Component, pageProps }) { - + + + + + diff --git a/src/utils/config/service-helpers.js b/src/utils/config/service-helpers.js index 775977f3..2dcfb0cc 100644 --- a/src/utils/config/service-helpers.js +++ b/src/utils/config/service-helpers.js @@ -357,6 +357,7 @@ export function cleanServiceGroups(groups) { method, // openmediavault widget mappings, // customapi widget refreshInterval, + integration, // calendar widget } = cleanedService.widget; let fieldsList = fields; @@ -440,6 +441,9 @@ export function cleanServiceGroups(groups) { if (mappings) cleanedService.widget.mappings = mappings; if (refreshInterval) cleanedService.widget.refreshInterval = refreshInterval; } + if (type === "calendar") { + if (integration) cleanedService.widget.integration = integration; + } } return cleanedService; diff --git a/src/utils/contexts/calendar.jsx b/src/utils/contexts/calendar.jsx new file mode 100644 index 00000000..70616d5f --- /dev/null +++ b/src/utils/contexts/calendar.jsx @@ -0,0 +1,28 @@ +import { createContext, useState, useMemo } from "react"; + +export const EventContext = createContext(); +export const ShowDateContext = createContext(); + +export function EventProvider({ initialEvent, children }) { + const [events, setEvents] = useState({}); + + if (initialEvent) { + setEvents(initialEvent); + } + + const value = useMemo(() => ({ events, setEvents }), [events]); + + return {children}; +} + +export function ShowDateProvider({ initialDate, children }) { + const [showDate, setShowDate] = useState(null); + + if (initialDate) { + setShowDate(initialDate); + } + + const value = useMemo(() => ({ showDate, setShowDate }), [showDate]); + + return {children}; +} diff --git a/src/utils/proxy/handlers/generic.js b/src/utils/proxy/handlers/generic.js index 93037dc5..d62cc341 100644 --- a/src/utils/proxy/handlers/generic.js +++ b/src/utils/proxy/handlers/generic.js @@ -18,7 +18,8 @@ export default async function genericProxyHandler(req, res, map) { } if (widget) { - const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget })); + // if there are more than one question marks, replace others to & + const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }).replace(/(?<=\?.*)\?/g, '&')); const headers = req.extraHeaders ?? widget.headers ?? {}; diff --git a/src/widgets/calendar/component.jsx b/src/widgets/calendar/component.jsx new file mode 100644 index 00000000..7fa0eb10 --- /dev/null +++ b/src/widgets/calendar/component.jsx @@ -0,0 +1,47 @@ +import { useContext, useMemo } from "react"; +import dynamic from "next/dynamic"; + +import { ShowDateContext } from "../../utils/contexts/calendar"; + +import MonthlyView from "./monthly-view"; + +import Container from "components/services/widget/container"; + +export default function Component({ service }) { + const { widget } = service; + const { showDate } = useContext(ShowDateContext); + + // params for API fetch + const params = useMemo(() => { + if (!showDate) { + return {}; + } + + return { + start: showDate.minus({months: 3}).toFormat("yyyy-MM-dd"), + end: showDate.plus({months: 3}).toFormat("yyyy-MM-dd"), + unmonitored: 'false', + }; + }, [showDate]); + + // Load active integrations + const integrations = useMemo(() => widget.integration?.map(integration => ({ + service: dynamic(() => import(`./integrations/${integration?.type}`)), + widget: integration, + })) ?? [], [widget.integration]); + + return +
+
+ {integrations.map(integration => { + const Integration = integration.service; + const key = integration.widget.type + integration.widget.service_name + integration.widget.service_group; + + return + })} +
+ +
+
; +} diff --git a/src/widgets/calendar/monthly-view.jsx b/src/widgets/calendar/monthly-view.jsx new file mode 100644 index 00000000..21a58cb2 --- /dev/null +++ b/src/widgets/calendar/monthly-view.jsx @@ -0,0 +1,156 @@ +import { useContext, useMemo } from "react"; +import { DateTime, Info } from "luxon"; +import classNames from "classnames"; +import { useTranslation } from "next-i18next"; + +import { EventContext, ShowDateContext } from "../../utils/contexts/calendar"; + +const colorVariants = { + // https://tailwindcss.com/docs/content-configuration#dynamic-class-names + amber: "bg-amber-500", blue: "bg-blue-500", cyan: "bg-cyan-500", + emerald: "bg-emerald-500", fuchsia: "bg-fuchsia-500", gray: "bg-gray-500", + green: "bg-green-500", indigo: "bg-indigo-500", lime: "bg-lime-500", + neutral: "bg-neutral-500", orange: "bg-orange-500", pink: "bg-pink-500", + purple: "bg-purple-500", red: "bg-red-500", rose: "bg-rose-500", + sky: "bg-sky-500", slate: "bg-slate-500", stone: "bg-stone-500", + teal: "bg-teal-500", violet: "bg-violet-500", white: "bg-white-500", + yellow: "bg-yellow-500", zinc: "bg-zinc-500", +} + +const cellStyle = "relative w-10 flex items-center justify-center flex-col"; +const monthButton = "pl-6 pr-6 ml-2 mr-2 hover:bg-theme-100/20 dark:hover:bg-white/5 rounded-md cursor-pointer" + +export function Day({ weekNumber, weekday, events }) { + const currentDate = DateTime.now(); + const { showDate, setShowDate } = useContext(ShowDateContext); + + const cellDate = showDate.set({ weekday, weekNumber }).startOf("day"); + const filteredEvents = events?.filter(event => event.date?.startOf("day").toUnixInteger() === cellDate.toUnixInteger()); + + const dayStyles = (displayDate) => { + let style = "h-9 "; + + if ([6,7].includes(displayDate.weekday)) { + // weekend style + style += "text-red-500 "; + // different month style + style += displayDate.month !== showDate.month ? "text-red-500/40 " : ""; + } else if (displayDate.month !== showDate.month) { + // different month style + style += "text-gray-500 "; + } + + // selected same day style + style += displayDate.toFormat("MM-dd-yyyy") === showDate.toFormat("MM-dd-yyyy") ? "text-black-500 bg-theme-100/20 dark:bg-white/10 rounded-md " : ""; + + if (displayDate.toFormat("MM-dd-yyyy") === currentDate.toFormat("MM-dd-yyyy")) { + // today style + style += "text-black-500 bg-theme-100/20 dark:bg-black/20 rounded-md "; + } else { + style += "hover:bg-theme-100/20 dark:hover:bg-white/5 rounded-md cursor-pointer "; + } + + return style; + } + + return +} + +export function Event({ event }) { + let title = event.title.slice(0,55); + if (title !== event.title) { + title += "..."; + } + + return
{title} +
+} + +const dayInWeekId = { + monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 7 +}; + + +export default function MonthlyView({ service }) { + const { widget } = service; + const { i18n } = useTranslation(); + const { showDate, setShowDate } = useContext(ShowDateContext); + const { events } = useContext(EventContext); + const currentDate = DateTime.now().setLocale(i18n.language).startOf("day"); + + if (!showDate) { + setShowDate(currentDate); + } + + const dayNames = Info.weekdays("short", { locale: i18n.language }); + + const firstDayInCalendar = widget?.firstDayInCalendar ? widget?.firstDayInCalendar?.toLowerCase() : "monday"; + for (let i = 1; i < dayInWeekId[firstDayInCalendar]; i+=1) { + dayNames.push(dayNames.shift()); + } + + const daysInWeek = useMemo(() => [ ...Array(7).keys() ].map( i => i + dayInWeekId[firstDayInCalendar] + ), [(firstDayInCalendar)]); + + if (!showDate) { + return
; + } + + const firstWeek = DateTime.local(showDate.year, showDate.month, 1).setLocale(i18n.language); + + let weekNumbers = [ ...Array(Math.ceil(5) + 1).keys() ] + .map(i => firstWeek.weekNumber+i); + + if (weekNumbers.includes(55)) { + // if we went too far with the weeks, it's the beginning of the year + weekNumbers = weekNumbers.map(weekNum => weekNum-52 ); + } + + const eventsArray = Object.keys(events).map(eventKey => events[eventKey]); + + return
+
+ + { showDate.setLocale(i18n.language).toFormat("MMMM y") } + +
+ +
+
+ { dayNames.map(name => {name}) } +
+ +
{weekNumbers.map(weekNumber => + daysInWeek.map(dayInWeek => + + ) + )} +
+ +
+ {eventsArray?.filter(event => showDate.startOf("day").toUnixInteger() === event.date?.startOf("day").toUnixInteger()) + .map(event => )} +
+
+
+} diff --git a/src/widgets/components.js b/src/widgets/components.js index 66044406..9d311b97 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -9,6 +9,7 @@ const components = { azuredevops: dynamic(() => import("./azuredevops/component")), bazarr: dynamic(() => import("./bazarr/component")), caddy: dynamic(() => import("./caddy/component")), + calendar: dynamic(() => import("./calendar/component")), calibreweb: dynamic(() => import("./calibreweb/component")), changedetectionio: dynamic(() => import("./changedetectionio/component")), channelsdvrserver: dynamic(() => import("./channelsdvrserver/component")),