I'd like to set up navigation state logging for Se...
# decompose
p
I'd like to set up navigation state logging for Sentry. One of the problems is that I can't think of a way to log the navigation tree - I have a deep tree of nested components. It may be enough to just log the stack being updated, perfectly it would be nice to name the action, like push/pop/pushNew, it could have been done with comparing old/new count, but may not be accurate with replacing multiple items. Another issue is that I often store a model object in the config, and in the log I would only want to include limited info about the object, like object id. Feels like I would need to parse strings manually, as Kotlin serialization is not flexible enough to allow me to override the serialization logic for each object of a specific interface. Any chance someone has already worked on something like this?
a
In it's core, Decompose just manipulates instances of ComponentContext. The concept of a component class with injected ComponentContext is actually just a use case. So yes, it's not possible to traverse the hierarchy. You could implement a custom ComponentContext that would keep track of created and destroyed instances of child ComponentContexts, but you won't have access to the arguments (aka component configurations) and the relation in the parent component (like stack, pages, etc). It looks like the best you can do is: 1. Create a custom AppCoponentContext with a logger property (so that it's available in every component). 2. Create an extension function for Child Stack logging, like:
fun AppComponentContext.log(stack: Value<ChildStack<...>>)
3. Call
log
in the
init
block in a component with stack. You will have to keep track of the old state and manually compare the new with the old state. It's easy to do for common operations like
push
,
pop
,
bringToFront
,
pushToFront
. Excluding some of the arguments is a bit more complicated. Maybe create an interface like:
Copy code
interface Loggable {
    fun logText(): String
}
Then let configuration classes that need custom logging to implement that interface. And in your
log
function check for that interface at runtime and either use
logText
or
toString
. Also, be careful to not leak any user-sensitive information like passwords, etc.
❤️ 1
a
Isnt it possible to add observables on child stack, like rootComponent will have a child stack, then observe the top stack item, and so on...?
a
You can observe the root stack, but in general you can't traverse down the hierarchy. You'd have to switch-case all the components manually, etc.