Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/app/(app)/maintainer/invite-contributor-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { useState, useTransition } from 'react';
import { useRouter } from 'next/navigation';
import { sendInvite } from '@/app/actions/maintainer';
import { sendInvite, getMyGithubHandle } from '@/app/actions/maintainer';
import { Link, Check } from 'lucide-react';

export default function InviteContributorButton({
installationId,
Expand All @@ -15,6 +16,8 @@ export default function InviteContributorButton({
const [email, setEmail] = useState('');
const [isPending, startTransition] = useTransition();
const [error, setError] = useState<string | null>(null);
const [linkCopied, setLinkCopied] = useState(false);
const [generatingLink, setGeneratingLink] = useState(false);
const router = useRouter();

async function handleSubmit(e: React.FormEvent) {
Expand All @@ -37,6 +40,30 @@ export default function InviteContributorButton({
});
}

async function handleCopyLink() {
setGeneratingLink(true);
const res = await getMyGithubHandle();
if (!res.ok) {
setError(res.error.message || 'Failed to generate invite link');
setGeneratingLink(false);
return;
}
const link = `${process.env.NEXT_PUBLIC_APP_URL || 'https://mergeship.dev'}/invite?ref=${res.data}`;

try {
await navigator.clipboard.writeText(link);
setLinkCopied(true);
} catch {
setError('Failed to copy to clipboard. Please try again.');
} finally {
setGeneratingLink(false);
setTimeout(() => {
setLinkCopied(false);
setError(null);
}, 2000);
}
}

return (
<>
<button
Expand Down Expand Up @@ -77,12 +104,33 @@ export default function InviteContributorButton({
{error && <p className="text-sm text-red-400">{error}</p>}
</form>

<div className="mt-5 flex items-center gap-3">
<span className="h-px flex-1 bg-zinc-800" />
<span className="text-xs text-zinc-600">or</span>
<span className="h-px flex-1 bg-zinc-800" />
</div>

<button
type="button"
onClick={handleCopyLink}
disabled={generatingLink}
className="mt-4 flex w-full items-center justify-center gap-2 rounded-lg border border-zinc-700 px-3 py-2 text-sm text-zinc-300 hover:border-zinc-600 disabled:opacity-50"
>
{linkCopied ? (
<Check className="h-4 w-4 text-emerald-500" />
) : (
<Link className="h-4 w-4" />
)}
{linkCopied ? 'Link copied!' : generatingLink ? 'Generating...' : 'Copy invite link'}
</button>

<button
type="button"
onClick={() => {
setOpen(false);
setError(null);
setEmail('');
setLinkCopied(false);
}}
className="mt-5 text-sm text-zinc-500 hover:text-zinc-300"
>
Expand Down
8 changes: 7 additions & 1 deletion src/app/actions/maintainer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ export {
} from './analytics';

export { getFlaggedAccounts, resolveFlaggedAccount } from './flagged-accounts';
export * from './invites';
export {
listPendingInvites,
sendInvite,
resendInvite,
getMyGithubHandle,
type InviteRow,
} from './invites';

export {
getFailedWebhookEvents,
Expand Down
17 changes: 17 additions & 0 deletions src/app/actions/maintainer/invites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ export async function sendInvite(
});
}

export async function getMyGithubHandle(): Promise<Result<string>> {
const authRes = await requireMaintainer({
rateLimit: { namespace: 'maint:my-handle', ...RATE_LIMIT_TIERS.GENEROUS },
});
if (!authRes.ok) return authRes;
const { user } = authRes.data;

const db = getDb();
const [profile] = await db
.select({ githubHandle: profiles.githubHandle })
.from(profiles)
.where(eq(profiles.id, user.id));

if (!profile) return err('not_found', 'Profile not found');
return ok(profile.githubHandle);
}

export async function resendInvite(inviteId: string): Promise<Result<void>> {
const authRes = await requireMaintainer({
rateLimit: { namespace: 'maint:resend-invite', ...RATE_LIMIT_TIERS.STANDARD },
Expand Down