import { Form, Image, Input, Modal, Tooltip, message } from "antd"
import { Share } from "lucide-react"
import { useState } from "react"
import type { Message } from "~/store/option"
import Markdown from "./Markdown"
import React from "react"
import { useMutation } from "@tanstack/react-query"
import { getPageShareUrl } from "~/services/ollama"
import { cleanUrl } from "~/libs/clean-url"
import { getTitleById, getUserId, saveWebshare } from "@/db"
import { useTranslation } from "react-i18next"
import fetcher from "@/libs/fetcher"
type Props = {
messages: Message[]
historyId: string
}
const reformatMessages = (messages: Message[], username: string) => {
return messages.map((message, idx) => {
return {
id: idx,
name: message.isBot ? message.name : username,
isBot: message.isBot,
message: message.message,
images: message.images
}
})
}
export const PlaygroundMessage = (
props: Message & {
username: string
}
) => {
return (
{props.isBot ? (
) : (
)}
{props.isBot ? props.name : props.username}
{/* source if aviable */}
{props.images && props.images.length > 0 && (
{props.images
.filter((image) => image.length > 0)
.map((image, index) => (
))}
)}
)
}
export const ShareBtn: React.FC = ({ messages, historyId }) => {
const { t } = useTranslation("common")
const [open, setOpen] = useState(false)
const [form] = Form.useForm()
const name = Form.useWatch("name", form)
React.useEffect(() => {
if (messages.length > 0) {
getTitleById(historyId).then((title) => {
form.setFieldsValue({
title
})
})
}
}, [messages, historyId])
const onSubmit = async (values: { title: string; name: string }) => {
const owner_id = await getUserId()
const chat = reformatMessages(messages, values.name)
const title = values.title
const url = await getPageShareUrl()
const res = await fetcher(`${cleanUrl(url)}/api/v1/share/create`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
owner_id,
messages: chat,
title
})
})
if (!res.ok) throw new Error(t("share.notification.failGenerate"))
const data = await res.json()
return {
...data,
url: `${cleanUrl(url)}/share/${data.chat_id}`,
api_url: cleanUrl(url),
share_id: data.chat_id
}
}
const { mutate: createShareLink, isPending } = useMutation({
mutationFn: onSubmit,
onSuccess: async (data) => {
const url = data.url
navigator.clipboard.writeText(url)
message.success(t("share.notification.successGenerate"))
await saveWebshare({
title: data.title,
url,
api_url: data.api_url,
share_id: data.share_id
})
setOpen(false)
},
onError: (error) => {
message.error(error?.message || t("share.notification.failGenerate"))
}
})
return (
<>
setOpen(false)}>
{messages.map((message, index) => (
))}
>
)
}