Skip to content

Commit

Permalink
Merge pull request #55 from TheNexusCity/issue-7
Browse files Browse the repository at this point in the history
Added the Event Manager Window
  • Loading branch information
alextitonis authored May 2, 2022
2 parents 5401077 + 277964a commit 7edc1d4
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 1 deletion.
2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"react-scroll-to-bottom": "^4.2.0",
"react-select": "^4.3.1",
"react-simple-code-editor": "^0.11.0",
"react-table": "7.7.0",
"rebass": "^4.0.7",
"reconnecting-websocket": "^4.4.0",
"redux": "^4.1.1",
Expand Down Expand Up @@ -101,6 +102,7 @@
"@types/react": "^17.0.15",
"@types/react-dom": "^17.0.9",
"@types/react-redux": "^7.1.18",
"@types/react-table": "7.7.9",
"@welldone-software/why-did-you-render": "^6.2.1",
"babel-loader": "^8.2.2",
"babel-plugin-transform-class-properties": "^6.24.1",
Expand Down
9 changes: 9 additions & 0 deletions client/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,12 @@ input[type='text'] {
border-radius: 0.4rem;
margin: 0 0.5rem;
}

.bare-input {
padding: 0;
margin: 0;
border: 0;
background: transparent;
outline: none;
font-size: 0.975rem;
}
9 changes: 9 additions & 0 deletions client/src/components/MenuBar/MenuBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const MenuBar = () => {
$CREATE_SEARCH_CORPUS,
$CREATE_TEXT_EDITOR,
$CREATE_CONSOLE,
$CREATE_EVENT_MANAGER,
$SERIALIZE,
$EXPORT,
$UNDO,
Expand Down Expand Up @@ -122,6 +123,11 @@ const MenuBar = () => {
publish($CREATE_CONSOLE(activeTabRef.current.id))
}

const onEventManagerCreate = () => {
if (!activeTabRef.current) return
publish($CREATE_EVENT_MANAGER(activeTabRef.current.id))
}

//Menu bar hotkeys
useHotkeys(
'cmd+s, crtl+s',
Expand Down Expand Up @@ -224,6 +230,9 @@ const MenuBar = () => {
console: {
onClick: onConsole,
},
event_manager: {
onClick: onEventManagerCreate,
},
},
},
}
Expand Down
1 change: 1 addition & 0 deletions client/src/contexts/PubSubProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const events = {
$CREATE_SEARCH_CORPUS: tabId => `createSearchCorpus:${tabId}`,
$CREATE_DEBUG_CONSOLE: tabId => `createDebugConsole:${tabId}`,
$CREATE_CONSOLE: tabId => `createDebugConsole:${tabId}`,
$CREATE_EVENT_MANAGER: tabId => `createEventManager:${tabId}`,
$SERIALIZE: tabId => `serialize:${tabId}`,
$PROCESS: tabId => `process:${tabId}`,
$EXPORT: tabId => `export:${tabId}`,
Expand Down
6 changes: 6 additions & 0 deletions client/src/data/layouts/defaultLayout.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
"id": "#16",
"name": "Search Corpus",
"component": "searchCorpus"
},
{
"type": "tab",
"id": "#19",
"name": "Event Manager",
"component": "eventManager"
}
]
},
Expand Down
6 changes: 6 additions & 0 deletions client/src/screens/Thoth/components/EventHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const EventHandler = ({ pubSub, tab }) => {
$CREATE_PLAYTEST,
$CREATE_INSPECTOR,
$CREATE_CONSOLE,
$CREATE_EVENT_MANAGER,
$CREATE_TEXT_EDITOR,
$SERIALIZE,
$EXPORT,
Expand Down Expand Up @@ -150,6 +151,10 @@ const EventHandler = ({ pubSub, tab }) => {
const createConsole = () => {
createOrFocus(windowTypes.CONSOLE, 'Console')
}

const createEventManager = () => {
createOrFocus(windowTypes.EVENT_MANAGER, 'Event Manager')
}

const onSerialize = () => {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -217,6 +222,7 @@ const EventHandler = ({ pubSub, tab }) => {
[$CREATE_INSPECTOR(tab.id)]: createInspector,
[$CREATE_TEXT_EDITOR(tab.id)]: createTextEditor,
[$CREATE_CONSOLE(tab.id)]: createConsole,
[$CREATE_EVENT_MANAGER(tab.id)]: createEventManager,
[$SERIALIZE(tab.id)]: onSerialize,
[$EXPORT(tab.id)]: onExport,
[$CLOSE_EDITOR(tab.id)]: onCloseEditor,
Expand Down
3 changes: 3 additions & 0 deletions client/src/workspaces/spells/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { sharedb } from '@/config'
import SearchCorpus from './windows/SearchCorpusWindow'
import EntityManagerWindow from './windows/EntityManagerWindow'
import { diff } from '@/utils/json0'
import EventManagerWindow from './windows/EventManager'

const Workspace = ({ tab, tabs, pubSub }) => {
const spellRef = useRef<Spell>()
Expand Down Expand Up @@ -131,6 +132,8 @@ const Workspace = ({ tab, tabs, pubSub }) => {
return <SearchCorpus />
case 'entityManager':
return <EntityManagerWindow />
case 'eventManager':
return <EventManagerWindow />
default:
return <p></p>
}
Expand Down
202 changes: 202 additions & 0 deletions client/src/workspaces/spells/windows/EventManager/EventTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// @ts-nocheck
import { useEffect, useMemo, useState } from 'react'
import { useAsyncDebounce, useGlobalFilter, usePagination, useSortBy, useTable } from 'react-table'
import {
TableContainer,
Table,
TableHead,
TableBody,
TableRow,
TableCell,
Paper,
Pagination,
Stack,
IconButton,
} from '@mui/material'
import { VscArrowDown, VscArrowUp, VscTrash } from 'react-icons/vsc'
import { useSnackbar } from 'notistack'
import axios from 'axios'
import _ from 'lodash'

const GlobalFilter = ({ globalFilter, setGlobalFilter }) => {
const [value, setValue] = useState(globalFilter)
const onChange = useAsyncDebounce((value) => {
setGlobalFilter(value || undefined)
}, 500)
return (
<input
type='text'
value={value || ''}
onChange={e => {
setValue(e.target.value)
onChange(e.target.value)
}}
placeholder='Search events...'
style={{ width: '30%' }}
/>
)
}

function EventTable({ events, updateCallback }) {
const { enqueueSnackbar } = useSnackbar()

const columns = useMemo(() => [
{
Header: 'Agent',
accessor: 'agent'
},
{
Header: 'Client',
accessor: 'client'
},
{
Header: 'Sender',
accessor: 'sender'
},
{
Header: 'Text',
accessor: 'text'
},
{
Header: 'Type',
accessor: 'type'
},
{
Header: 'Channel',
accessor: 'channel'
},
{
Header: 'Date',
accessor: 'date'
},
{
Header: 'Actions',
Cell: row => (
<IconButton onClick={() => handleEventDelete(row.row.original)}>
<VscTrash size={16} color='#ffffff'/>
</IconButton>
)
},
], [])

const updateEvent = async ({ id, ...rowData }, columnId, value) => {
let reqBody = {
...rowData,
[columnId]: value
}
if(!_.isEqual(reqBody, rowData)) {
const isUpdated = await axios.put(`${process.env.REACT_APP_API_ROOT_URL}/event/${id}`, reqBody)
if(isUpdated) enqueueSnackbar('Event updated', { variant: 'success' })
else enqueueSnackbar('Error updating event', { variant: 'error' })
updateCallback()
}
}

const EditableCell = ({ value, row: { original: row }, column: { id }, updateEvent }) => {
const [val, setVal] = useState(value)
const onChange = (e) => setVal(e.target.value)
const onBlur = (e) => updateEvent(row, id, val)
useEffect(() => setVal(value), [value])
return <input
value={val}
onChange={onChange}
onBlur={onBlur}
className='bare-input'
/>
}

const defaultColumn = {
Cell: EditableCell
}

const {
getTableProps,
getTableBodyProps,
headerGroups,
page,
prepareRow,
pageOptions,
gotoPage,
setGlobalFilter,
state
} = useTable(
{
columns,
data: events,
defaultColumn,
updateEvent
},
useGlobalFilter,
useSortBy,
usePagination
)

const handlePageChange = (page: number) => {
const pageIndex = page - 1
gotoPage(pageIndex)
}

const handleEventDelete = async (event: any) => {
console.log('event to delete ::: ', event);
const isDeleted = await axios.delete(`${process.env.REACT_APP_API_ROOT_URL}/event/${event.id}`)
if(isDeleted) enqueueSnackbar('Event deleted', { variant: 'success' })
else enqueueSnackbar('Error deleting Event', { variant: 'error' })
updateCallback()
}

return (
<Stack spacing={2}>
<GlobalFilter
globalFilter={state.globalFilter}
setGlobalFilter={setGlobalFilter}
/>
<TableContainer component={Paper}>
<Table {...getTableProps()}>
<TableHead style={{ backgroundColor: '#000'}}>
{headerGroups.map((headerGroup, idx) => (
<TableRow {...headerGroup.getHeaderGroupProps()} key={idx}>
{headerGroup.headers.map((column, idx) => (
<TableCell
{...column.getHeaderProps(column.getSortByToggleProps())}
style={{fontSize: '0.985rem'}}
key={idx}
>
{column.render('Header')}{' '}
<span>
{column.isSorted ?
column.isSortedDesc ?
<VscArrowDown size={14} />
: <VscArrowUp size={14} />
: ''}
</span>
</TableCell>
))}
</TableRow>
))}
</TableHead>
<TableBody {...getTableBodyProps()}>
{page.map((row: Row<object>, idx: number) => {
prepareRow(row)
return (
<TableRow {...row.getRowProps()} key={idx}>
{row.cells.map((cell, idx) => (
<TableCell {...cell.getCellProps} key={idx}>{cell.render('Cell')}</TableCell>
))}
</TableRow>
)
})}
</TableBody>
</Table>
</TableContainer>
<Pagination
count={pageOptions.length}
onChange={(e, page) => handlePageChange(page)}
shape='rounded'
showFirstButton
showLastButton
/>
</Stack>
)
}

export default EventTable
31 changes: 31 additions & 0 deletions client/src/workspaces/spells/windows/EventManager/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-nocheck
import { useEffect, useState } from 'react'
import axios from 'axios'
import EventTable from './EventTable'

const EventManagerWindow = () => {
const [events, setEvents] = useState(null)

useEffect(() => {
fetchEvents()
}, [])

const resetEvents = async () => {
await fetchEvents()
}

const fetchEvents = async () => {
const { data } = await axios.get(`${process.env.REACT_APP_API_ROOT_URL}/events`);
setEvents(data)
}

return (
<div className='agent-container'>
{events && (
<EventTable events={events} updateCallback={resetEvents}/>
)}
</div>
)
}

export default EventManagerWindow
Loading

0 comments on commit 7edc1d4

Please sign in to comment.