Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

3.2 KB
Raw Blame
'use client';

import { useState } from 'react';
import { useTranslations } from 'next-intl';
import type { InviteRow as Invite } from '@/lib/api/endpoints/invites';
import ConfirmDialog from '@/components/common/overlay/ConfirmDialog';
import { ItemGroup } from '@/components/ui/item';
import {
  useDeleteInvite,
  useInvitesQuery,
  useRenewInvite,
  useSendInviteEmail,
} from '@/services/members.service';
import { usePermissions } from '@/hooks/usePermissions';
import { useShell } from '@/context/shellContext';
import { mayRenewInvite } from '@/utils/invites';
import InviteRow from './InviteRow';

// Invite panel shown above the members list: the invites that have not been accepted
// or rejected yet, each with a revoke action. Sending one is the page's own action.
// Mirrors the memberAdmin guard on the API — the members_invite matrix, or the
// standing of an owner or manager of the team — and renders nothing without it.
export default function InvitesManager({ projectKey }: { projectKey: string }) {
  const t = useTranslations('members.invites');
  const { can, isAdmin, isOwner } = usePermissions();
  const viewerTeamRole = useShell().project?.viewer?.teamRole ?? null;
  const canRead = can('members_invite', 'read') || isAdmin;
  const canCreate = can('members_invite', 'create') || isAdmin;
  const canDelete = can('members_invite', 'delete') || isAdmin;
  const invitesQuery = useInvitesQuery(projectKey, canRead);
  const deleteInvite = useDeleteInvite(projectKey);
  const sendEmail = useSendInviteEmail(projectKey);
  const renew = useRenewInvite(projectKey);
  const [target, setTarget] = useState<Invite | null>(null);

  function resendInvite(invite: Invite) {
    sendEmail.mutate(invite.id);
  }

  if (!canRead) return null;

  const pending = (invitesQuery.data ?? []).filter((invite) => invite.status === 'pending');
  if (pending.length === 0) return null;

  return (
    <div>
      <div className="mb-8">
        <div className="mb-1 border-b pb-1 text-xs font-medium text-muted-foreground">
          {t('pendingCount', { count: pending.length })}
        </div>
        <ItemGroup>
          {pending.map((invite) => (
            <InviteRow
              key={invite.id}
              invite={invite}
              onResend={canCreate ? resendInvite : undefined}
              onRenew={
                canCreate &&
                mayRenewInvite(invite, { teamRole: viewerTeamRole, ownsProject: isOwner })
                  ? (expired) => renew.mutate(expired.id)
                  : undefined
              }
              renewing={renew.isPending && renew.variables === invite.id}
              onRevoke={canDelete ? setTarget : undefined}
              resending={sendEmail.isPending && sendEmail.variables === invite.id}
            />
          ))}
        </ItemGroup>
      </div>

      {canDelete && target && (
        <ConfirmDialog
          title={t('revokeTitle', { email: target.email })}
          confirmLabel={t('revokeConfirm')}
          onConfirm={async () => {
            await deleteInvite.mutateAsync(target.id);
            setTarget(null);
          }}
          onClose={() => setTarget(null)}
        >
          <div className="text-sm text-muted-foreground">{t('revokeDescription')}</div>
        </ConfirmDialog>
      )}
    </div>
  );
}