Your favorite shift management application!
"use client";
import { useState } from "react";
import { useLocale, useTranslations } from "next-intl";
import { format, formatDistanceToNow } from "date-fns";
import { Eye, EyeOff, Link as LinkIcon, Trash2 } from "lucide-react";
import { ConfirmationDialog } from "@/components/ui/confirmation-dialog";
import { ListRow, Pill, RowIconButton, SectionLabel } from "@/components/form-kit";
import { useBundleDisplayName } from "@/components/permission-bundle-picker";
import { useCalendarTokens, type CalendarAccessToken } from "@/hooks/useCalendarTokens";
import { getDateLocale } from "@/lib/locales";
import { cn } from "@/lib/utils";
function isExpired(token: CalendarAccessToken) {
return !!token.expiresAt && new Date(token.expiresAt) < new Date();
}
export function isUsableToken(token: CalendarAccessToken) {
return token.isActive && !isExpired(token);
}
function TokenRow({
token,
onToggleActive,
onRevoke,
}: {
token: CalendarAccessToken;
onToggleActive: () => void;
onRevoke: () => void;
}) {
const t = useTranslations();
const displayName = useBundleDisplayName();
const dateLocale = getDateLocale(useLocale());
const expired = isExpired(token);
const dimmed = expired || !token.isActive;
const expiryDate = token.expiresAt
? format(new Date(token.expiresAt), "P", { locale: dateLocale })
: null;
const expiry = !expiryDate
? t("sharingSheet.noExpiry")
: expired
? t("sharingSheet.expiredOn", { date: expiryDate })
: t("sharingSheet.expiresOn", { date: expiryDate });
const usage = [
t("token.usedCount", { count: token.usageCount }),
token.lastUsedAt &&
t("sharingSheet.lastUsed", {
time: formatDistanceToNow(new Date(token.lastUsedAt), {
addSuffix: true,
locale: dateLocale,
}),
}),
]
.filter(Boolean)
.join(" · ");
return (
<ListRow className={cn(dimmed && "bg-surface-panel")}>
<LinkIcon className="size-4 shrink-0 text-fg-tertiary" />
<div className="min-w-0 flex-1">
<div
className={cn(
"truncate text-[14px] font-semibold",
dimmed ? "text-fg-secondary" : "text-fg-strong"
)}
>
{token.name || (
<span className="font-normal italic text-fg-tertiary">{t("token.unnamed")}</span>
)}
</div>
<div
className={cn(
"mt-0.5 truncate font-mono text-[12px]",
expired ? "text-danger" : "text-fg-tertiary"
)}
>
{expiry}
</div>
<div className="truncate text-[12px] text-fg-tertiary">{usage}</div>
</div>
<Pill>{displayName(token.bundle) ?? t("common.labels.permissions.none")}</Pill>
<RowIconButton
icon={token.isActive ? EyeOff : Eye}
label={token.isActive ? t("token.disable") : t("token.enable")}
onClick={onToggleActive}
/>
<RowIconButton icon={Trash2} tone="danger" label={t("token.revoke")} onClick={onRevoke} />
</ListRow>
);
}
/** Existing links of a calendar, split into active and inactive/expired. */
export function CalendarTokenList({ calendarId }: { calendarId: string }) {
const t = useTranslations();
const { tokens, updateToken, deleteToken } = useCalendarTokens(calendarId);
const [tokenToRevoke, setTokenToRevoke] = useState<CalendarAccessToken | null>(null);
const active = tokens.filter(isUsableToken);
const inactive = tokens.filter((token) => !isUsableToken(token));
const renderRows = (list: CalendarAccessToken[]) =>
list.map((token) => (
<TokenRow
key={token.id}
token={token}
onToggleActive={() => updateToken(token.id, { isActive: !token.isActive })}
onRevoke={() => setTokenToRevoke(token)}
/>
));
return (
<>
<section className="flex flex-col gap-2">
<SectionLabel className="mb-0">{t("sharingSheet.activeLinks")}</SectionLabel>
{active.length === 0 ? (
<p className="rounded-[11px] border border-dashed border-control px-3.5 py-3 text-center text-[13px] text-fg-tertiary">
{t("sharingSheet.noActiveLinks")}
</p>
) : (
renderRows(active)
)}
</section>
{inactive.length > 0 && (
<section className="flex flex-col gap-2">
<SectionLabel className="mb-0">{t("sharingSheet.inactiveLinks")}</SectionLabel>
{renderRows(inactive)}
</section>
)}
{tokenToRevoke && (
<ConfirmationDialog
open
onOpenChange={(open) => !open && setTokenToRevoke(null)}
onConfirm={async () => {
if (await deleteToken(tokenToRevoke.id)) setTokenToRevoke(null);
}}
title={t("token.revokeConfirm")}
description={t("token.revokeConfirmDescription", {
name: tokenToRevoke.name || t("token.unnamed"),
})}
confirmText={t("token.revoke")}
confirmVariant="destructive"
/>
)}
</>
);
}