Skip to content

Commit

Permalink
feat: share session
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanlundberg committed Sep 15, 2024
1 parent ad87427 commit 5b10166
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 25 deletions.
8 changes: 7 additions & 1 deletion messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@
"descending": "Descending",
"share": "Share",
"last": "Last",
"all": "All"
"all": "All",

"share-clipboard": {
"header": "Generated by NexusTimer",
"average": "Average",
"list-of-times": "List of times"
}
},
"StatsPage": {
"title": "Stats",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import {
DropdownMenuPortal,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { shareSolves } from "@/lib/shareSolves";
import { createShareMessage } from "@/lib/createShareMessage";
import { useSolveFiltersStore } from "@/store/SolvesFilters";
import { useTimerStore } from "@/store/timerStore";
import {
Expand All @@ -21,34 +20,37 @@ import {
DragHandleVerticalIcon,
Share1Icon,
} from "@radix-ui/react-icons";
import { useTranslations } from "next-intl";
import { DateTime } from "luxon";
import { useLocale, useTranslations } from "next-intl";
import { toast } from "sonner";

export default function DropdownFilterSolves() {
const t = useTranslations("Index");
const { sortType, order, handleChangeOrder, handleChangeSortType, tab } =
useSolveFiltersStore();
const { selectedCube } = useTimerStore();

const handleShare = () => {
const locale = useLocale();
const date = DateTime.now().setLocale(locale).toLocaleString();
const handleShare = (type: "All" | "3" | "5" | "12" | "50" | "100") => {
if (selectedCube) {
const {
formattedAo5,
formattedAo12,
formattedLast5Solves,
formattedLast12Solves,
} = shareSolves({
solves:
tab === "session"
? selectedCube.solves.session
: selectedCube.solves.all,
const tempSolves =
tab === "all" ? selectedCube.solves.all : selectedCube.solves.session;

const message = createShareMessage({
type,
solves: tempSolves,
translations: {
statsTitle: t("SolvesPage.share-clipboard.header"),
avg: t("SolvesPage.share-clipboard.average"),
listOfTimes: t("SolvesPage.share-clipboard.list-of-times"),
date: date,
},
});
console.log(
formattedAo5,
formattedAo12,
formattedLast5Solves,
formattedLast12Solves
);

if ("clipboard" in navigator) {
navigator.clipboard.writeText(message);
}

toast(t("SolvesPage.toast.success-copy"), {
description: t("SolvesPage.toast.success-copy-description"),
});
Expand Down Expand Up @@ -132,13 +134,13 @@ export default function DropdownFilterSolves() {
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent>
<DropdownMenuItem onClick={handleShare}>
<DropdownMenuItem onClick={() => handleShare("5")}>
{t("SolvesPage.last")} Ao5
</DropdownMenuItem>
<DropdownMenuItem onClick={handleShare}>
<DropdownMenuItem onClick={() => handleShare("12")}>
{t("SolvesPage.last")} Ao12
</DropdownMenuItem>
<DropdownMenuItem onClick={handleShare}>
<DropdownMenuItem onClick={() => handleShare("All")}>
{t("SolvesPage.all")}
</DropdownMenuItem>
</DropdownMenuSubContent>
Expand Down
74 changes: 74 additions & 0 deletions src/lib/createShareMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Solve } from "@/interfaces/Solve";
import getMean from "./getMean";
import getWorstTime from "./getWorstTime";
import getBestTime from "./getBestTime";
import getDeviation from "./getDeviation";
import formatTime from "./formatTime";

interface createShareMessage {
type: "All" | "3" | "5" | "12" | "50" | "100";
solves: Solve[] | null;
translations: {
statsTitle: string;
avg: string;
listOfTimes: string;
date: string;
};
}

export function createShareMessage({
type,
solves,
translations: { statsTitle, avg, listOfTimes, date },
}: createShareMessage) {
if (!solves) return "";

let dataSet: Solve[] = [];
if (type === "All") {
dataSet = [...solves];
} else {
dataSet = solves.slice(0, parseInt(type));
}

const average = getMean(dataSet);
const worstTime = getWorstTime(dataSet);
const bestTime = getBestTime({ solves: dataSet });
const deviation = getDeviation(dataSet);

// Header
let content = `${statsTitle}: ${date} `;

// summary
content += `\n${avg} ${dataSet.length}: ${formatTime(
average
)} (σ = ${formatTime(deviation)})`;

// Space row
content += `\n \n`;

// subtitle
content += listOfTimes;

// Space row
content += `\n \n`;

// formatSolves

let formattedDataSet = "";

dataSet.sort((a, b) => a.endTime - b.endTime);

for (let i = 0; i < dataSet.length; i++) {
const parenthesis =
worstTime === dataSet[i].time || bestTime === dataSet[i].time;

formattedDataSet += `${i + 1}. ${parenthesis ? "(" : ""}${formatTime(
dataSet[i].time
)}${dataSet[i].plus2 ? "+" : ""}${parenthesis ? ")" : ""} ${
dataSet[i].scramble
}\n`;
}

// attach dataset
return (content += formattedDataSet);
}

0 comments on commit 5b10166

Please sign in to comment.