You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
memos/web/src/components/ShareMemoDialog.tsx

146 lines
5.0 KiB
TypeScript

import { Button } from "@mui/joy";
import copy from "copy-to-clipboard";
import React, { useEffect, useRef } from "react";
import { toast } from "react-hot-toast";
import { getDateTimeString } from "@/helpers/datetime";
import useLoading from "@/hooks/useLoading";
import toImage from "@/labs/html2image";
import { useUserV1Store, extractUsernameFromName } from "@/store/v1";
import { useTranslate } from "@/utils/i18n";
import { generateDialog } from "./Dialog";
import showEmbedMemoDialog from "./EmbedMemoDialog";
import Icon from "./Icon";
import MemoContentV1 from "./MemoContentV1";
import MemoResourceListView from "./MemoResourceListView";
import UserAvatar from "./UserAvatar";
import "@/less/share-memo-dialog.less";
4 years ago
interface Props extends DialogProps {
memo: Memo;
4 years ago
}
const ShareMemoDialog: React.FC<Props> = (props: Props) => {
4 years ago
const { memo: propsMemo, destroy } = props;
const t = useTranslate();
const userV1Store = useUserV1Store();
const downloadingImageState = useLoading(false);
const loadingState = useLoading();
const memoElRef = useRef<HTMLDivElement>(null);
const memo = {
4 years ago
...propsMemo,
displayTsStr: getDateTimeString(propsMemo.displayTs),
4 years ago
};
const user = userV1Store.getUserByUsername(memo.creatorUsername);
4 years ago
useEffect(() => {
(async () => {
await userV1Store.getOrFetchUserByUsername(memo.creatorUsername);
loadingState.setFinish();
})();
}, []);
const handleCloseBtnClick = () => {
destroy();
};
const handleDownloadImageBtnClick = () => {
if (!memoElRef.current) {
return;
}
downloadingImageState.setLoading();
toImage(memoElRef.current, {
pixelRatio: window.devicePixelRatio * 2,
})
.then((url) => {
const a = document.createElement("a");
a.href = url;
feat: improve i18n support as a whole (#1526) * feat: improve i18n support as a whole - Remove dayjs in favor of /helpers/datetime.ts, which uses Intl.DateTimeFormat and Date. Dayjs is not exactly i18n friendly and has several locale related opened issues. - Move/refactor date/time code from /helpers/utils.ts to /helpers/datetime.ts. - Fix Daily Review weekday not changing according to selected date. - Localize Daily review weekday and month. - Load i18n listed strings from /locales/{locale}.json in a dynamic way. This makes much easier to add new locales, by just adding a properly named json file and listing it only in /web/src/i18n.ts and /api/user_setting.go. - Fallback languages are now set in /web/src/i18n.ts. - Full language codes are now preffered, but they fallback to 2-letter codes when not available. - The locale dropdown is now populated dynamically from the available locales. Locale names are populated by the browser via Intl.DisplayNames(locale). - /web/src/i18n.ts now exports a type TLocale from availableLocales array. This is used only by findNearestLanguageMatch(). As I was unable to use this type in ".d.ts" files, I switched the Locale type from /web/src/types/i18n.d.ts to string. - Move pretty much all hardcoded text strings to i18n strings. - Add pt-BR translation. - Remove site.ts and move its content to a i18n string. - Rename zh.json to zh-Hans.json to get the correct language name on selector dropdown. - Remove pt_BR.json and replace with pt-BR.json. - Some minor layout spacing fixes to accommodate larger texts. - Improve some error messages. * Delete .yarnrc.yml * Delete package-lock.json * fix: 158:28 error Insert `⏎` prettier/prettier
2 years ago
a.download = `memos-${getDateTimeString(Date.now())}.png`;
a.click();
downloadingImageState.setFinish();
})
.catch((err) => {
console.error(err);
});
};
const handleShowEmbedMemoDialog = () => {
showEmbedMemoDialog(memo.id);
};
const handleCopyLinkBtnClick = () => {
copy(`${window.location.origin}/m/${memo.id}`);
toast.success(t("message.succeed-copy-link"));
};
if (loadingState.isLoading) {
return null;
}
4 years ago
return (
<>
<div className="dialog-header-container py-3 px-4 !mb-0 rounded-t-lg">
<p className="">{t("common.share")} Memo</p>
4 years ago
<button className="btn close-btn" onClick={handleCloseBtnClick}>
<Icon.X className="icon-img" />
4 years ago
</button>
</div>
<div className="dialog-content-container w-full flex flex-col justify-start items-start relative">
<div className="px-4 pb-3 w-full flex flex-row justify-start items-center space-x-2">
<Button color="neutral" variant="outlined" disabled={downloadingImageState.isLoading} onClick={handleDownloadImageBtnClick}>
{downloadingImageState.isLoading ? (
<Icon.Loader className="w-4 h-auto mr-1 animate-spin" />
) : (
<Icon.Download className="w-4 h-auto mr-1" />
)}
{t("common.image")}
</Button>
<Button color="neutral" variant="outlined" onClick={handleShowEmbedMemoDialog}>
<Icon.Code className="w-4 h-auto mr-1" />
{t("memo.embed")}
</Button>
<Button color="neutral" variant="outlined" onClick={handleCopyLinkBtnClick}>
<Icon.Link className="w-4 h-auto mr-1" />
{t("common.link")}
</Button>
</div>
<div className="w-full border-t dark:border-zinc-700 overflow-clip">
<div
className="w-full h-auto select-none relative flex flex-col justify-start items-start bg-white dark:bg-zinc-800"
ref={memoElRef}
>
<span className="w-full px-6 pt-5 pb-2 text-sm text-gray-500">{memo.displayTsStr}</span>
<div className="w-full px-6 text-base pb-4">
<MemoContentV1 content={memo.content} />
<MemoResourceListView resourceList={memo.resourceList} />
</div>
<div className="flex flex-row justify-between items-center w-full bg-gray-100 dark:bg-zinc-700 py-4 px-6">
<div className="flex flex-row justify-start items-center">
<UserAvatar className="mr-2" avatarUrl={user.avatarUrl} />
<div className="w-auto grow truncate flex mr-2 flex-col justify-center items-start">
<span className="w-full text truncate font-medium text-gray-600 dark:text-gray-300">
{user.nickname || extractUsernameFromName(user.name)}
</span>
</div>
</div>
<span className="text-gray-500 dark:text-gray-400">via memos</span>
</div>
</div>
</div>
4 years ago
</div>
</>
);
};
export default function showShareMemoDialog(memo: Memo): void {
generateDialog(
4 years ago
{
className: "share-memo-dialog",
dialogName: "share-memo-dialog",
4 years ago
},
ShareMemoDialog,
4 years ago
{ memo }
);
}