Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
04005d6d
Commit
04005d6d
authored
Nov 08, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
socket for txs
parent
5f0e0c0e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
140 additions
and
7 deletions
+140
-7
types.ts
lib/socket/types.ts
+8
-2
useSocketChannel.tsx
lib/socket/useSocketChannel.tsx
+2
-2
useSocketMessage.tsx
lib/socket/useSocketMessage.tsx
+1
-1
TxsNewItemNotice.tsx
ui/txs/TxsNewItemNotice.tsx
+115
-0
TxsTable.tsx
ui/txs/TxsTable.tsx
+5
-1
TxsWithSort.tsx
ui/txs/TxsWithSort.tsx
+9
-1
No files found.
lib/socket/types.ts
View file @
04005d6d
...
@@ -4,9 +4,12 @@ import type { NewBlockSocketResponse } from 'types/api/block';
...
@@ -4,9 +4,12 @@ import type { NewBlockSocketResponse } from 'types/api/block';
export
type
SocketMessageParams
=
SocketMessage
.
NewBlock
|
export
type
SocketMessageParams
=
SocketMessage
.
NewBlock
|
SocketMessage
.
BlocksIndexStatus
|
SocketMessage
.
BlocksIndexStatus
|
SocketMessage
.
TxStatusUpdate
;
SocketMessage
.
TxStatusUpdate
|
SocketMessage
.
NewTx
|
SocketMessage
.
NewPendingTx
|
SocketMessage
.
Unknown
;
interface
SocketMessageParamsGeneric
<
Event
extends
string
,
Payload
extends
object
>
{
interface
SocketMessageParamsGeneric
<
Event
extends
string
|
undefined
,
Payload
extends
object
|
unknown
>
{
channel
:
Channel
|
undefined
;
channel
:
Channel
|
undefined
;
event
:
Event
;
event
:
Event
;
handler
:
(
payload
:
Payload
)
=>
void
;
handler
:
(
payload
:
Payload
)
=>
void
;
...
@@ -17,4 +20,7 @@ export namespace SocketMessage {
...
@@ -17,4 +20,7 @@ export namespace SocketMessage {
export
type
NewBlock
=
SocketMessageParamsGeneric
<
'
new_block
'
,
NewBlockSocketResponse
>
;
export
type
NewBlock
=
SocketMessageParamsGeneric
<
'
new_block
'
,
NewBlockSocketResponse
>
;
export
type
BlocksIndexStatus
=
SocketMessageParamsGeneric
<
'
index_status
'
,
{
finished
:
boolean
;
ratio
:
string
}
>
;
export
type
BlocksIndexStatus
=
SocketMessageParamsGeneric
<
'
index_status
'
,
{
finished
:
boolean
;
ratio
:
string
}
>
;
export
type
TxStatusUpdate
=
SocketMessageParamsGeneric
<
'
collated
'
,
NewBlockSocketResponse
>
;
export
type
TxStatusUpdate
=
SocketMessageParamsGeneric
<
'
collated
'
,
NewBlockSocketResponse
>
;
export
type
NewTx
=
SocketMessageParamsGeneric
<
'
transaction
'
,
{
transaction
:
number
}
>
;
export
type
NewPendingTx
=
SocketMessageParamsGeneric
<
'
pending_transaction
'
,
{
pending_transaction
:
number
}
>
;
export
type
Unknown
=
SocketMessageParamsGeneric
<
undefined
,
unknown
>
;
}
}
lib/socket/useSocketChannel.tsx
View file @
04005d6d
...
@@ -6,7 +6,7 @@ import notEmpty from 'lib/notEmpty';
...
@@ -6,7 +6,7 @@ import notEmpty from 'lib/notEmpty';
import
{
useSocket
}
from
'
./context
'
;
import
{
useSocket
}
from
'
./context
'
;
interface
Params
{
interface
Params
{
topic
:
string
;
topic
:
string
|
undefined
;
params
?:
object
;
params
?:
object
;
isDisabled
:
boolean
;
isDisabled
:
boolean
;
onJoin
?:
(
channel
:
Channel
,
message
:
unknown
)
=>
void
;
onJoin
?:
(
channel
:
Channel
,
message
:
unknown
)
=>
void
;
...
@@ -47,7 +47,7 @@ export default function useSocketChannel({ topic, params, isDisabled, onJoin, on
...
@@ -47,7 +47,7 @@ export default function useSocketChannel({ topic, params, isDisabled, onJoin, on
},
[
channel
,
isDisabled
]);
},
[
channel
,
isDisabled
]);
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
socket
===
null
||
isDisabled
)
{
if
(
socket
===
null
||
isDisabled
||
!
topic
)
{
return
;
return
;
}
}
...
...
lib/socket/useSocketMessage.tsx
View file @
04005d6d
...
@@ -7,7 +7,7 @@ export default function useSocketMessage({ channel, event, handler }: SocketMess
...
@@ -7,7 +7,7 @@ export default function useSocketMessage({ channel, event, handler }: SocketMess
handlerRef
.
current
=
handler
;
handlerRef
.
current
=
handler
;
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
channel
===
undefined
)
{
if
(
channel
===
undefined
||
event
===
undefined
)
{
return
;
return
;
}
}
...
...
ui/txs/TxsNewItemNotice.tsx
0 → 100644
View file @
04005d6d
import
{
Alert
,
Spinner
,
Text
,
Link
,
chakra
}
from
'
@chakra-ui/react
'
;
import
type
{
NextRouter
}
from
'
next/router
'
;
import
{
useRouter
}
from
'
next/router
'
;
import
React
from
'
react
'
;
import
{
ROUTES
}
from
'
lib/link/routes
'
;
import
useSocketChannel
from
'
lib/socket/useSocketChannel
'
;
import
useSocketMessage
from
'
lib/socket/useSocketMessage
'
;
interface
InjectedProps
{
content
:
React
.
ReactNode
;
}
interface
Props
{
children
:
(
props
:
InjectedProps
)
=>
JSX
.
Element
;
className
?:
string
;
}
function
getSocketParams
(
router
:
NextRouter
)
{
if
(
router
.
pathname
===
ROUTES
.
txs
.
pattern
&&
router
.
query
.
tab
===
'
validated
'
&&
!
router
.
query
.
block_number
)
{
return
{
topic
:
'
transactions:new_transaction
'
as
const
,
event
:
'
transaction
'
as
const
};
}
if
(
router
.
pathname
===
ROUTES
.
txs
.
pattern
&&
router
.
query
.
tab
===
'
pending
'
&&
!
router
.
query
.
block_number
)
{
return
{
topic
:
'
transactions:new_pending_transaction
'
as
const
,
event
:
'
pending_transaction
'
as
const
};
}
return
{};
}
function
assertIsNewTxResponse
(
response
:
unknown
):
response
is
{
transaction
:
number
}
{
return
typeof
response
===
'
object
'
&&
response
!==
null
&&
'
transaction
'
in
response
;
}
function
assertIsNewPendingTxResponse
(
response
:
unknown
):
response
is
{
pending_transaction
:
number
}
{
return
typeof
response
===
'
object
'
&&
response
!==
null
&&
'
pending_transaction
'
in
response
;
}
const
TxsNewItemNotice
=
({
children
,
className
}:
Props
)
=>
{
const
router
=
useRouter
();
const
[
num
,
setNum
]
=
React
.
useState
(
0
);
const
[
socketAlert
,
setSocketAlert
]
=
React
.
useState
(
''
);
const
{
topic
,
event
}
=
getSocketParams
(
router
);
const
handleClick
=
React
.
useCallback
(()
=>
{
window
.
location
.
reload
();
},
[]);
const
handleNewTxMessage
=
React
.
useCallback
((
response
:
{
transaction
:
number
}
|
{
pending_transaction
:
number
}
|
unknown
)
=>
{
if
(
assertIsNewTxResponse
(
response
))
{
setNum
((
prev
)
=>
prev
+
response
.
transaction
);
}
if
(
assertIsNewPendingTxResponse
(
response
))
{
setNum
((
prev
)
=>
prev
+
response
.
pending_transaction
);
}
},
[]);
const
handleSocketClose
=
React
.
useCallback
(()
=>
{
setSocketAlert
(
'
Connection is lost. Please click here to load new transactions.
'
);
},
[]);
const
handleSocketError
=
React
.
useCallback
(()
=>
{
setSocketAlert
(
'
An error has occurred while fetching new transactions. Please click here to refresh the page.
'
);
},
[]);
const
channel
=
useSocketChannel
({
topic
,
onSocketClose
:
handleSocketClose
,
onSocketError
:
handleSocketError
,
isDisabled
:
!
topic
,
});
useSocketMessage
({
channel
,
event
,
handler
:
handleNewTxMessage
,
});
if
(
!
topic
&&
!
event
)
{
return
null
;
}
const
content
=
(()
=>
{
if
(
socketAlert
)
{
return
(
<
Alert
className=
{
className
}
status=
"warning"
p=
{
4
}
borderRadius=
{
0
}
onClick=
{
handleClick
}
cursor=
"pointer"
>
{
socketAlert
}
</
Alert
>
);
}
if
(
!
num
)
{
return
null
;
}
return
(
<
Alert
className=
{
className
}
status=
"warning"
p=
{
4
}
borderRadius=
{
0
}
fontWeight=
{
400
}
>
<
Spinner
size=
"sm"
mr=
{
3
}
/>
<
Text
as=
"span"
whiteSpace=
"pre"
>
+
{
num
}
new transaction
{
num
>
1
?
'
s
'
:
''
}
.
</
Text
>
<
Link
onClick=
{
handleClick
}
>
Show in list
</
Link
>
</
Alert
>
);
})();
return
children
({
content
});
};
export
default
chakra
(
TxsNewItemNotice
);
ui/txs/TxsTable.tsx
View file @
04005d6d
import
{
Link
,
Table
,
Tbody
,
Tr
,
Th
,
Icon
}
from
'
@chakra-ui/react
'
;
import
{
Link
,
Table
,
Tbody
,
Tr
,
Th
,
Td
,
Icon
}
from
'
@chakra-ui/react
'
;
import
React
from
'
react
'
;
import
React
from
'
react
'
;
import
type
{
Transaction
}
from
'
types/api/transaction
'
;
import
type
{
Transaction
}
from
'
types/api/transaction
'
;
...
@@ -8,6 +8,7 @@ import appConfig from 'configs/app/config';
...
@@ -8,6 +8,7 @@ import appConfig from 'configs/app/config';
import
rightArrowIcon
from
'
icons/arrows/east.svg
'
;
import
rightArrowIcon
from
'
icons/arrows/east.svg
'
;
import
TheadSticky
from
'
ui/shared/TheadSticky
'
;
import
TheadSticky
from
'
ui/shared/TheadSticky
'
;
import
TxsNewItemNotice
from
'
./TxsNewItemNotice
'
;
import
TxsTableItem
from
'
./TxsTableItem
'
;
import
TxsTableItem
from
'
./TxsTableItem
'
;
type
Props
=
{
type
Props
=
{
...
@@ -46,6 +47,9 @@ const TxsTable = ({ txs, sort, sorting }: Props) => {
...
@@ -46,6 +47,9 @@ const TxsTable = ({ txs, sort, sorting }: Props) => {
</
Tr
>
</
Tr
>
</
TheadSticky
>
</
TheadSticky
>
<
Tbody
>
<
Tbody
>
<
TxsNewItemNotice
>
{
({
content
})
=>
<
Tr
><
Td
colSpan=
{
10
}
p=
{
0
}
>
{
content
}
</
Td
></
Tr
>
}
</
TxsNewItemNotice
>
{
txs
.
map
((
item
)
=>
(
{
txs
.
map
((
item
)
=>
(
<
TxsTableItem
<
TxsTableItem
key=
{
item
.
hash
}
key=
{
item
.
hash
}
...
...
ui/txs/TxsWithSort.tsx
View file @
04005d6d
...
@@ -7,6 +7,7 @@ import type { Sort } from 'types/client/txs-sort';
...
@@ -7,6 +7,7 @@ import type { Sort } from 'types/client/txs-sort';
import
sortTxs
from
'
lib/tx/sortTxs
'
;
import
sortTxs
from
'
lib/tx/sortTxs
'
;
import
TxsListItem
from
'
./TxsListItem
'
;
import
TxsListItem
from
'
./TxsListItem
'
;
import
TxsNewItemNotice
from
'
./TxsNewItemNotice
'
;
import
TxsTable
from
'
./TxsTable
'
;
import
TxsTable
from
'
./TxsTable
'
;
type
Props
=
{
type
Props
=
{
...
@@ -28,7 +29,14 @@ const TxsWithSort = ({
...
@@ -28,7 +29,14 @@ const TxsWithSort = ({
return
(
return
(
<>
<>
<
Show
below=
"lg"
ssr=
{
false
}
><
Box
>
{
sortedTxs
.
map
(
tx
=>
<
TxsListItem
tx=
{
tx
}
key=
{
tx
.
hash
}
/>)
}
</
Box
></
Show
>
<
Show
below=
"lg"
ssr=
{
false
}
>
<
Box
>
<
TxsNewItemNotice
borderRadius=
"md"
>
{
({
content
})
=>
<
Box
>
{
content
}
</
Box
>
}
</
TxsNewItemNotice
>
{
sortedTxs
.
map
(
tx
=>
<
TxsListItem
tx=
{
tx
}
key=
{
tx
.
hash
}
/>)
}
</
Box
>
</
Show
>
<
Hide
below=
"lg"
ssr=
{
false
}
><
TxsTable
txs=
{
sortedTxs
}
sort=
{
sort
}
sorting=
{
sorting
}
/></
Hide
>
<
Hide
below=
"lg"
ssr=
{
false
}
><
TxsTable
txs=
{
sortedTxs
}
sort=
{
sort
}
sorting=
{
sorting
}
/></
Hide
>
</>
</>
);
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment