Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): add grouped messages #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/app/src/front/components/grid/LogCell.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FsdtLogMessageContent } from '@fullstack-devtool/core'
import styled from '@emotion/styled'
import { ObjectView } from '../ObjectView'
import { StoredMessage } from '../../stores/messageStore'

type LogCellProps = {
value: FsdtLogMessageContent
data: StoredMessage
}

const StyledLogCell = styled.div`
Expand All @@ -13,8 +15,24 @@ const StyledLogCell = styled.div`
* {
font-family: ${(props) => props.theme.fontFamily.secondary};
}
display: flex;
align-items: center;
`

export const LogCell = ({ value }: LogCellProps) => {
return <StyledLogCell>{typeof value === 'object' ? <ObjectView value={value} /> : value}</StyledLogCell>
const Quantity = styled.span`
margin-left: 8px;
background-color: ${(props) => props.theme.colors.primary};
color: white;
padding: 2px 4px;
border-radius: 400px;
font-size: 12px;
`

export const LogCell = ({ value, data }: LogCellProps) => {
return (
<StyledLogCell>
{typeof value === 'object' ? <ObjectView value={value} /> : value}
{data.quantity > 1 && <Quantity>{data.quantity}</Quantity>}
</StyledLogCell>
)
}
30 changes: 27 additions & 3 deletions packages/app/src/front/stores/messageStore.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { FsdtLogMessageContent, FsdtServerMessage } from '@fullstack-devtool/core'
import { create } from 'zustand'
import { groupMessagesWithTheSameContent } from '../utils/message'

export type StoredMessage = FsdtServerMessage<FsdtLogMessageContent> & {
quantity: number
}

interface MessageState {
messages: FsdtServerMessage<FsdtLogMessageContent>[]
messages: StoredMessage[]
sources: string[]
tags: string[]
addMessage: (message: FsdtServerMessage<FsdtLogMessageContent>) => void
Expand All @@ -16,6 +21,18 @@ export const useMessageStore = create<MessageState>((set) => ({
tags: [],
addMessage: (message) =>
set((state) => {
// Group messages with the same content
const lastMessage = state.messages[state.messages.length - 1]
if (
lastMessage &&
lastMessage.source === message.source &&
lastMessage.data.content === message.data.content &&
lastMessage.data.tag === message.data.tag
) {
lastMessage.quantity++
return { messages: [...state.messages] }
}
// Add new message
const newSources = [...state.sources]
const newTags = [...state.tags]
if (!newSources.includes(message.source)) {
Expand All @@ -24,7 +41,7 @@ export const useMessageStore = create<MessageState>((set) => ({
if (!newTags.includes(message.data.tag) && message.data.tag) {
newTags.push(message.data.tag)
}
return { messages: [...state.messages, message], sources: newSources, tags: newTags }
return { messages: [...state.messages, { ...message, quantity: 1 }], sources: newSources, tags: newTags }
}),
addMessages: (messages) =>
set((state) => {
Expand All @@ -38,7 +55,14 @@ export const useMessageStore = create<MessageState>((set) => ({
newTags.push(message.data.tag)
}
})
return { messages: [...state.messages, ...messages], sources: newSources, tags: newTags }
return {
messages: groupMessagesWithTheSameContent([
...state.messages,
...messages.map((mess) => ({ ...mess, quantity: 1 })),
]),
sources: newSources,
tags: newTags,
}
}),
clearMessages: () => set({ messages: [] }),
}))
88 changes: 88 additions & 0 deletions packages/app/src/front/utils/message.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { EventType, LogLevel } from '@fullstack-devtool/core'
import { StoredMessage } from '../stores/messageStore'
import { groupMessagesWithTheSameContent } from './message'

describe('message', () => {
describe('groupMessagesWithTheSameContent', () => {
it('should group messages with the same content', () => {
const allMessages: StoredMessage[] = [
{
id: 1,
source: 'source1',
type: EventType.LOG,
data: {
content: 'content1',
tag: 'tag1',
timestamp: '1',
level: LogLevel.LOG,
},
quantity: 1,
},
{
id: 2,
source: 'source1',
type: EventType.LOG,
data: {
content: 'content1',
tag: 'tag1',
timestamp: '2',
level: LogLevel.LOG,
},
quantity: 1,
},
{
id: 3,
source: 'source1',
type: EventType.LOG,
data: {
content: 'content2',
tag: 'tag1',
timestamp: '3',
level: LogLevel.LOG,
},
quantity: 1,
},
{
id: 4,
source: 'source1',
type: EventType.LOG,
data: {
content: 'content2',
tag: 'tag1',
timestamp: '4',
level: LogLevel.LOG,
},
quantity: 1,
},
]
const groupedMessages: StoredMessage[] = [
{
id: 1,
source: 'source1',
type: EventType.LOG,
data: {
content: 'content1',
tag: 'tag1',
timestamp: '1',
level: LogLevel.LOG,
},
quantity: 2,
},
{
id: 3,
source: 'source1',
type: EventType.LOG,
data: {
content: 'content2',
tag: 'tag1',
timestamp: '3',
level: LogLevel.LOG,
},
quantity: 2,
},
]

expect(groupMessagesWithTheSameContent(allMessages)).toEqual(groupedMessages)
})
})
})
19 changes: 19 additions & 0 deletions packages/app/src/front/utils/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { StoredMessage } from '../stores/messageStore'

export const groupMessagesWithTheSameContent = (messages: StoredMessage[]) => {
const groupedMessages: StoredMessage[] = []
messages.forEach((message) => {
const lastMessage = groupedMessages[groupedMessages.length - 1]
if (
lastMessage &&
lastMessage.source === message.source &&
lastMessage.data.content === message.data.content &&
lastMessage.data.tag === message.data.tag
) {
lastMessage.quantity++
} else {
groupedMessages.push({ ...message, quantity: 1 })
}
})
return groupedMessages
}
Loading