-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
refactor(modal): solid modal for save last signed out result (@fehmer) #8183
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
fehmer
wants to merge
2
commits into
master
Choose a base branch
from
feature/migrate-modals
base: master
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.
+210
−280
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
201 changes: 201 additions & 0 deletions
201
frontend/src/ts/components/modals/LastSignedOutResultModal.tsx
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,201 @@ | ||
| import { CompletedEvent } from "@monkeytype/schemas/results"; | ||
| import { Mode } from "@monkeytype/schemas/shared"; | ||
| import objectHash from "object-hash"; | ||
| import { createMemo, JSX } from "solid-js"; | ||
|
|
||
| import Ape from "../../ape"; | ||
| import { getConfig } from "../../config/store"; | ||
| import { SnapshotResult } from "../../constants/default-snapshot"; | ||
| import { saveLocalResult, SaveLocalResultData } from "../../db"; | ||
| import { authEvent } from "../../events/auth"; | ||
| import { getAuthenticatedUser } from "../../firebase"; | ||
| import { hideModal, showModal } from "../../states/modals"; | ||
| import { | ||
| showErrorNotification, | ||
| showNoticeNotification, | ||
| showSuccessNotification, | ||
| } from "../../states/notifications"; | ||
| import { | ||
| getLastSignedOutResult, | ||
| setLastSignedOutResult, | ||
| } from "../../states/test"; | ||
| import { cn } from "../../utils/cn"; | ||
| import { Formatting } from "../../utils/format"; | ||
| import { AnimatedModal } from "../common/AnimatedModal"; | ||
| import { Button } from "../common/Button"; | ||
| import { Separator } from "../common/Separator"; | ||
|
|
||
| const modalId = "LastSignedOutResult"; | ||
|
|
||
| export function LastSignedOutResultModal() { | ||
| const format = createMemo( | ||
| () => | ||
| new Formatting({ | ||
| alwaysShowDecimalPlaces: getConfig.alwaysShowDecimalPlaces, | ||
| typingSpeedUnit: getConfig.typingSpeedUnit, | ||
| }), | ||
| ); | ||
|
|
||
| return ( | ||
| <AnimatedModal | ||
| id={modalId} | ||
| title="Last signed out result" | ||
| modalClass="max-w-2xl" | ||
| > | ||
| <p class="">Would you like to save it?</p> | ||
| <Separator /> | ||
|
|
||
| <div class="grid grid-cols-2 gap-4"> | ||
| <Value | ||
| class="text-xl" | ||
| label={format().typingSpeedUnit} | ||
| value={format().typingSpeed(getLastSignedOutResult()?.wpm ?? 0)} | ||
| /> | ||
| <Value | ||
| class="text-xl" | ||
| label="accuracy" | ||
| value={format().accuracy(getLastSignedOutResult()?.acc ?? 0)} | ||
| /> | ||
| <Value | ||
| label="raw" | ||
| value={format().typingSpeed(getLastSignedOutResult()?.rawWpm ?? 0)} | ||
| /> | ||
| <Value | ||
| label="consistency" | ||
| value={format().percentage( | ||
| getLastSignedOutResult()?.consistency ?? 0, | ||
| )} | ||
| /> | ||
| <Value | ||
| class="col-span-2" | ||
| label="characters" | ||
| value={getLastSignedOutResult()?.charStats.join("/") ?? "-"} | ||
| /> | ||
| <Value | ||
| label="test type" | ||
| class="col-span-2" | ||
| value={formatTestType(getLastSignedOutResult())} | ||
| /> | ||
|
|
||
| <Button | ||
| text="discard" | ||
| onClick={() => { | ||
| setLastSignedOutResult(null); | ||
| showNoticeNotification("Last test result discarded"); | ||
| hideModal(modalId); | ||
| }} | ||
| /> | ||
| <Button | ||
| text="save" | ||
| onClick={() => { | ||
| void syncLastSignedOutResult(); | ||
| }} | ||
| /> | ||
| </div> | ||
| </AnimatedModal> | ||
| ); | ||
| } | ||
|
|
||
| function Value(props: { | ||
| label: string; | ||
| value: string | (string | JSX.Element)[]; | ||
| class?: string; | ||
| }) { | ||
| return ( | ||
| <div class={cn("flex flex-col text-sm", props.class)}> | ||
| <span class="text-[0.75em] text-sub">{props.label}</span> | ||
| <span>{props.value}</span> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function formatTestType(r: CompletedEvent | null): (string | JSX.Element)[] { | ||
| if (r === null) return ["-"]; | ||
| const tt: (string | JSX.Element)[] = [`${r.mode} ${r.mode2}`]; | ||
|
|
||
| tt.push(<br />, `${r.language}`); | ||
|
|
||
| if (r.numbers) tt.push(<br />, "numbers"); | ||
| if (r.punctuation) tt.push(<br />, "punctuation"); | ||
| if (r.blindMode) tt.push(<br />, "blind"); | ||
| if (r.lazyMode) tt.push(<br />, "lazy"); | ||
| if (r.funbox.length > 0) { | ||
| const funboxLabel = r.funbox.map((it) => it.replace(/_/g, " ")).join(","); | ||
| tt.push(<br />, funboxLabel); | ||
| } | ||
| if (r.difficulty !== "normal") tt.push(<br />, `${r.difficulty}`); | ||
| if (r.tags.length > 0) tt.push(<br />, `${r.tags.length} tags`); | ||
| return tt; | ||
| } | ||
|
|
||
| async function syncLastSignedOutResult(): Promise<void> { | ||
| const user = getAuthenticatedUser(); | ||
| const lastResult = getLastSignedOutResult(); | ||
| if (user === null) { | ||
| showNoticeNotification( | ||
| "Failed to save last test result: user not authenticated", | ||
| ); | ||
| hideModal(modalId); | ||
| return; | ||
| } | ||
| if (lastResult === null) { | ||
| showNoticeNotification("Failed to save last test result: no last result"); | ||
| hideModal(modalId); | ||
| return; | ||
| } | ||
|
|
||
| const updatedResult = updateUidAndHash(user.uid, lastResult); | ||
| const response = await Ape.results.add({ body: { result: updatedResult } }); | ||
|
|
||
| if (response.status !== 200) { | ||
| showErrorNotification(`Failed to save last result`, { | ||
| response, | ||
| }); | ||
| hideModal(modalId); | ||
| return; | ||
| } | ||
|
|
||
| //TODO - this type cast was not needed before because we were using JSON cloning | ||
| // but now with the stronger types it shows that we are forcing completed event | ||
| // into a snapshot result - might not cause issues but worth investigating | ||
| const result = structuredClone( | ||
| updatedResult, | ||
| ) as unknown as SnapshotResult<Mode>; | ||
|
|
||
| const dataToSave: SaveLocalResultData = { | ||
| xp: response.body.data.xp, | ||
| streak: response.body.data.streak, | ||
| result, | ||
| isPb: response.body.data.isPb, | ||
| }; | ||
|
|
||
| result._id = response.body.data.insertedId; | ||
| if (response.body.data.isPb) { | ||
| result.isPb = true; | ||
| } | ||
| saveLocalResult(dataToSave); | ||
| setLastSignedOutResult(null); | ||
| showSuccessNotification( | ||
| `Last test result saved ${response.body.data.isPb ? `(new pb!)` : ""}`, | ||
| ); | ||
| hideModal(modalId); | ||
| } | ||
|
|
||
| export function updateUidAndHash( | ||
| uid: string, | ||
| notSignedInLastResult: CompletedEvent, | ||
| ): CompletedEvent { | ||
| notSignedInLastResult.uid = uid; | ||
| //@ts-expect-error really need to delete this | ||
| delete notSignedInLastResult.hash; | ||
| notSignedInLastResult.hash = objectHash(notSignedInLastResult); | ||
| return notSignedInLastResult; | ||
| } | ||
|
|
||
| authEvent.subscribe((event) => { | ||
| if (event.type === "snapshotUpdated" && event.data.isInitial) { | ||
| if (getLastSignedOutResult() !== null) { | ||
| showModal(modalId); | ||
| } | ||
| } | ||
| }); | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.