2021-10-14 10:57:49 +00:00
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
2021-07-08 09:23:22 +00:00
|
|
|
import utc from "dayjs/plugin/utc";
|
2021-08-09 09:24:39 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-10-14 10:57:49 +00:00
|
|
|
import { QueryCell } from "@lib/QueryCell";
|
2021-10-13 10:49:15 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-10-14 10:57:49 +00:00
|
|
|
import { inferQueryOutput, trpc } from "@lib/trpc";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
|
|
|
import Loader from "@components/Loader";
|
2021-08-27 12:35:20 +00:00
|
|
|
import Shell from "@components/Shell";
|
2021-08-02 20:51:57 +00:00
|
|
|
|
2021-07-08 09:23:22 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
|
2021-10-14 10:57:49 +00:00
|
|
|
type User = inferQueryOutput<"viewer.me">;
|
|
|
|
|
|
|
|
const AvailabilityView = ({ user }: { user: User }) => {
|
2021-10-13 10:49:15 +00:00
|
|
|
const { t } = useLocale();
|
2021-08-09 09:24:39 +00:00
|
|
|
const [loading, setLoading] = useState(true);
|
2022-02-09 00:05:13 +00:00
|
|
|
const [availability, setAvailability] = useState<{ end: string; start: string }[]>([]);
|
2021-10-07 22:49:53 +00:00
|
|
|
const [selectedDate, setSelectedDate] = useState(dayjs());
|
2021-07-08 09:23:22 +00:00
|
|
|
|
2021-09-24 10:16:46 +00:00
|
|
|
function convertMinsToHrsMins(mins: number) {
|
2021-07-08 09:23:22 +00:00
|
|
|
let h = Math.floor(mins / 60);
|
|
|
|
let m = mins % 60;
|
2022-02-24 19:36:02 +00:00
|
|
|
let hs = h < 10 ? "0" + h : h;
|
|
|
|
let ms = m < 10 ? "0" + m : m;
|
|
|
|
return `${hs}:${ms}`;
|
2021-07-08 09:23:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 09:24:39 +00:00
|
|
|
useEffect(() => {
|
2021-10-14 10:57:49 +00:00
|
|
|
const fetchAvailability = (date: Dayjs) => {
|
|
|
|
const dateFrom = date.startOf("day").utc().format();
|
|
|
|
const dateTo = date.endOf("day").utc().format();
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
fetch(`/api/availability/${user.username}?dateFrom=${dateFrom}&dateTo=${dateTo}`)
|
|
|
|
.then((res) => {
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((availableIntervals) => {
|
|
|
|
setAvailability(availableIntervals.busy);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setLoading(false);
|
|
|
|
});
|
|
|
|
};
|
2021-08-09 09:24:39 +00:00
|
|
|
fetchAvailability(selectedDate);
|
2021-08-12 04:54:01 +00:00
|
|
|
}, [selectedDate]);
|
2021-08-09 09:24:39 +00:00
|
|
|
|
2021-07-08 09:23:22 +00:00
|
|
|
return (
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="max-w-xl overflow-hidden rounded-sm bg-white shadow">
|
2021-10-14 10:57:49 +00:00
|
|
|
<div className="px-4 py-5 sm:p-6">
|
|
|
|
{t("overview_of_day")}{" "}
|
|
|
|
<input
|
|
|
|
type="date"
|
2022-02-09 00:05:13 +00:00
|
|
|
className="inline h-8 border-none p-0"
|
2021-10-14 10:57:49 +00:00
|
|
|
defaultValue={selectedDate.format("YYYY-MM-DD")}
|
|
|
|
onChange={(e) => {
|
|
|
|
setSelectedDate(dayjs(e.target.value));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<small className="block text-neutral-400">{t("hover_over_bold_times_tip")}</small>
|
|
|
|
<div className="mt-4 space-y-4">
|
2022-03-05 15:37:46 +00:00
|
|
|
<div className="bg-brand dark:bg-darkmodebrand overflow-hidden rounded-sm">
|
|
|
|
<div className="text-brandcontrast dark:text-darkmodebrandcontrast px-4 py-2 sm:px-6">
|
2021-10-14 10:57:49 +00:00
|
|
|
{t("your_day_starts_at")} {convertMinsToHrsMins(user.startTime)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{loading ? (
|
|
|
|
<Loader />
|
|
|
|
) : availability.length > 0 ? (
|
|
|
|
availability.map((slot) => (
|
2021-12-14 10:39:32 +00:00
|
|
|
<div key={slot.start} className="overflow-hidden rounded-sm bg-neutral-100">
|
|
|
|
<div className="px-4 py-5 text-black sm:p-6">
|
2021-10-14 10:57:49 +00:00
|
|
|
{t("calendar_shows_busy_between")}{" "}
|
|
|
|
<span className="font-medium text-neutral-800" title={slot.start}>
|
|
|
|
{dayjs(slot.start).format("HH:mm")}
|
|
|
|
</span>{" "}
|
|
|
|
{t("and")}{" "}
|
|
|
|
<span className="font-medium text-neutral-800" title={slot.end}>
|
|
|
|
{dayjs(slot.end).format("HH:mm")}
|
|
|
|
</span>{" "}
|
|
|
|
{t("on")} {dayjs(slot.start).format("D")}{" "}
|
|
|
|
{t(dayjs(slot.start).format("MMMM").toLowerCase())} {dayjs(slot.start).format("YYYY")}
|
2021-07-08 09:23:22 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-14 10:57:49 +00:00
|
|
|
))
|
|
|
|
) : (
|
2021-12-14 10:39:32 +00:00
|
|
|
<div className="overflow-hidden rounded-sm bg-neutral-100">
|
|
|
|
<div className="px-4 py-5 text-black sm:p-6">{t("calendar_no_busy_slots")}</div>
|
2021-10-14 10:57:49 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2022-03-05 15:37:46 +00:00
|
|
|
<div className="bg-brand dark:bg-darkmodebrand overflow-hidden rounded-sm">
|
|
|
|
<div className="text-brandcontrast dark:text-darkmodebrandcontrast px-4 py-2 sm:px-6">
|
2021-10-14 10:57:49 +00:00
|
|
|
{t("your_day_ends_at")} {convertMinsToHrsMins(user.endTime)}
|
2021-07-08 09:23:22 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-14 10:57:49 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function Troubleshoot() {
|
|
|
|
const query = trpc.useQuery(["viewer.me"]);
|
|
|
|
const { t } = useLocale();
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Shell heading={t("troubleshoot")} subtitle={t("troubleshoot_description")}>
|
|
|
|
<QueryCell query={query} success={({ data }) => <AvailabilityView user={data} />} />
|
2021-07-08 09:23:22 +00:00
|
|
|
</Shell>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|