I think it's quite well-known that people with dementia can be quite sensitive to transitions - for example, a visitor arriving or leaving. So sometimes when the person's mood suddenly changes, for better or worse, you can look for any change that has recently taken place.
I've realised that this can be used to advantage. S was particularly restless one night and I asked her if she needed the toilet. She is, fortunately, prepared, almost always, to see if she needs the toilet. On this occasion, it turned out that she didn't, but once she had settled back into bed her mood had changed and I was able to fall asleep to her usual happy 'conversations'.
It's as if a change is rebooting the brain.
When she is noticeably happy, I try to avoid much change so as to prolong that state.