These are custom React hooks - functions called in the React render phase which obey the rules of hooks and can therefore use other React hooks just like a React component can.
Most custom hooks in this SDK accept one object argument, props
, which is destructured like a component's props and type-checked manually using PropTypes.checkPropTypes()
. This allows developers using the SDK to benefit from browser console mistyping warnings and easy in-code type guidance for hooks, the same as for components.
Be aware that this prop type checking of hook props is a convenience specific to this repo, and not a mandated rule of React Hooks. The completeness of prop types for hooks are therefore not automatially checked by lint like they are for components.
In any hooks file where the argument is props
, a propTypes object can be at the end of the file.
Consumes the output of useStackedData
along with pixel height
and width
dimensions and returns an array of objects containing:
key
: The unique key of theuseStackedData
datum.pathDef
: A string containing a path definition the area shape visualising this datum in a D3 Area Chart. These may be applied to SVG<path>
elements'd
attributes, or a Canvas Path2D constructor.
Calls a given function based on types of available context named in the given array, and caches its result using React's useMemo
, only repeating the function when one of the named contexts changes. Returns the latest result of the function.
Intended for use where a complex, performance-heavy calculation using contexts needs to be specified somewhere outside of React's render pipeline then called inside it.
For given dimensions and optional animation function, returns an object containing:
canvasRef
: auseRef
object to be passed as aref
prop to a<canvas>
elementanimationRef
: auseRef
object which carries status data about the current animation frame and animation transition statusgetCanvasContext
: a function cached with [useCallback
] that returns the specified render context of the<canvas>
element (by default,CanvasRenderingContext2D
)
This is used to assist drawing Canvas elements that persist on re-renders, resize as their width and height changes (e.g. window resizes) and continue animations through re-renders.
Exposes libp2p observer data to the console via the window
object and prints a description of the data available.
Manages the central source of libp2p Introspection data and metadata and returns the current data and updater functions.
The data stored in useDatastore
represents what is held in memory: data removed here will be unretrievable and up for garbage collection. Any filters selected by the user using UI tools are applied outside of this hook (for example, global filters are applied in DataProvider
, before propogating the data store contents as contexts).
An object is returned, containing:
-
states
: An array of states messages, managed byuseReducer
, sorted by end time and with states earlier that the current runtime cutoff time erased. If no data is resent, an empty array is returned. -
events
: An array of states messages, managed withuseReducer
, sorted by event time and with events earlier that the current runtime cutoff time erased. If no data is resent, an empty array is returned. -
runtime
: The currentruntime
message; ornull
-
peerIds
: An array of peerId strings reflecting current user selections -
source
: An object containing metadata on the current data source; ornull
. The object contains:name
: String describing the specific current data source, such as a file's filename or a websocket's URLtype
: String, eithersample
,upload
orlive
indicating one of the three user-selectable options when choosing a data sourceisLoading
: Boolean, true if the data source is not yet fully loaded (for example, while a file is being uploaded or between the initiation and connection of a websocket)
-
websocket
:null
except when there is an active websocket connection, in which case this is an object managed bydispatchWebsocket
viauseReducer
, with keys:ws
: The currentWebSocket
instance.isPaused
: Boolean changing as pause signals are sent.hasData
: Boolean set tofalse
until first data is received over this websocket connection.sendSignal( cmd:string, content:string )
: Function writing and sending a protobuf signal message over the current websocket connection, with the givencmd
and optionalcontent
.
-
setIsLoading( bool )
: A function setting only theisLoading
key of the datasource
-
updateData( { states, events, runtime } )
: A function cached withuseCallback
that updates the runtime if one is provided, appends any provided events and states to the an existing dataset, and processes that data (updates the sort order if needed and removes any states or events that now exceed the runtime's cutoff time). -
replaceData( { states, events, runtime, source } )
: A function cached withuseCallback
that removes the existing dataset and replaces it with the data provided, processing that data asupdateData
does. -
removeData( { source } )
: Clears the current data set. If a source is provided, it is applied (for example, describing a data source that has only just begun to load, wheresource.isLoading = true
), otherwise the data store is left completely empty. -
setPeerIds( peerIds )
: AuseState
setter to set currently selected peerIds to either an array of strings ornull
. -
updateRuntime( runtime )
: Replaces the current runtime message with either the provided message ornull
, then removes any states or events that are now invalid. -
dispatchWebsocket( options )
: AuseReducer
dispatch function managing thewebsocket
object. The suppliedoptions
object must include anaction
string as listed below and the additional properties specified:'onOpen'
action expectsws
WebSocket object andsendSignal
function of the new websocket connection.'onData'
action accepts an optionalcallback
function, which will be called with no arguments passed.'onPauseChange'
action expects booleanisPaused
true if now paused'close'
action closes the websocket passing areason
string and an optionalstatusCode
number (default1000
)
Takes current data from contexts (after any global filters have been applied) and returns a blob of the data encoded into binary protobuf format and written to binary following the file format specification.
Combines any number of user-adjusable data filters into one filter function and allows that filter function to be updated by user actions.
Processes an array of filter definition objects of the following shape:
name
: String, should communicate what this filter does to a userdoFilter( datum, values )
: A function returning true if the given datum should be included after filteringmapFilter(datum)
: An optional data accessor or transformation function; if provided, its return value is passed todoFilter
instead of the original datum.initialValues
: The initial set of user-editablevalues
used indoFilter
. These values may be updated usingdispatchFilters
; typically done by the user using UI form controls.
Returns an object with:
applyFilters(datum)
: A function, used likedataArray.filter(applyFilters)
, which tests the given datum against every filter currentlyenabled
using each such filter's currentvalues
dispatchFilters( options )
: AuseReducer
dispatch function used to update filter values. The suppliedoptions
object must include anaction
string as listed below and the additional properties specified:'update'
action expectsname
string identifying the filter to update and avalues
object to replace the existing filtervalues
'enable'
action expectsname
string identifying the filter to enable'disable'
action expectsname
string identifying the filter to disable'reset'
action expectsname
string identifying the filter to reset to its already-held initial values.
filters
: An array of objects for each filter, including:name
: Matches the original filter definitionvalues
: Object storing the current filter valuesenabled
: Boolean indicating if this filter currently should be included in theapplyFilters
functiongetFilterDef
: Function returning the original filter definition of this filter- Any other properties from the filter definition, such as components to use in the filter's editting UI.
For a given handler
function and targetRef
object from useRef
assigned to a HTML element by passing it to a component as a ref
property, this binds the handler to the HTML element for the given eventType
(default: click
) and unbinds it when the hook is mounted and unbinds it when it is unmounted. An optional className
is also applied and removed (default: clickable
).
This allows an element to have events that are contigent on other elements elsewhere in the DOM tree. For example, a parent element can have a click handler only when it has a certain child and that handler can depend on props or state of the child.
Returns a function, hidePrevious( function )
, that calls (optional) the function passed to this hook and removes or replaces that function with the function passed to this hidePrevious
.
This can be used to ensure only one of a set of any number of different components is shown at a time: call useHidePrevious
in a common parent, and pass the returned hidePrevious
function to each child that should be hidden when another is shown. In those components, call hidePrevious(hide)
in any show
function, where hide
is a function hiding this component, and call hidePrevious(null)
in any show
function.
Organises an array of data
into pools based on one or more continuous variables, counting the number of data points falling into each pool. Typically used for histograms, tallies and similar metrics.
How to pool data is defined in a poolings
object with the following shape:
mapData( datum )
: Optional accessor or data transformation function returning the value eachdatum
is to be pooled bypoolsCount
: Optional number setting the number of pools to use. If unset, a number will be chosen usingd3.ticks
scaleType
: Optional string (defaultlinear
), setting the scaling to be used in assigning pools, from the following options:linear
: Uses d3'sscaleLinear
. Each pool will cover the same range, and the pool boundaries will be chosen as evenly-spaced "nice" round numbers based on the range of provided data.time
: Uses d3'sscaleTime
. Data are treated as timestamps, and each pool will cover the same time duration as withlinear
but the "nicing" of the data will be based on round times (e.g. timestamps that snap to exact minutes or hours)log
: Uses d3'sscaleLog
. Data are treated as simple numbers but pooled on a non-linear log10 scale: for example, pool boundaries might be >=0 && < 0.1, >=0.1 && < 1, >=1 && < 10, >= 10 && < 100...
The poolings
prop may be an object with the above shape, or an array of objects with this shape. If an array is passed, the data within each pool will then be pooled again as specified by the next object in the poolings array.
Each poolings
object generates an array, poolSets
, setting the lower and upper boundaries of each pool. An array of fixed poolSets
may be passed as an optional prop.
The hook returns an object containing:
pooledData
: An array of nested arrays containing the data in each pool. The depth of the nested arrays will match the number ofpoolings
passed in.poolSets
: An array of the pooling boundaries used to pool the data.dispatchPoolings( options )
: AuseReducer
dispatch function used to update the data and pooling settings. The suppliedoptions
object must include anaction
string as listed below and the additional properties specified:set
action: replaces previous poolings with thepoolings
object or array providedremove
action: Removes a pooling by itsindex
in the poolings array.add
action: Appends the providedpoolings
object or objects in the providedpoolings
array to the currentpoolings
arrayedit
action: Applies the providedpoolings
properties, leaving properties undefined in the passedpoolings
unchanged. If an array is passed, poolings update stored poolings at the same index position.
Generates a sorting function and returns it along with a string describing the sorting direction and a function update the sorting direction.
Expected props are:
getSorter( sortDirection:string )
: A function that returns a sorting function for a given direction.mapSorter( datum )
: An optional accessor or data transformation function; if passed, the returned value is passed to the sorter instead of the originaldatum
.defaultDirection
: A string setting the initial direction to sort on first use.directionOptions
: An optional array providing alternative sorting direction keys and labels if the defaultasc
anddesc
are not appropriate.disabled
: A boolean value disabling the sorter iftrue
Returns an object containing:
sorter( a, b )
: A function to be used likedataArray.sort(sorter)
sortDirection: A string matching the currently active item in
directionOptions`- setSortDirection: A
useState
setter that updatessortDirection
and causes a newsorter
to be generated.
Stacks data using d3.stack
, applying scales, keys and updatable sorting, returning an array of arrays of numbers each with a unique key linking it to the original datum and each containing values that include the sum of all previous values according to a given sorting order.
May be used with useAreaChart
to produce stacked area charts.
Expected props are:
data
: An array of data to processgetKeys( data, ySorter, applyFilters )
: A function returning an ordered set of unique key strings from the data arraykeyData( data, keys )
: A function returning an ordered array of objects with a numeric value for each key and a property for the x-axis value.mapYSorter( datum )
: Passed touseSorter
for sorting the Y axismapXSorter( datum )
: Passed touseSorter
for sorting the X axis- Optional properties for
useSorter
:defaultYSortDirection
,getYSorter
,defaultXSortDirection
,getXSorter
Returns an object containing:
stackedData
: An array output fromd3.stack
xScale
: Ad3.scale
object for the X axisyScale
: Ad3.scale
object for the Y axissetXSortDirection
: FromuseSorter
, for sorting the X axissetYSortDirection
: FromuseSorter
, for sorting the Y axis
Organises data based on treating items in a data array as "rows" which may be paginated, and applying a schema of "columns" where each column has a rendering component and optional sorting and filtering rules.
While primarily intended for preparing data for the <DataTable>
component, nothing in this hook is specific to that component or to HTML <table>
elements. It can potentially be used for many visualisation types where different components and sorting rules should be applied to each item in an array of data.
Expected props are:
columns
: An array of objects specifying a column. The keys used by this hook are:name
: A unique string identifying this columnheader
: An optional React node (string or render) labelling the column (name
will be used if not set).getProps( datum, metadata )
: A function returning an object of properties for each datum in thedata
array. If undefined, a default function returns{ value: datum[name] }
renderContent( props )
: A optional React component receiving the props fromgetProps
; a simple component rendering thevalue
prop as a string will be used if not set.sort
: If the column is sortable, an object containing:
data
: An array of objectsdefaultSort
: Name of the column to be used for initial sortingdefaultRange
: optional 2-length array containing the initial number indexes of the first and last items to be included in the initial "pagination" page
Returns an object containing:
columnDefs
:columns
with default values applied toheader
,getProps
andRenderContent
where appropriateallContent
: An array of "row" arrays, sorted by the current sorting selection, where each "row" array contains a props object for each "column" containing:- Each key returned by that column's
getProps
called on this row'sdatum
rowIndex
number referring to the row's position in theallContent
arraycolumnIndex
number referring to the column definition's position in the array of column definitionscolumnName
matching thename
string of this column
- Each key returned by that column's
shownContent
: The slice ofallContent
falling in the currentrange
. Note thatrowIndexes
are not changed so they continue to refer to the row's position inallContent
, allowing a paginated row's original position to be known.sortColumn
: string matching the name of the column the data is currently sorted bysetSortColumn( sortColumn:string )
: function fromuseState
updating thesortColumn
and re-sorting the datasortDirection
: string returned fromuseSorter
setSortDirection
: function returned fromuseSorter
rowCounts
: object containing metadata about the returned rows:total
: number of rows before applyingrange
shown
: number of rows after applyingrange
maxShown
: maximum number of rows that could be shown given the currentrange
showFrom
: index number of the first row that the currentrange
allows to be shown (0
if norange
is applied)
setRange
: function fromuseState
updating the currentrange
resulting in new values forshownContent
androwCounts
.