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

Fix sharing, standardize share icon #725

Merged
merged 1 commit into from
Nov 11, 2024
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
4 changes: 2 additions & 2 deletions src/components/Message/MessageBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {

import { AiOutlineEdit } from "react-icons/ai";
import { MdContentCopy } from "react-icons/md";
import { TbDownload, TbShare2, TbTrash } from "react-icons/tb";
import { TbDownload, TbShare3, TbTrash } from "react-icons/tb";
import { Link as ReactRouterLink } from "react-router-dom";
import ResizeTextarea from "react-textarea-autosize";
import { Menu, MenuDivider, MenuItem, MenuItemLink, SubMenu } from "../Menu";
Expand Down Expand Up @@ -565,7 +565,7 @@ function MessageBase({
</>
)}
<MenuDivider />
<MenuItem onClick={() => handleShareMessage()} icon={<TbShare2 />}>
<MenuItem onClick={() => handleShareMessage()} icon={<TbShare3 />}>
Share Message
</MenuItem>
{(!disableEdit || shouldShowDeleteMenu) && <MenuDivider />}
Expand Down
42 changes: 4 additions & 38 deletions src/components/OptionsButton.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,25 @@
import { Button, IconButton, Input, useDisclosure } from "@chakra-ui/react";
import { useFetcher } from "react-router-dom";
import { TbShare2, TbTrash, TbCopy, TbDownload } from "react-icons/tb";
import { TbShare3, TbTrash, TbCopy, TbDownload } from "react-icons/tb";
import { PiGearBold } from "react-icons/pi";
import { BsPaperclip } from "react-icons/bs";
import { useCallback, useRef } from "react";
import { useCopyToClipboard } from "react-use";
import * as yaml from "yaml";

import { ChatCraftChat } from "../lib/ChatCraftChat";
import { useUser } from "../hooks/use-user";
import { useAlert } from "../hooks/use-alert";
import { useSettings } from "../hooks/use-settings";
import ShareModal from "./ShareModal";
import { download } from "../lib/utils";
import { Menu, MenuDivider, MenuItem, MenuItemLink, SubMenu } from "./Menu";

function ShareMenuItem({ chat }: { chat?: ChatCraftChat }) {
const supportsWebShare = !!navigator.share;
const { user } = useUser();
const { error } = useAlert();
function ShareMenuItem({ chat }: { chat: ChatCraftChat }) {
const { isOpen, onOpen, onClose } = useDisclosure();

const handleWebShare = useCallback(async () => {
if (!chat || !user) {
return;
}

try {
const { url } = await chat.share(user);
if (!url) {
throw new Error("Unable to create share URL for chat");
}

navigator.share({ title: "ChatCraft Chat", text: chat.summary, url });
} catch (err: any) {
console.error(err);
error({ title: "Unable to share chat", message: err.message });
}
}, [chat, user, error]);

// Nothing to share, disable the menu item
if (!chat) {
return (
<>
<MenuDivider />
<MenuItem icon={<TbShare2 />} isDisabled={true}>
Share
</MenuItem>
</>
);
}

return (
<>
<MenuItem icon={<TbShare2 />} onClick={supportsWebShare ? handleWebShare : onOpen}>
<MenuItem icon={<TbShare3 />} onClick={onOpen}>
Share
</MenuItem>
<ShareModal chat={chat} isOpen={isOpen} onClose={onClose} />
Expand Down Expand Up @@ -242,7 +208,7 @@ function OptionsButton({
Export as YAML
</MenuItem>
</SubMenu>
<ShareMenuItem chat={chat} />
{!!chat && <ShareMenuItem chat={chat} />}
<MenuDivider />
{!!onAttachFiles && (
<>
Expand Down
33 changes: 29 additions & 4 deletions src/components/ShareModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "@chakra-ui/react";
import { useCopyToClipboard } from "react-use";
import { BsGithub } from "react-icons/bs";
import { TbCopy } from "react-icons/tb";
import { TbCopy, TbShare3 } from "react-icons/tb";

import { useUser } from "../hooks/use-user";
import { ChatCraftChat } from "../lib/ChatCraftChat";
Expand All @@ -45,6 +45,7 @@ function AuthenticatedForm({ chat, user }: AuthenticatedForm) {
const [isSharing, setIsSharing] = useState(false);
const [, copyToClipboard] = useCopyToClipboard();
const { callChatApi } = useChatOpenAI();
const supportsWebShare = !!navigator.share;

const handleShareClick = async () => {
setIsSharing(true);
Expand Down Expand Up @@ -112,17 +113,25 @@ function AuthenticatedForm({ chat, user }: AuthenticatedForm) {
copyToClipboard(feedUrl);
}, [feedUrl, copyToClipboard]);

const handleShareUrl = async (url: string) => {
await navigator
.share({ title: "ChatCraft Chat", text: chat.summary, url })
.catch(console.error);
};

return (
<VStack gap={2}>
<FormControl>
<FormLabel>Summary</FormLabel>
<Textarea value={summary} onChange={(e) => setSummary(e.target.value)}></Textarea>
<Textarea value={summary} onChange={(e) => setSummary(e.target.value)}>
{chat.summary}
</Textarea>
</FormControl>

<FormControl>
<FormLabel>
Use your own <strong>Title</strong> and <strong>Summary</strong>, or click{" "}
<strong>Summarize</strong> to generate them automatically using GPT 3.5.
Enter your own <strong>Summary</strong>, or click <strong>Summarize</strong> to generate
one automatically.
</FormLabel>
<ButtonGroup w="100%" justifyContent="space-between">
<Button
Expand All @@ -148,6 +157,14 @@ function AuthenticatedForm({ chat, user }: AuthenticatedForm) {
<FormLabel>Public Share URL</FormLabel>
<Flex gap={1}>
<Input autoFocus={false} type="url" defaultValue={url} readOnly flex={1} />{" "}
{supportsWebShare && (
<IconButton
icon={<TbShare3 />}
aria-label="Share URL"
variant="ghost"
onClick={() => handleShareUrl(url)}
/>
)}
<IconButton
icon={<TbCopy />}
aria-label="Copy URL"
Expand All @@ -161,6 +178,14 @@ function AuthenticatedForm({ chat, user }: AuthenticatedForm) {
<FormLabel>Public Shared Chats Feed URL</FormLabel>
<Flex gap={1}>
<Input autoFocus={false} type="url" defaultValue={feedUrl} readOnly flex={1} />{" "}
{supportsWebShare && (
<IconButton
icon={<TbShare3 />}
aria-label="Share Feed URL"
variant="ghost"
onClick={() => handleShareUrl(feedUrl)}
/>
)}
<IconButton
icon={<TbCopy />}
aria-label="Copy URL"
Expand Down
Loading