Skip to content
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

Simplify deck share #7

Merged
merged 3 commits into from
Nov 13, 2023
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
5 changes: 5 additions & 0 deletions functions/lib/short-unique-id/short-unique-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ShortUniqueId from "short-unique-id";

export const shortUniqueId = () => {
return new ShortUniqueId({ length: 10 }).rnd();
};
64 changes: 0 additions & 64 deletions functions/share-deck.ts

This file was deleted.

2 changes: 2 additions & 0 deletions functions/upsert-deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { deckSchema } from "./db/deck/decks-with-cards-schema.ts";
import { addDeckToMineDb } from "./db/deck/add-deck-to-mine-db.ts";
import { createForbiddenRequestResponse } from "./lib/json-response/create-forbidden-request-response.ts";
import { canEditDeck } from "./db/deck/can-edit-deck.ts";
import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts";

const requestSchema = z.object({
id: z.number().nullable().optional(),
Expand Down Expand Up @@ -53,6 +54,7 @@ export const onRequestPost = handleError(async ({ request, env }) => {
.upsert({
id: input.data.id ? input.data.id : undefined,
author_id: user.id,
share_id: input.data.id ? undefined : shortUniqueId(),
name: input.data.title,
description: input.data.description,
is_public: false,
Expand Down
12 changes: 0 additions & 12 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import {
UpsertDeckRequest,
UpsertDeckResponse,
} from "../../functions/upsert-deck.ts";
import {
ShareDeckRequest,
ShareDeckResponse,
} from "../../functions/share-deck.ts";
import { GetSharedDeckResponse } from "../../functions/get-shared-deck.ts";
import { AddCardRequest, AddCardResponse } from "../../functions/add-card.ts";

Expand Down Expand Up @@ -59,11 +55,3 @@ export const upsertDeckRequest = (body: UpsertDeckRequest) => {
export const addCardRequest = (body: AddCardRequest) => {
return request<AddCardResponse, AddCardRequest>("/add-card", "POST", body);
};

export const shareDeckRequest = (body: ShareDeckRequest) => {
return request<ShareDeckResponse, ShareDeckRequest>(
"/share-deck",
"POST",
body,
);
};
2 changes: 1 addition & 1 deletion src/screens/deck-review/deck-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const DeckPreview = observer(() => {
</div>

<div className={css({ display: "flex", gap: 16 })}>
<ShareDeckButton deckId={deck.id} defaultShareId={deck.share_id} />
<ShareDeckButton deckId={deck.id} shareId={deck.share_id} />
{deckListStore.myId && deck.author_id === deckListStore.myId ? (
<Button
icon={"mdi-pencil"}
Expand Down
40 changes: 10 additions & 30 deletions src/screens/deck-review/share-deck-button.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
import React, { useState } from "react";
import React from "react";
import { assert } from "../../lib/typescript/assert.ts";
import { trimEnd } from "../../lib/string/trim.ts";
import WebApp from "@twa-dev/sdk";
import { Button } from "../../ui/button.tsx";
import { shareDeckRequest } from "../../api/api.ts";
import { theme } from "../../ui/theme.tsx";
import { reportHandledError } from "../../lib/rollbar/rollbar.tsx";

type Props = {
deckId: number;
defaultShareId?: string | null;
shareId?: string | null;
};

export const ShareDeckButton = (props: Props) => {
const [isLoading, setIsLoading] = useState(false);
// Usually it's an antipattern, but since props.defaultShareId never gets updated, it's ok for now
const [shareId, setShareId] = useState(props.defaultShareId);
const { shareId } = props;

const onClick = async () => {
if (shareId) {
const botUrl = import.meta.env.VITE_BOT_APP_URL;
assert(botUrl, "Bot URL is not set");
const botUrlWithDeckId = `${trimEnd(botUrl, "/")}?startapp=${shareId}`;
const shareUrl = `https://t.me/share/url?text=&url=${botUrlWithDeckId}`;
WebApp.openTelegramLink(shareUrl);
} else {
setIsLoading(true);

try {
const result = await shareDeckRequest({
deckId: props.deckId,
});
setShareId(result.shareId);
} catch (e) {
reportHandledError("Error while sharing deck", e);
} finally {
setIsLoading(false);
}
}
const botUrl = import.meta.env.VITE_BOT_APP_URL;
assert(botUrl, "Bot URL is not set");
const botUrlWithDeckId = `${trimEnd(botUrl, "/")}?startapp=${shareId}`;
const shareUrl = `https://t.me/share/url?text=&url=${botUrlWithDeckId}`;
WebApp.openTelegramLink(shareUrl);
};

return (
<Button
icon={isLoading ? "mdi-loading mdi-spin" : "mdi-share"}
icon={"mdi-share"}
mainColor={theme.textColor}
disabled={isLoading}
onClick={onClick}
transparent
outline
>
{shareId ? "Share deck" : "Get share link"}
Share deck
</Button>
);
};