Skip to content

Commit

Permalink
feature: Add title to bookmarks and allow editing them. Fixes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Apr 15, 2024
1 parent 5c9acb1 commit fc85cce
Show file tree
Hide file tree
Showing 17 changed files with 1,240 additions and 55 deletions.
9 changes: 8 additions & 1 deletion apps/mobile/components/bookmarks/BookmarkCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function LinkCard({ bookmark }: { bookmark: ZBookmark }) {
className="line-clamp-2 text-xl font-bold"
onPress={() => WebBrowser.openBrowserAsync(url)}
>
{bookmark.content.title ?? parsedUrl.host}
{bookmark.title ?? bookmark.content.title ?? parsedUrl.host}
</Text>
<TagList bookmark={bookmark} />
<Divider orientation="vertical" className="mt-2 h-0.5 w-full" />
Expand All @@ -220,6 +220,9 @@ function TextCard({ bookmark }: { bookmark: ZBookmark }) {
}
return (
<View className="flex max-h-96 gap-2 p-2">
{bookmark.title && (
<Text className="line-clamp-2 text-xl font-bold">{bookmark.title}</Text>
)}
<View className="max-h-56 overflow-hidden p-2">
<Markdown>{bookmark.content.text}</Markdown>
</View>
Expand All @@ -238,6 +241,7 @@ function AssetCard({ bookmark }: { bookmark: ZBookmark }) {
if (bookmark.content.type !== "asset") {
throw new Error("Wrong content type rendered");
}
const title = bookmark.title ?? bookmark.content.fileName;

return (
<View className="flex gap-2">
Expand All @@ -251,6 +255,9 @@ function AssetCard({ bookmark }: { bookmark: ZBookmark }) {
className="h-56 min-h-56 w-full object-cover"
/>
<View className="flex gap-2 p-2">
{title && (
<Text className="line-clamp-2 text-xl font-bold">{title}</Text>
)}
<TagList bookmark={bookmark} />
<Divider orientation="vertical" className="mt-2 h-0.5 w-full" />
<View className="mt-2 flex flex-row justify-between px-2 pb-2">
Expand Down
2 changes: 1 addition & 1 deletion apps/web/components/dashboard/bookmarks/AssetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function AssetCard({

return (
<BookmarkLayoutAdaptingCard
title={bookmarkedAsset.content.fileName}
title={bookmarkedAsset.title ?? bookmarkedAsset.content.fileName}
footer={null}
bookmark={bookmarkedAsset}
className={className}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/components/dashboard/bookmarks/LinkCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function LinkTitle({ bookmark }: { bookmark: ZBookmarkTypeLink }) {
const parsedUrl = new URL(link.url);
return (
<Link className="line-clamp-2" href={link.url} target="_blank">
{link?.title ?? parsedUrl.host}
{bookmark.title ?? link?.title ?? parsedUrl.host}
</Link>
);
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/components/dashboard/bookmarks/TextCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function TextCard({
setOpen={setPreviewModalOpen}
/>
<BookmarkLayoutAdaptingCard
title={bookmark.title}
content={
<Markdown className="prose dark:prose-invert">
{bookmarkedText.text}
Expand Down
51 changes: 15 additions & 36 deletions apps/web/components/dashboard/preview/BookmarkPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type { ZBookmark } from "@hoarder/trpc/types/bookmarks";

import ActionBar from "./ActionBar";
import { AssetContentSection } from "./AssetContentSection";
import { EditableTitle } from "./EditableTitle";
import { NoteEditor } from "./NoteEditor";
import { TextContentSection } from "./TextContentSection";

Expand Down Expand Up @@ -62,37 +63,6 @@ function CreationTime({ createdAt }: { createdAt: Date }) {
);
}

function LinkHeader({ bookmark }: { bookmark: ZBookmark }) {
if (bookmark.content.type !== "link") {
throw new Error("Unexpected content type");
}

const title = bookmark.content.title ?? bookmark.content.url;

return (
<div className="flex w-full flex-col items-center justify-center space-y-3">
<Tooltip>
<TooltipTrigger asChild>
<p className="line-clamp-2 text-center text-lg">{title}</p>
</TooltipTrigger>
<TooltipPortal>
<TooltipContent side="bottom" className="w-96">
{title}
</TooltipContent>
</TooltipPortal>
</Tooltip>
<Link
href={bookmark.content.url}
className="mx-auto flex gap-2 text-gray-400"
>
<span className="my-auto">View Original</span>
<ExternalLink />
</Link>
<Separator />
</div>
);
}

export default function BookmarkPreview({
initialData,
}: {
Expand Down Expand Up @@ -131,17 +101,26 @@ export default function BookmarkPreview({
}
}

const linkHeader = bookmark.content.type == "link" && (
<LinkHeader bookmark={bookmark} />
);

return (
<div className="grid h-full grid-rows-3 gap-2 overflow-hidden bg-background lg:grid-cols-3 lg:grid-rows-none">
<div className="row-span-2 h-full w-full overflow-auto p-2 md:col-span-2 lg:row-auto">
{isBookmarkStillCrawling(bookmark) ? <ContentLoading /> : content}
</div>
<div className="lg:col-span1 row-span-1 flex flex-col gap-4 overflow-auto bg-accent p-4 lg:row-auto">
{linkHeader}
<div className="flex w-full flex-col items-center justify-center gap-y-2">
<EditableTitle bookmark={bookmark} />
{bookmark.content.type == "link" && (
<Link
href={bookmark.content.url}
className="flex items-center gap-2 text-gray-400"
>
<span>View Original</span>
<ExternalLink />
</Link>
)}
<Separator />
</div>

<CreationTime createdAt={bookmark.createdAt} />
<div className="flex gap-4">
<p className="text-sm text-gray-400">Tags</p>
Expand Down
165 changes: 165 additions & 0 deletions apps/web/components/dashboard/preview/EditableTitle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { useEffect, useRef, useState } from "react";
import { ActionButtonWithTooltip } from "@/components/ui/action-button";
import { ButtonWithTooltip } from "@/components/ui/button";
import {
Tooltip,
TooltipContent,
TooltipPortal,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { toast } from "@/components/ui/use-toast";
import { Check, Pencil, X } from "lucide-react";

import { useUpdateBookmark } from "@hoarder/shared-react/hooks/bookmarks";
import { ZBookmark } from "@hoarder/trpc/types/bookmarks";

interface Props {
bookmarkId: string;
originalTitle: string | null;
setEditable: (editable: boolean) => void;
}

function EditMode({ bookmarkId, originalTitle, setEditable }: Props) {
const ref = useRef<HTMLDivElement>(null);

const { mutate: updateBookmark, isPending } = useUpdateBookmark({
onSuccess: () => {
toast({
description: "Title updated!",
});
},
});

useEffect(() => {
if (ref.current) {
ref.current.focus();
ref.current.textContent = originalTitle;
}
}, [ref]);

const onSave = () => {
let toSave: string | null = ref.current?.textContent ?? null;
if (originalTitle == toSave) {
// Nothing to do here
return;
}
if (toSave == "") {
toSave = null;
}
updateBookmark({
bookmarkId,
title: toSave,
});
setEditable(false);
};

return (
<div className="flex gap-3">
<div
ref={ref}
role="presentation"
className="p-2 text-center text-lg"
contentEditable={true}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
}
}}
/>
<ActionButtonWithTooltip
tooltip="Save"
delayDuration={500}
size="none"
variant="ghost"
className="align-middle text-gray-400"
loading={isPending}
onClick={() => onSave()}
>
<Check className="size-4" />
</ActionButtonWithTooltip>
<ButtonWithTooltip
tooltip="Cancel"
delayDuration={500}
size="none"
variant="ghost"
className="align-middle text-gray-400"
onClick={() => {
setEditable(false);
}}
>
<X className="size-4" />
</ButtonWithTooltip>
</div>
);
}

function ViewMode({ originalTitle, setEditable }: Props) {
return (
<Tooltip delayDuration={500}>
<div className="flex items-center gap-3 text-center">
<TooltipTrigger asChild>
{originalTitle ? (
<p className="line-clamp-2 text-lg">{originalTitle}</p>
) : (
<p className="text-lg italic text-gray-600">Untitled</p>
)}
</TooltipTrigger>
<ButtonWithTooltip
delayDuration={500}
tooltip="Edit title"
size="none"
variant="ghost"
className="align-middle text-gray-400"
onClick={() => {
setEditable(true);
}}
>
<Pencil className="size-4" />
</ButtonWithTooltip>
</div>
<TooltipPortal>
{originalTitle && (
<TooltipContent side="bottom" className="max-w-[40ch]">
{originalTitle}
</TooltipContent>
)}
</TooltipPortal>
</Tooltip>
);
}

export function EditableTitle({ bookmark }: { bookmark: ZBookmark }) {
const [editable, setEditable] = useState(false);

let title: string | null = null;
switch (bookmark.content.type) {
case "link":
title = bookmark.content.title ?? bookmark.content.url;
break;
case "text":
title = null;
break;
case "asset":
title = bookmark.content.fileName ?? null;
break;
}

title = bookmark.title ?? title;
if (title == "") {
title = null;
}

return editable ? (
<EditMode
bookmarkId={bookmark.id}
originalTitle={title}
setEditable={setEditable}
/>
) : (
<ViewMode
bookmarkId={bookmark.id}
originalTitle={title}
setEditable={setEditable}
/>
);
}
40 changes: 31 additions & 9 deletions apps/web/components/ui/action-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { useClientConfig } from "@/lib/clientConfig";
import type { ButtonProps } from "./button";
import { Button } from "./button";
import LoadingSpinner from "./spinner";
import {
Tooltip,
TooltipContent,
TooltipPortal,
TooltipTrigger,
} from "./tooltip";

const ActionButton = React.forwardRef<
HTMLButtonElement,
ButtonProps & {
loading: boolean;
spinner?: React.ReactNode;
ignoreDemoMode?: boolean;
}
>(
interface ActionButtonProps extends ButtonProps {
loading: boolean;
spinner?: React.ReactNode;
ignoreDemoMode?: boolean;
}

const ActionButton = React.forwardRef<HTMLButtonElement, ActionButtonProps>(
(
{ children, loading, spinner, disabled, ignoreDemoMode = false, ...props },
ref,
Expand All @@ -35,4 +40,21 @@ const ActionButton = React.forwardRef<
);
ActionButton.displayName = "ActionButton";

export { ActionButton };
const ActionButtonWithTooltip = React.forwardRef<
HTMLButtonElement,
ActionButtonProps & { tooltip: string; delayDuration?: number }
>(({ tooltip, delayDuration, ...props }, ref) => {
return (
<Tooltip delayDuration={delayDuration}>
<TooltipTrigger>
<ActionButton ref={ref} {...props} />
</TooltipTrigger>
<TooltipPortal>
<TooltipContent>{tooltip}</TooltipContent>
</TooltipPortal>
</Tooltip>
);
});
ActionButtonWithTooltip.displayName = "ActionButtonWithTooltip";

export { ActionButton, ActionButtonWithTooltip };
Loading

0 comments on commit fc85cce

Please sign in to comment.