Repository navigation
feat(web): add dismissible in-app GitHub star nudge after successful sends #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Siddarth474
wants to merge
2
commits into
usesend:main
Choose a base branch
from
Siddarth474:feat/github-star-nudge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { toast } from "@usesend/ui/src/toaster"; | ||
|
|
||
| export const STAR_NUDGE_STORAGE_KEY = "usesend:star-nudge-dismissed"; | ||
| const STAR_NUDGE_TOAST_ID = "usesend-star-nudge"; | ||
| export const FEEDBACK_ACTIVE_SESSION_KEY = "usesend:feedback-active"; | ||
| export const GITHUB_REPO_URL = "https://github.com/usesend/usesend"; | ||
|
|
||
| export function isStarNudgeDismissed(): boolean { | ||
| if (typeof window === "undefined") return true; | ||
| try { | ||
| return localStorage.getItem(STAR_NUDGE_STORAGE_KEY) === "true"; | ||
| } catch { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| export function dismissStarNudge(): void { | ||
| if (typeof window === "undefined") return; | ||
| try { | ||
| localStorage.setItem(STAR_NUDGE_STORAGE_KEY, "true"); | ||
| } catch { | ||
| // Graceful fallback for restricted storage environments | ||
| } | ||
| } | ||
|
|
||
| export function isFeedbackActive(): boolean { | ||
| if (typeof window === "undefined") return false; | ||
| try { | ||
| return sessionStorage.getItem(FEEDBACK_ACTIVE_SESSION_KEY) === "true"; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function triggerStarNudge(delayMs = 1500): void { | ||
| if (isStarNudgeDismissed() || isFeedbackActive()) return; | ||
|
|
||
| window.setTimeout(() => { | ||
| if (isStarNudgeDismissed() || isFeedbackActive()) return; | ||
|
|
||
| toast("Enjoying useSend?", { | ||
| id: STAR_NUDGE_TOAST_ID, | ||
| description: | ||
| "It's open source — a star on GitHub helps others find it! ⭐", | ||
| duration: 10000, | ||
| action: { | ||
| label: "Star on GitHub", | ||
| onClick: () => { | ||
| dismissStarNudge(); | ||
| window.open(GITHUB_REPO_URL, "_blank", "noopener,noreferrer"); | ||
| }, | ||
| }, | ||
| cancel: { | ||
| label: "Not now", | ||
| onClick: () => { | ||
| dismissStarNudge(); | ||
| }, | ||
| }, | ||
| onDismiss: () => { | ||
| dismissStarNudge(); | ||
| }, | ||
| onAutoClose: () => { | ||
| dismissStarNudge(); | ||
| }, | ||
| }); | ||
| }, delayMs); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"; | ||
| import { | ||
| isStarNudgeDismissed, | ||
| dismissStarNudge, | ||
| isFeedbackActive, | ||
| triggerStarNudge, | ||
| STAR_NUDGE_STORAGE_KEY, | ||
| FEEDBACK_ACTIVE_SESSION_KEY, | ||
| GITHUB_REPO_URL, | ||
| } from "./star-nudge"; | ||
| import { toast } from "@usesend/ui/src/toaster"; | ||
|
|
||
| vi.mock("@usesend/ui/src/toaster", () => ({ | ||
| toast: vi.fn(), | ||
| })); | ||
|
|
||
| describe("star-nudge utility", () => { | ||
| let localStorageMock: Record<string, string> = {}; | ||
| let sessionStorageMock: Record<string, string> = {}; | ||
| let windowOpenSpy = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| localStorageMock = {}; | ||
| sessionStorageMock = {}; | ||
| windowOpenSpy = vi.fn(); | ||
|
|
||
| vi.useFakeTimers(); | ||
|
|
||
| vi.stubGlobal("localStorage", { | ||
| getItem: vi.fn((key: string) => localStorageMock[key] ?? null), | ||
| setItem: vi.fn((key: string, value: string) => { | ||
| localStorageMock[key] = value; | ||
| }), | ||
| removeItem: vi.fn((key: string) => { | ||
| delete localStorageMock[key]; | ||
| }), | ||
| }); | ||
|
|
||
| vi.stubGlobal("sessionStorage", { | ||
| getItem: vi.fn((key: string) => sessionStorageMock[key] ?? null), | ||
| setItem: vi.fn((key: string, value: string) => { | ||
| sessionStorageMock[key] = value; | ||
| }), | ||
| removeItem: vi.fn((key: string) => { | ||
| delete sessionStorageMock[key]; | ||
| }), | ||
| }); | ||
|
|
||
| vi.stubGlobal("window", { | ||
| open: windowOpenSpy, | ||
| setTimeout: (fn: () => void, ms?: number) => setTimeout(fn, ms), | ||
| clearTimeout: (id: any) => clearTimeout(id), | ||
| }); | ||
|
|
||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it("returns false for isStarNudgeDismissed when storage is empty", () => { | ||
| expect(isStarNudgeDismissed()).toBe(false); | ||
| }); | ||
|
|
||
| it("returns true for isStarNudgeDismissed when already marked dismissed", () => { | ||
| localStorageMock[STAR_NUDGE_STORAGE_KEY] = "true"; | ||
| expect(isStarNudgeDismissed()).toBe(true); | ||
| }); | ||
|
|
||
| it("dismissStarNudge writes true to localStorage", () => { | ||
| dismissStarNudge(); | ||
| expect(localStorageMock[STAR_NUDGE_STORAGE_KEY]).toBe("true"); | ||
| expect(isStarNudgeDismissed()).toBe(true); | ||
| }); | ||
|
|
||
| it("triggerStarNudge does not trigger toast if already dismissed", () => { | ||
| localStorageMock[STAR_NUDGE_STORAGE_KEY] = "true"; | ||
|
|
||
| triggerStarNudge(500); | ||
| vi.advanceTimersByTime(600); | ||
|
|
||
| expect(toast).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("triggerStarNudge does not trigger toast if feedback is active in session", () => { | ||
| sessionStorageMock[FEEDBACK_ACTIVE_SESSION_KEY] = "true"; | ||
|
|
||
| triggerStarNudge(500); | ||
| vi.advanceTimersByTime(600); | ||
|
|
||
| expect(toast).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("isFeedbackActive returns false by default and true when flagged", () => { | ||
| expect(isFeedbackActive()).toBe(false); | ||
| sessionStorageMock[FEEDBACK_ACTIVE_SESSION_KEY] = "true"; | ||
| expect(isFeedbackActive()).toBe(true); | ||
| }); | ||
|
|
||
| it("triggerStarNudge does not trigger toast if feedback opens during the delay", () => { | ||
| triggerStarNudge(1000); | ||
|
|
||
| // Feedback opens at 400ms, while timer is pending | ||
| vi.advanceTimersByTime(400); | ||
| sessionStorageMock[FEEDBACK_ACTIVE_SESSION_KEY] = "true"; | ||
|
|
||
| // Advance past the remaining time | ||
| vi.advanceTimersByTime(700); | ||
|
|
||
| expect(toast).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("triggerStarNudge shows toast after specified delay when eligible", () => { | ||
| triggerStarNudge(1000); | ||
| expect(toast).not.toHaveBeenCalled(); | ||
|
|
||
| vi.advanceTimersByTime(1000); | ||
| expect(toast).toHaveBeenCalledTimes(1); | ||
|
|
||
| const toastCall = (toast as any).mock.calls[0]; | ||
| expect(toastCall[0]).toBe("Enjoying useSend?"); | ||
| expect(toastCall[1]).toMatchObject({ | ||
| action: expect.objectContaining({ label: "Star on GitHub" }), | ||
| cancel: expect.objectContaining({ label: "Not now" }), | ||
| }); | ||
|
|
||
| // Test clicking Star on GitHub action | ||
| toastCall[1].action.onClick(); | ||
| expect(windowOpenSpy).toHaveBeenCalledWith( | ||
| GITHUB_REPO_URL, | ||
| "_blank", | ||
| "noopener,noreferrer" | ||
| ); | ||
| expect(localStorageMock[STAR_NUDGE_STORAGE_KEY]).toBe("true"); | ||
| }); | ||
|
|
||
| it("clicking cancel in toast marks nudge dismissed", () => { | ||
| triggerStarNudge(500); | ||
| vi.advanceTimersByTime(500); | ||
|
|
||
| const toastCall = (toast as any).mock.calls[0]; | ||
| toastCall[1].cancel.onClick(); | ||
| expect(localStorageMock[STAR_NUDGE_STORAGE_KEY]).toBe("true"); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.