Skip to content

Commit

Permalink
feat: add single message share logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yzh990918 committed Jun 9, 2023
1 parent 017055d commit 4de8a40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/components/header/ConversationMessageShareButton.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { useStore } from '@nanostores/solid'
import { currentConversationId } from '@/stores/conversation'
import { showShareModal } from '@/stores/ui'
import { getMessagesByConversationId, updateMessage } from '@/stores/messages'

export default () => {
const $currentConversationId = useStore(currentConversationId)
const handleShareContext = () => {
const messages = getMessagesByConversationId($currentConversationId())
messages.forEach((message) => {
updateMessage($currentConversationId(), message.id, { isSelected: true },
)
})
showShareModal.set(true)
}

return (
<>
{$currentConversationId() && (
<div
class="fcc p-2 rounded-md text-xl hv-foreground"
onClick={() => { showShareModal.set(true) }}
onClick={() => { handleShareContext() }}
>
<div i-carbon-export />
</div>
Expand Down
18 changes: 14 additions & 4 deletions src/components/main/MessageItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { For, Show } from 'solid-js/web'
import { createSignal } from 'solid-js'
import { useStore } from '@nanostores/solid'
import { useClipboardCopy } from '@/hooks'
import { deleteMessageByConversationId, spliceMessageByConversationId, spliceUpdateMessageByConversationId } from '@/stores/messages'
import { conversationMap } from '@/stores/conversation'
import { deleteMessageByConversationId, getMessagesByConversationId, spliceMessageByConversationId, spliceUpdateMessageByConversationId, updateMessage } from '@/stores/messages'
import { conversationMap, currentConversationId } from '@/stores/conversation'
import { handlePrompt } from '@/logics/conversation'
import { scrollController } from '@/stores/ui'
import { scrollController, showShareModal } from '@/stores/ui'
import { globalAbortController } from '@/stores/settings'
import StreamableText from '../StreamableText'
import { DropDownMenu, Tooltip } from '../ui/base'
Expand All @@ -23,6 +23,7 @@ interface Props {
export default (props: Props) => {
let inputRef: HTMLTextAreaElement
const $conversationMap = useStore(conversationMap)
const $currentConversationId = useStore(currentConversationId)

const [showRawCode, setShowRawCode] = createSignal(false)
const [copied, setCopied] = createSignal(false)
Expand Down Expand Up @@ -58,6 +59,15 @@ export default (props: Props) => {
inputRef.focus()
}

const handleShareMessageItem = () => {
const messages = getMessagesByConversationId($currentConversationId())
messages.forEach((message) => {
updateMessage($currentConversationId(), message.id, { isSelected: props.message.id === message.id },
)
})
showShareModal.set(true)
}

const handleSend = () => {
if (!inputRef.value)
return
Expand All @@ -80,7 +90,7 @@ export default (props: Props) => {
{ id: 'edit', label: 'Edit message', icon: 'i-carbon:edit', role: 'user', action: handleEditMessageItem },
{ id: 'copy', label: 'Copy message', icon: 'i-carbon-copy', role: 'all', action: handleCopyMessageItem },
{ id: 'delete', label: 'Delete message', icon: 'i-carbon-trash-can', role: 'all', action: handleDeleteMessageItem },
{ id: 'share', label: 'Share message', icon: 'i-carbon:export', role: 'all' },
{ id: 'share', label: 'Share message', icon: 'i-carbon:export', role: 'all', action: handleShareMessageItem },
])

if (props.message.role === 'user')
Expand Down
3 changes: 2 additions & 1 deletion src/stores/messages.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { action, map } from 'nanostores'
import { action, atom, map } from 'nanostores'
import { conversationMessagesMapData } from './tests/message.mock'
import { db } from './storage/message'
import { updateConversationById } from './conversation'
import type { MessageInstance } from '@/types/message'

export const conversationMessagesMap = map<Record<string, MessageInstance[]>>({})
export const shareMessageIds = atom<string[]>([])

export const rebuildMessagesStore = async() => {
const data = await db.exportData() || {}
Expand Down

0 comments on commit 4de8a40

Please sign in to comment.