From 3dff4330a1b6ab9d74004ee6ea67e2cd09a987eb Mon Sep 17 00:00:00 2001 From: an-lee Date: Fri, 12 Jan 2024 15:47:33 +0800 Subject: [PATCH] new post type --- enjoy/package.json | 2 +- enjoy/src/api/client.ts | 2 +- enjoy/src/renderer/components/posts/index.ts | 2 + .../components/posts/post-actions.tsx | 59 ++++++++++ .../renderer/components/posts/post-card.tsx | 105 ++++++++++++++++++ enjoy/src/renderer/components/posts/posts.tsx | 69 +----------- enjoy/src/renderer/pages/conversation.tsx | 5 +- enjoy/src/types.d.ts | 25 ----- enjoy/src/types/post.d.ts | 20 +++- enjoy/src/types/story.d.ts | 24 ++++ 10 files changed, 211 insertions(+), 102 deletions(-) create mode 100644 enjoy/src/renderer/components/posts/post-actions.tsx create mode 100644 enjoy/src/renderer/components/posts/post-card.tsx create mode 100644 enjoy/src/types/story.d.ts diff --git a/enjoy/package.json b/enjoy/package.json index 4925914f..fb6e6a16 100644 --- a/enjoy/package.json +++ b/enjoy/package.json @@ -7,7 +7,7 @@ "main": ".vite/build/main.js", "types": "./src/types.d.ts", "scripts": { - "dev": "rimraf .vite && WEB_API_URL=http://192.168.31.116:3000 electron-forge start", + "dev": "rimraf .vite && WEB_API_URL=http://localhost:3000 electron-forge start", "start": "rimraf .vite && electron-forge start", "package": "rimraf .vite && electron-forge package", "make": "rimraf .vite && electron-forge make", diff --git a/enjoy/src/api/client.ts b/enjoy/src/api/client.ts index fc0196b4..6038d578 100644 --- a/enjoy/src/api/client.ts +++ b/enjoy/src/api/client.ts @@ -95,7 +95,7 @@ export class Client { } createPost(params: { - content?: string; + metadata?: PostType["metadata"]; targetType?: string; targetId?: string; }): Promise { diff --git a/enjoy/src/renderer/components/posts/index.ts b/enjoy/src/renderer/components/posts/index.ts index 653291f2..801e16c4 100644 --- a/enjoy/src/renderer/components/posts/index.ts +++ b/enjoy/src/renderer/components/posts/index.ts @@ -1,2 +1,4 @@ export * from "./posts"; export * from "./post-audio-player"; +export * from "./post-card"; +export * from "./post-actions"; diff --git a/enjoy/src/renderer/components/posts/post-actions.tsx b/enjoy/src/renderer/components/posts/post-actions.tsx new file mode 100644 index 00000000..be0c94e8 --- /dev/null +++ b/enjoy/src/renderer/components/posts/post-actions.tsx @@ -0,0 +1,59 @@ +import { useContext, useEffect, useState } from "react"; +import { AppSettingsProviderContext } from "@renderer/context"; +import { PostAudioPlayer } from "@renderer/components"; +import { Button } from "@renderer/components/ui"; +import { formatDateTime } from "@renderer/lib/utils"; +import { t } from "i18next"; +import { MediaPlayer, MediaProvider } from "@vidstack/react"; +import { + DefaultVideoLayout, + defaultLayoutIcons, +} from "@vidstack/react/player/layouts/default"; +import Markdown from "react-markdown"; +import { BotIcon, CheckIcon, CopyPlusIcon, PlusCircleIcon } from "lucide-react"; +import { useCopyToClipboard } from "@uidotdev/usehooks"; + +export const PostActions = (props: { post: PostType }) => { + const { post } = props; + const [_, copyToClipboard] = useCopyToClipboard(); + const [copied, setCopied] = useState(false); + + return ( +
+ {post.target && post.targetType === "Medium" && ( + + )} + {typeof post.metadata?.content === "string" && ( + + )} + {post.metadata?.type === "prompt" && ( + + )} +
+ ); +}; diff --git a/enjoy/src/renderer/components/posts/post-card.tsx b/enjoy/src/renderer/components/posts/post-card.tsx new file mode 100644 index 00000000..03e994d8 --- /dev/null +++ b/enjoy/src/renderer/components/posts/post-card.tsx @@ -0,0 +1,105 @@ +import { useContext, useEffect, useState } from "react"; +import { AppSettingsProviderContext } from "@renderer/context"; +import { PostAudioPlayer, PostActions } from "@renderer/components"; +import { + Avatar, + AvatarImage, + AvatarFallback, + Button, +} from "@renderer/components/ui"; +import { formatDateTime } from "@renderer/lib/utils"; +import { t } from "i18next"; +import { MediaPlayer, MediaProvider } from "@vidstack/react"; +import { + DefaultVideoLayout, + defaultLayoutIcons, +} from "@vidstack/react/player/layouts/default"; +import Markdown from "react-markdown"; + +export const PostCard = (props: { post: PostType }) => { + const { post } = props; + + return ( +
+
+
+ + + + {post.user.name[0].toUpperCase()} + + +
+
{post.user.name}
+
+ {formatDateTime(post.createdAt)} +
+
+
+
+ + {post.metadata?.type === "prompt" && ( + <> +
+ {t("sharedPrompt")} +
+ + {"```prompt\n" + post.metadata.content + "\n```"} + + + )} + + {post.targetType == "Medium" && ( + + )} + + +
+ ); +}; + +const PostMedium = (props: { medium: MediumType }) => { + const { medium } = props; + if (!medium.sourceUrl) return null; + + return ( +
+ {medium.mediumType == "Video" && ( + <> +
+ {t("sharedAudio")} +
+ + + + + + )} + + {medium.mediumType == "Audio" && ( + <> +
+ {t("sharedAudio")} +
+ + + )} + + {medium.coverUrl && medium.mediumType == "Audio" && ( +
+ +
+ )} +
+ ); +}; + +const PostOptions = (props: { post: PostType }) => {}; diff --git a/enjoy/src/renderer/components/posts/posts.tsx b/enjoy/src/renderer/components/posts/posts.tsx index 3d36c4ad..6952ea52 100644 --- a/enjoy/src/renderer/components/posts/posts.tsx +++ b/enjoy/src/renderer/components/posts/posts.tsx @@ -1,14 +1,7 @@ import { useContext, useEffect, useState } from "react"; import { AppSettingsProviderContext } from "@renderer/context"; -import { PostAudioPlayer } from "@renderer/components"; -import { Avatar, AvatarImage, AvatarFallback } from "@renderer/components/ui"; +import { PostCard } from "@renderer/components"; import { t } from "i18next"; -import { MediaPlayer, MediaProvider } from "@vidstack/react"; -import { - DefaultVideoLayout, - defaultLayoutIcons, -} from "@vidstack/react/player/layouts/default"; -import Markdown from "react-markdown"; export const Posts = () => { const { webApi } = useContext(AppSettingsProviderContext); @@ -43,63 +36,3 @@ export const Posts = () => { ); }; - -const PostCard = (props: { post: PostType }) => { - const { post } = props; - - return ( -
-
-
- - - - {post.user.name[0].toUpperCase()} - - -
{post.user.name}
-
-
- {post.content && ( - {post.content} - )} - {post.targetType == "Medium" && } -
- ); -}; - -const PostMedium = (props: { medium: MediumType }) => { - const { medium } = props; - if (!medium.sourceUrl) return null; - - return ( - <> -
- {medium.mediumType == "Video" && ( - - - - - )} - - {medium.mediumType == "Audio" && ( - - )} -
- - {medium.coverUrl && medium.mediumType == "Audio" && ( -
- -
- )} - - ); -}; diff --git a/enjoy/src/renderer/pages/conversation.tsx b/enjoy/src/renderer/pages/conversation.tsx index 61019c7d..ed4ed6af 100644 --- a/enjoy/src/renderer/pages/conversation.tsx +++ b/enjoy/src/renderer/pages/conversation.tsx @@ -179,7 +179,10 @@ export default () => { const content = message.content; webApi .createPost({ - content, + metadata: { + type: "prompt", + content, + }, }) .then(() => { toast({ diff --git a/enjoy/src/types.d.ts b/enjoy/src/types.d.ts index 10192b1b..74eac327 100644 --- a/enjoy/src/types.d.ts +++ b/enjoy/src/types.d.ts @@ -105,31 +105,6 @@ type MeaningType = { lookups: LookupType[]; }; -type StoryType = { - id: string; - url: string; - title: string; - content: string; - metadata: { - [key: string]: string; - }; - vocabulary?: string[]; - extracted?: boolean; - starred?: boolean; - createdAt: Date; - updatedAt: Date; -}; - -type CreateStoryParamsType = { - title: string; - content: string; - url: string; - html: string; - metadata: { - [key: string]: string; - }; -}; - type PagyResponseType = { page: number; next: number | null; diff --git a/enjoy/src/types/post.d.ts b/enjoy/src/types/post.d.ts index 84a90544..41a0c94b 100644 --- a/enjoy/src/types/post.d.ts +++ b/enjoy/src/types/post.d.ts @@ -1,9 +1,17 @@ type PostType = { id: string; - content?: string; + metadata: { + type: 'text' | 'prompt' | 'llm_configuration'; + content: + | string + | { + [key: string]: any; + }; + }; user: UserType; - targetType: string; - target?: MediumType; - createdAt: string; - updatedAt: string; -} + targetType?: string; + targetId?: string; + target?: MediumType | StoryType; + createdAt: Date; + updatedAt: Date; +}; diff --git a/enjoy/src/types/story.d.ts b/enjoy/src/types/story.d.ts new file mode 100644 index 00000000..f3c63e02 --- /dev/null +++ b/enjoy/src/types/story.d.ts @@ -0,0 +1,24 @@ +type StoryType = { + id: string; + url: string; + title: string; + content: string; + metadata: { + [key: string]: string; + }; + vocabulary?: string[]; + extracted?: boolean; + starred?: boolean; + createdAt: Date; + updatedAt: Date; +}; + +type CreateStoryParamsType = { + title: string; + content: string; + url: string; + html: string; + metadata: { + [key: string]: string; + }; +};