-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(matches): export chat messages from context-menu
ref #1056
- Loading branch information
Showing
20 changed files
with
460 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { fetchChatMessages } from '../database/chat-messages/fetch-chat-messages'; | ||
import { writeChatMessagesToFile } from './write-chat-messages-to-file'; | ||
import type { ChatMessage } from 'csdm/common/types/chat-message'; | ||
|
||
export async function exportMatchChatMessages(checksum: string, filePath: string, messages?: ChatMessage[]) { | ||
let chatMessages: ChatMessage[]; | ||
if (!messages) { | ||
chatMessages = await fetchChatMessages(checksum); | ||
} else { | ||
chatMessages = messages; | ||
} | ||
|
||
return await writeChatMessagesToFile(chatMessages, filePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { fetchChatMessages } from '../database/chat-messages/fetch-chat-messages'; | ||
import path from 'node:path'; | ||
import { writeChatMessagesToFile } from './write-chat-messages-to-file'; | ||
|
||
export async function exportMatchesChatMessages(checksums: string[], outputFolder: string) { | ||
let hasExportedAtLeastOneMatch = false; | ||
|
||
await Promise.all( | ||
checksums.map(async (checksum) => { | ||
const messages = await fetchChatMessages(checksum); | ||
const filePath = path.join(outputFolder, `messages-${checksum}.txt`); | ||
const hasWrittenFile = await writeChatMessagesToFile(messages, filePath); | ||
if (hasWrittenFile) { | ||
hasExportedAtLeastOneMatch = true; | ||
} | ||
}), | ||
); | ||
|
||
return hasExportedAtLeastOneMatch; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import fs from 'fs-extra'; | ||
import type { ChatMessage } from 'csdm/common/types/chat-message'; | ||
|
||
function formatChatMessage(chatMessage: ChatMessage) { | ||
const { senderIsAlive, senderName, message } = chatMessage; | ||
const playerStatus = senderIsAlive ? '' : '*DEAD* '; | ||
|
||
return `${playerStatus}${senderName} : ${message}`; | ||
} | ||
|
||
export async function writeChatMessagesToFile(messages: ChatMessage[], filePath: string) { | ||
if (messages.length === 0) { | ||
return false; | ||
} | ||
|
||
const formattedChatMessages = messages.map(formatChatMessage); | ||
const text = formattedChatMessages.join('\n'); | ||
await fs.writeFile(filePath, text); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
src/server/handlers/renderer-process/match/export-match-chat-messages-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/server/handlers/renderer-process/match/export-matches-chat-messages-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { exportMatchesChatMessages } from 'csdm/node/chat-messages/export-matches-chat-messages'; | ||
import { handleError } from '../../handle-error'; | ||
|
||
export type ExportMatchesChatMessagesPayload = { outputFolderPath: string; checksums: string[] }; | ||
|
||
export async function exportMatchesChatMessagesHandler({ | ||
checksums, | ||
outputFolderPath, | ||
}: ExportMatchesChatMessagesPayload) { | ||
try { | ||
return await exportMatchesChatMessages(checksums, outputFolderPath); | ||
} catch (error) { | ||
handleError(error, 'Error while exporting matches chat messages'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/ui/components/context-menu/items/export-chat-messages-item.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from 'react'; | ||
import { Trans, useLingui } from '@lingui/react/macro'; | ||
import { ContextMenuItem } from 'csdm/ui/components/context-menu/context-menu-item'; | ||
import { useWebSocketClient } from 'csdm/ui/hooks/use-web-socket-client'; | ||
import { useShowToast } from '../../toasts/use-show-toast'; | ||
import { RendererClientMessageName } from 'csdm/server/renderer-client-message-name'; | ||
import type { ExportMatchesChatMessagesPayload } from 'csdm/server/handlers/renderer-process/match/export-matches-chat-messages-handler'; | ||
import { useExportMatchChatMessages } from 'csdm/ui/hooks/use-export-match-chat-messages'; | ||
|
||
type Props = { | ||
checksums: string[]; | ||
}; | ||
|
||
export function ExportChatMessagesItem({ checksums }: Props) { | ||
const { t } = useLingui(); | ||
const client = useWebSocketClient(); | ||
const showToast = useShowToast(); | ||
const exportChatMessages = useExportMatchChatMessages(); | ||
|
||
const onClick = async () => { | ||
if (checksums.length === 1) { | ||
return exportChatMessages(checksums[0]); | ||
} | ||
|
||
const { filePaths, canceled } = await window.csdm.showOpenDialog({ | ||
buttonLabel: t({ | ||
context: 'Button label', | ||
message: 'Select', | ||
}), | ||
properties: ['openDirectory'], | ||
}); | ||
|
||
if (canceled || filePaths.length === 0) { | ||
return; | ||
} | ||
|
||
const outputFolderPath = filePaths[0]; | ||
|
||
try { | ||
const payload: ExportMatchesChatMessagesPayload = { | ||
checksums, | ||
outputFolderPath, | ||
}; | ||
const hasExportedAtLeastOneMatch = await client.send({ | ||
name: RendererClientMessageName.ExportMatchesChatMessages, | ||
payload, | ||
}); | ||
|
||
if (hasExportedAtLeastOneMatch) { | ||
showToast({ | ||
content: <Trans context="Toast">Chat messages exported, click here to reveal the folder</Trans>, | ||
type: 'success', | ||
onClick: () => { | ||
window.csdm.browseToFolder(outputFolderPath); | ||
}, | ||
}); | ||
} else { | ||
showToast({ | ||
content: <Trans>No chat messages to export</Trans>, | ||
type: 'warning', | ||
}); | ||
} | ||
} catch (error) { | ||
showToast({ | ||
content: <Trans>An error occurred</Trans>, | ||
type: 'error', | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<ContextMenuItem onClick={onClick} isDisabled={checksums.length === 0}> | ||
<Trans context="Context menu">Chat messages</Trans> | ||
</ContextMenuItem> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import { Trans, useLingui } from '@lingui/react/macro'; | ||
import { useShowToast } from '../components/toasts/use-show-toast'; | ||
import { useWebSocketClient } from './use-web-socket-client'; | ||
import type { SaveDialogOptions, SaveDialogReturnValue } from 'electron'; | ||
import type { ExportChatMessagesPayload } from 'csdm/server/handlers/renderer-process/match/export-match-chat-messages-handler'; | ||
import type { ChatMessage } from 'csdm/common/types/chat-message'; | ||
import { RendererClientMessageName } from 'csdm/server/renderer-client-message-name'; | ||
|
||
export function useExportMatchChatMessages() { | ||
const client = useWebSocketClient(); | ||
const showToast = useShowToast(); | ||
const { t } = useLingui(); | ||
|
||
return async (checksum: string, messages?: ChatMessage[]) => { | ||
const options: SaveDialogOptions = { | ||
defaultPath: `messages-${checksum}.txt`, | ||
title: t({ | ||
context: 'OS save dialog title', | ||
message: 'Export', | ||
}), | ||
filters: [{ name: 'TXT', extensions: ['txt'] }], | ||
}; | ||
const { canceled, filePath }: SaveDialogReturnValue = await window.csdm.showSaveDialog(options); | ||
if (canceled || filePath === undefined) { | ||
return; | ||
} | ||
|
||
try { | ||
const payload: ExportChatMessagesPayload = { | ||
checksum, | ||
filePath, | ||
messages, | ||
}; | ||
const hasMessages = await client.send({ | ||
name: RendererClientMessageName.ExportMatchChatMessages, | ||
payload, | ||
}); | ||
|
||
if (hasMessages) { | ||
showToast({ | ||
content: <Trans>Chat messages exported, click here to reveal the file</Trans>, | ||
type: 'success', | ||
onClick: () => { | ||
window.csdm.browseToFile(filePath); | ||
}, | ||
}); | ||
} else { | ||
showToast({ | ||
content: <Trans>No chat messages to export</Trans>, | ||
type: 'warning', | ||
}); | ||
} | ||
} catch (error) { | ||
showToast({ | ||
content: <Trans>An error occurred</Trans>, | ||
type: 'error', | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 8 additions & 59 deletions
67
src/ui/match/chat-messages/export-chat-messages-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.