"use client";
import { useState } from "react";
import { IconExternalLink, IconTrash } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { revokeShare, type Share } from "@/lib/api/domains/share-api";
type Props = {
shares: Share[];
onRevoked: () => void;
};
/**
* Renders the list of active (non-revoked) shares with a per-row revoke
* button. Revoke is optimistic: the API call fires; on success we call
* onRevoked() so the parent can refresh from the source of truth.
*/
export function ShareList({ shares, onRevoked }: Props) {
const active = shares.filter((s) => !s.revoked_at);
if (active.length === 0) return null;
return (
Active shares for this session
{active.map((share) => (
))}
);
}
function ShareRow({ share, onRevoked }: { share: Share; onRevoked: () => void }) {
const [busy, setBusy] = useState(false);
const [err, setErr] = useState(null);
const handleRevoke = async () => {
setBusy(true);
setErr(null);
try {
await revokeShare(share.id);
onRevoked();
} catch (e) {
setErr(e instanceof Error ? e.message : "Failed to revoke share.");
} finally {
// Always clear busy — on success the row usually unmounts via the
// parent's refresh, but if it stays rendered (e.g. error path or a
// not-yet-reflected list) we don't want it stuck in "Revoking…".
setBusy(false);
}
};
return (
{err && {err}}
);
}