-
Notifications
You must be signed in to change notification settings - Fork 2
v5: "Report a problem" nav button that opens the chat pre-seeded #25
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
Merged
Merged
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
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
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,80 @@ | ||
| "use client"; | ||
|
|
||
| import { | ||
| createContext, | ||
| useCallback, | ||
| useContext, | ||
| useMemo, | ||
| useState, | ||
| } from "react"; | ||
|
|
||
| /** | ||
| * A one-shot message to auto-send into the chat. The nonce lets the same text | ||
| * re-trigger a send on repeat clicks — a plain string wouldn't change, so the | ||
| * consumer's effect wouldn't fire again. | ||
| */ | ||
| interface ChatSeed { | ||
| text: string; | ||
| nonce: number; | ||
| } | ||
|
|
||
| interface ChatLauncher { | ||
| /** Whether the chat sheet is open. */ | ||
| isOpen: boolean; | ||
| /** A pending message to auto-send, or null. `ChatFab` consumes it. */ | ||
| pendingSeed: ChatSeed | null; | ||
| /** Open the chat. Pass `seedText` to also auto-send an opening message. */ | ||
| open: (seedText?: string) => void; | ||
| /** Close the chat (the conversation is preserved). */ | ||
| close: () => void; | ||
| /** Clear the pending seed once it has been sent. */ | ||
| consumeSeed: () => void; | ||
| } | ||
|
|
||
| const ChatLauncherContext = createContext<ChatLauncher | null>(null); | ||
|
|
||
| /** | ||
| * Owns just the chat launcher's open state and any one-shot seed message, so | ||
| * that entry points outside `ChatFab` (e.g. the nav "Report" button) can open | ||
| * and seed the chat. All conversation/message state stays inside `ChatFab`. | ||
| */ | ||
| export function ChatLauncherProvider({ | ||
| children, | ||
| }: { | ||
| children: React.ReactNode; | ||
| }) { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [pendingSeed, setPendingSeed] = useState<ChatSeed | null>(null); | ||
|
|
||
| const open = useCallback((seedText?: string) => { | ||
| setIsOpen(true); | ||
| if (seedText) { | ||
| setPendingSeed((prev) => ({ | ||
| text: seedText, | ||
| nonce: (prev?.nonce ?? 0) + 1, | ||
| })); | ||
| } | ||
| }, []); | ||
|
|
||
| const close = useCallback(() => setIsOpen(false), []); | ||
| const consumeSeed = useCallback(() => setPendingSeed(null), []); | ||
|
|
||
| const value = useMemo( | ||
| () => ({ isOpen, pendingSeed, open, close, consumeSeed }), | ||
| [isOpen, pendingSeed, open, close, consumeSeed] | ||
| ); | ||
|
|
||
| return ( | ||
| <ChatLauncherContext.Provider value={value}> | ||
| {children} | ||
| </ChatLauncherContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useChatLauncher(): ChatLauncher { | ||
| const ctx = useContext(ChatLauncherContext); | ||
| if (!ctx) { | ||
| throw new Error("useChatLauncher must be used within a ChatLauncherProvider"); | ||
| } | ||
| return ctx; | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After
ChatFabconsumes a seed,pendingSeedis set back tonull, so the nextopen(t("reportSeed"))call recomputes the nonce as1again. SinceChatFabkeepslastSeedNonce.currentat1, a user who clicks REPORT a second time after the first seeded turn has been sent will only open the sheet; the seed is skipped and no report prompt is sent. Keep a monotonic counter outside the consumed seed state (or otherwise make each seed unique after consumption).Useful? React with 👍 / 👎.