From 0252bc022306123973ffd27b1cb211aadffae052 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 9 Aug 2024 17:14:53 +0800 Subject: [PATCH] feat: preview social media ai daily Signed-off-by: Innei --- .../src/components/ui/markdown/Markdown.tsx | 24 +- .../ui/markdown/renderers/MarkdownLink.tsx | 22 +- src/renderer/src/lib/parse-markdown.ts | 11 +- .../ai/ai-daily/FeedDailyModalContent.tsx | 10 +- .../src/modules/ai/ai-daily/daily.tsx | 262 ++++++++++++++++-- .../entry-column/layouts/EntryListHeader.tsx | 16 +- 6 files changed, 302 insertions(+), 43 deletions(-) diff --git a/src/renderer/src/components/ui/markdown/Markdown.tsx b/src/renderer/src/components/ui/markdown/Markdown.tsx index 087371d43..6fad7c1aa 100644 --- a/src/renderer/src/components/ui/markdown/Markdown.tsx +++ b/src/renderer/src/components/ui/markdown/Markdown.tsx @@ -1,17 +1,27 @@ +import type { RemarkOptions } from "@renderer/lib/parse-markdown" import { parseMarkdown } from "@renderer/lib/parse-markdown" import { cn } from "@renderer/lib/utils" -import { useMemo } from "react" +import { useMemo, useState } from "react" + +export const Markdown: Component< + { + children: string + } & Partial, +> = ({ children, components, className }) => { + const stableRemarkOptions = useState({ components })[0] -export const Markdown: Component<{ - children: string -}> = ({ children, className }) => { const markdownElement = useMemo( - () => parseMarkdown(children).content, - [children], + () => parseMarkdown(children, { ...stableRemarkOptions }).content, + [children, stableRemarkOptions], ) return ( -
+
{markdownElement}
) diff --git a/src/renderer/src/components/ui/markdown/renderers/MarkdownLink.tsx b/src/renderer/src/components/ui/markdown/renderers/MarkdownLink.tsx index 7270d9a5e..e3db00ae2 100644 --- a/src/renderer/src/components/ui/markdown/renderers/MarkdownLink.tsx +++ b/src/renderer/src/components/ui/markdown/renderers/MarkdownLink.tsx @@ -1,5 +1,6 @@ import { useNavigateEntry } from "@renderer/hooks/biz/useNavigateEntry" import { FeedViewType } from "@renderer/lib/enum" +import { isBizId } from "@renderer/lib/utils" import { useEntryContentContext } from "@renderer/modules/entry-content/hooks" import { useEntry } from "@renderer/store/entry" import { useCallback } from "react" @@ -16,18 +17,21 @@ import { ensureAndRenderTimeStamp } from "../utils" export const MarkdownLink = (props: LinkProps) => { const { view } = useEntryContentContext() - const entryId = (/^\w{17}$/.test(props.href)) ? props.href : null + const entryId = isBizId(props.href) ? props.href : null const entry = useEntry(entryId) const navigate = useNavigateEntry() - const onClick = useCallback((e: React.MouseEvent) => { - if (entryId) { - e.preventDefault() - navigate({ - entryId, - }) - } - }, [entryId, navigate]) + const onClick = useCallback( + (e: React.MouseEvent) => { + if (entryId) { + e.preventDefault() + navigate({ + entryId, + }) + } + }, + [entryId, navigate], + ) const parseTimeStamp = view === FeedViewType.Audios if (parseTimeStamp) { diff --git a/src/renderer/src/lib/parse-markdown.ts b/src/renderer/src/lib/parse-markdown.ts index 44b8821f4..ef21da3cd 100644 --- a/src/renderer/src/lib/parse-markdown.ts +++ b/src/renderer/src/lib/parse-markdown.ts @@ -5,6 +5,7 @@ import "remark-gh-alerts/styles/github-base.css" import remarkCalloutDirectives from "@microflash/remark-callout-directives" import { MarkdownLink } from "@renderer/components/ui/markdown/renderers" +import type { Components } from "hast-util-to-jsx-runtime" import { toJsxRuntime } from "hast-util-to-jsx-runtime" import { createElement } from "react" import { Fragment, jsx, jsxs } from "react/jsx-runtime" @@ -17,8 +18,15 @@ import remarkRehype from "remark-rehype" import { unified } from "unified" import { VFile } from "vfile" -export const parseMarkdown = (content: string) => { +export interface RemarkOptions { + components: Partial +} +export const parseMarkdown = ( + content: string, + options?: Partial, +) => { const file = new VFile(content) + const { components } = options || {} const pipeline = unified() .use(remarkParse) @@ -73,6 +81,7 @@ export const parseMarkdown = (content: string) => { components: { a: ({ node, ...props }) => createElement(MarkdownLink, { ...props } as any), + ...components, }, }), } diff --git a/src/renderer/src/modules/ai/ai-daily/FeedDailyModalContent.tsx b/src/renderer/src/modules/ai/ai-daily/FeedDailyModalContent.tsx index 8b6e18948..0a4eff29a 100644 --- a/src/renderer/src/modules/ai/ai-daily/FeedDailyModalContent.tsx +++ b/src/renderer/src/modules/ai/ai-daily/FeedDailyModalContent.tsx @@ -7,7 +7,10 @@ import { import { FeedViewType } from "@renderer/lib/enum" import { DayOf } from "./constants" -import { DailyReportContent, DailyReportTitle } from "./daily" +import { + DailyReportModalContent, + DailyReportTitle, +} from "./daily" import { useParseDailyDate } from "./hooks" const tabs = [DayOf.Today, DayOf.Yesterday] @@ -29,10 +32,7 @@ export const FeedDailyModalContent = () => {
{tabs.map((tab: any) => ( - diff --git a/src/renderer/src/modules/ai/ai-daily/daily.tsx b/src/renderer/src/modules/ai/ai-daily/daily.tsx index 9ef0cd143..767c4ddb8 100644 --- a/src/renderer/src/modules/ai/ai-daily/daily.tsx +++ b/src/renderer/src/modules/ai/ai-daily/daily.tsx @@ -1,8 +1,17 @@ +import { FeedIcon } from "@renderer/components/feed-icon" +import { EmptyIcon } from "@renderer/components/icons/empty" import { AutoResizeHeight } from "@renderer/components/ui/auto-resize-height" import { Card, CardContent } from "@renderer/components/ui/card" import { Collapse } from "@renderer/components/ui/collapse" +import { RelativeTime } from "@renderer/components/ui/datetime" +import type { LinkProps } from "@renderer/components/ui/link" import { LoadingCircle } from "@renderer/components/ui/loading" import { Markdown } from "@renderer/components/ui/markdown" +import { MarkdownLink } from "@renderer/components/ui/markdown/renderers" +import { Media } from "@renderer/components/ui/media" +import { usePreviewMedia } from "@renderer/components/ui/media/hooks" +import { useCurrentModal, useModalStack } from "@renderer/components/ui/modal" +import { NoopChildren } from "@renderer/components/ui/modal/stacked/utils" import { ScrollArea } from "@renderer/components/ui/scroll-area" import { Tooltip, @@ -13,9 +22,15 @@ import { import { useAuthQuery } from "@renderer/hooks/common" import { apiClient } from "@renderer/lib/api-fetch" import { defineQuery } from "@renderer/lib/defineQuery" -import { cn } from "@renderer/lib/utils" +import { nextFrame } from "@renderer/lib/dom" +import { cn, isBizId } from "@renderer/lib/utils" import { MarkAllButton } from "@renderer/modules/entry-column/components/mark-all-button" -import { Fragment, useState } from "react" +import { StarIcon } from "@renderer/modules/entry-column/star-icon" +import { Queries } from "@renderer/queries" +import { useEntry } from "@renderer/store/entry" +import { useFeedById } from "@renderer/store/feed" +import { m, useAnimationControls } from "framer-motion" +import { useEffect, useState } from "react" import { useParseDailyDate } from "./hooks" import type { DailyItemProps, DailyView } from "./types" @@ -88,21 +103,12 @@ export const DailyReportTitle = ({
) -export const DailyReportContent: Component<{ - view: DailyView - startDate: number - endDate: number - viewportClassName?: string - autoResize?: boolean -}> = ({ +const useQueryData = ({ endDate, startDate, - autoResize = true, view, - className, - viewportClassName, -}) => { - const content = useAuthQuery( +}: Pick) => + useAuthQuery( defineQuery(["daily", view, startDate, endDate], async () => { const res = await apiClient.ai.daily.$get({ query: { @@ -119,19 +125,32 @@ export const DailyReportContent: Component<{ }, ) +interface DailyReportContentProps { + view: DailyView + startDate: number + endDate: number +} + +export const DailyReportContent: Component = ({ + endDate, + startDate, + + view, + className, +}) => { + const content = useQueryData({ endDate, startDate, view }) const [markAllButtonRef, setMarkAllButtonRef] = useState(null) - const ResizeSwitch = autoResize ? AutoResizeHeight : Fragment return ( - + {content.isLoading ? ( ) : ( @@ -141,7 +160,7 @@ export const DailyReportContent: Component<{ ) )} - + {!!content.data && ( + )} + + ) +} + +const RelatedEntryLink = (props: LinkProps) => { + const { href, children } = props + const entryId = isBizId(href) ? href : null + + const { present } = useModalStack() + + if (!entryId) { + return + } + return ( + + ) +} + +const EntryPreview = ({ entryId }: { entryId: string }) => { + const modal = useCurrentModal() + useAuthQuery(Queries.entries.byId(entryId)) + + const variants = { + enter: { + x: 0, + opacity: 1, + }, + initial: { + x: 700, + opacity: 0.9, + }, + exit: { + x: 750, + opacity: 0, + }, + } + const entry = useEntry(entryId) + const feed = useFeedById(entry?.feedId || "") + const controller = useAnimationControls() + useEffect(() => { + nextFrame(() => controller.start("enter")) + }, [controller]) + + const previewMedia = usePreviewMedia() + if (!entry || !feed) { + return null + } + + return ( +
+ e.stopPropagation()} + tabIndex={-1} + initial="initial" + animate={controller} + variants={variants} + transition={{ + type: "spring", + mass: 0.4, + tension: 120, + friction: 1.4, + }} + exit="exit" + layout="size" + className={cn( + "shadow-perfect relative flex flex-col items-center rounded-xl border bg-theme-background p-8", + "mr-4 mt-4 max-h-[500px] w-[60ch] max-w-full overflow-auto", + )} + > + +
+ +
+
+ {entry.entries.author} + ยท + + + +
+
+
+ {entry.entries.description} +
+ {!!entry.collections && } +
+ +
+ {entry.entries.media?.map((media, i, mediaList) => ( + { + e.stopPropagation() + previewMedia(mediaList, i) + }} + /> + ))} +
+ + {/* End right column */} +
+
+
+
+ ) +} diff --git a/src/renderer/src/modules/entry-column/layouts/EntryListHeader.tsx b/src/renderer/src/modules/entry-column/layouts/EntryListHeader.tsx index 823e2f98d..fd560a3cd 100644 --- a/src/renderer/src/modules/entry-column/layouts/EntryListHeader.tsx +++ b/src/renderer/src/modules/entry-column/layouts/EntryListHeader.tsx @@ -171,9 +171,19 @@ export const EntryListHeader: FC<{ const DailyReportButton: FC = () => { const present = useAIDailyReportModal() return ( - - - + + { + present() + window.posthog?.capture("Daily Report Modal", { + click: 1, + }) + }} + tooltip="Daily Report" + > + + + ) }