Skip to content
Merged
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
20 changes: 12 additions & 8 deletions components/bounty-detail/bounty-detail-submissions-card.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useState, useEffect, useRef } from "react";

Check warning on line 3 in components/bounty-detail/bounty-detail-submissions-card.tsx

View workflow job for this annotation

GitHub Actions / build-and-lint (24.x)

'useRef' is defined but never used
import { Loader2, DollarSign } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Expand Down Expand Up @@ -57,32 +57,35 @@
useState<BountySubmissionType | null>(null);

const [prUrl, setPrUrl] = useState("");
const [submitComments, setSubmitComments] = useState("");
const [reviewComments, setReviewComments] = useState("");
const [submitComments, setSubmitComments] = useState("");
const [reviewComments, setReviewComments] = useState("");
const reviewStatus = "APPROVED";
const [transactionHash, setTransactionHash] = useState("");

const submitToBounty = useSubmitToBounty();
const reviewSubmission = useReviewSubmission();
const markSubmissionPaid = useMarkSubmissionPaid();

const hasHydratedDraft = useRef(false);
const [isHydrated, setIsHydrated] = useState(false);

// Load draft on mount
useEffect(() => {
if (draft?.formData) {
setPrUrl(draft.formData.githubPullRequestUrl);
setSubmitComments(draft.formData.comments);
}
hasHydratedDraft.current = true;
setIsHydrated(true);
}, [draft]);

// Auto-save on form changes
useEffect(() => {
if (!hasHydratedDraft.current) return;
const cleanup = autoSave({ githubPullRequestUrl: prUrl, comments: submitComments });
if (!isHydrated) return;
const cleanup = autoSave({
githubPullRequestUrl: prUrl,
comments: submitComments,
});
return cleanup;
}, [prUrl, submitComments, autoSave]);
}, [prUrl, submitComments, autoSave, isHydrated]);

const isOrgMember =
(session?.user as ExtendedUser)?.organizations?.includes(
Expand Down Expand Up @@ -184,7 +187,8 @@
Submit your GitHub pull request URL.
{draft && (
<span className="block mt-1 text-xs text-blue-400">
Draft restored from {new Date(draft.updatedAt).toLocaleString()}
Draft restored from{" "}
{new Date(draft.updatedAt).toLocaleString()}
</span>
)}
</DialogDescription>
Expand Down
28 changes: 16 additions & 12 deletions docs/SUBMISSION_DRAFTS_EXAMPLE.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Submission Draft System - Quick Start Example
*
*
* This example shows how to integrate the submission draft system
* into any form component.
*/
Expand All @@ -10,27 +10,31 @@ import { useSubmissionDraft } from "@/hooks/use-submission-draft";

export function SubmissionFormExample({ bountyId }: { bountyId: string }) {
const { draft, clearDraft, autoSave } = useSubmissionDraft(bountyId);

const [prUrl, setPrUrl] = useState(draft?.formData.githubPullRequestUrl || "");

const [prUrl, setPrUrl] = useState(
draft?.formData.githubPullRequestUrl || "",
);
const [comments, setComments] = useState(draft?.formData.comments || "");

// 2. Auto-save when form changes
useEffect(() => {
const cleanup = autoSave({
githubPullRequestUrl: prUrl,
comments
const cleanup = autoSave({
githubPullRequestUrl: prUrl,
comments,
});
return cleanup;
}, [prUrl, comments, autoSave]);

// 3. Clear draft on successful submit
const handleSubmit = async () => {
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();

// Your submit logic here
await submitToAPI({ prUrl, comments });

// Clear draft after success
clearDraft();

// Reset form
setPrUrl("");
setComments("");
Expand All @@ -44,19 +48,19 @@ export function SubmissionFormExample({ bountyId }: { bountyId: string }) {
Draft saved at {new Date(draft.updatedAt).toLocaleString()}
</div>
)}

<input
value={prUrl}
onChange={(e) => setPrUrl(e.target.value)}
placeholder="Pull Request URL"
/>

<textarea
value={comments}
onChange={(e) => setComments(e.target.value)}
placeholder="Comments"
/>

<button type="submit">Submit</button>
</form>
);
Expand Down
15 changes: 8 additions & 7 deletions hooks/__tests__/use-submission-draft.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, act, waitFor } from "@testing-library/react";

Check warning on line 1 in hooks/__tests__/use-submission-draft.test.ts

View workflow job for this annotation

GitHub Actions / build-and-lint (24.x)

'waitFor' is defined but never used
import { useSubmissionDraft } from "../use-submission-draft";

describe("useSubmissionDraft", () => {
Expand Down Expand Up @@ -49,7 +49,8 @@
expect(result.current.draft).toBeNull();
});

it("should auto-save after delay", async () => {
it("should auto-save after delay", () => {
jest.useFakeTimers();
const { result } = renderHook(() => useSubmissionDraft(bountyId));
const formData = {
githubPullRequestUrl: "https://github.com/test/pr/1",
Expand All @@ -60,12 +61,12 @@
result.current.autoSave(formData);
});

await waitFor(
() => {
expect(result.current.draft?.formData).toEqual(formData);
},
{ timeout: 1500 }
);
act(() => {
jest.advanceTimersByTime(1000); // AUTO_SAVE_DELAY
});

expect(result.current.draft?.formData).toEqual(formData);
jest.useRealTimers();
});

it("should persist draft across hook instances", () => {
Expand Down
13 changes: 7 additions & 6 deletions hooks/use-submission-draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const AUTO_SAVE_DELAY = 1000;

export function useSubmissionDraft(bountyId: string) {
const draftKey = `${DRAFT_KEY_PREFIX}${bountyId}`;
const [draft, setDraft] = useLocalStorage<SubmissionDraft | null>(draftKey, null);
const [draft, setDraft] = useLocalStorage<SubmissionDraft | null>(
draftKey,
null,
);

const saveDraft = useCallback(
(formData: SubmissionForm) => {
Expand All @@ -19,7 +22,7 @@ export function useSubmissionDraft(bountyId: string) {
};
setDraft(newDraft);
},
[bountyId, setDraft]
[bountyId, setDraft],
);

const clearDraft = useCallback(() => {
Expand All @@ -29,13 +32,11 @@ export function useSubmissionDraft(bountyId: string) {
const autoSave = useCallback(
(formData: SubmissionForm) => {
const timer = setTimeout(() => {
if (formData.githubPullRequestUrl || formData.comments) {
saveDraft(formData);
}
saveDraft(formData);
}, AUTO_SAVE_DELAY);
return () => clearTimeout(timer);
},
[saveDraft]
[saveDraft],
);

return {
Expand Down
Loading