AN 10.61 says the food/nutriment (ahara) of the five hindrances (which include sadness & despair) are the three unwholesome types of action.
It follows sadness & despair are actually results of addictive behaviours.
If addictive behaviours are stopped, completely, the sadness & despair will eventually disappear, also, because they are not fed/nourished.
You have already experienced the reality that as quickly as they are felt, they seem to go away very fast, and leave a happy state. This is natural mental purification that occurs from avoidance of addictive behaviours. In other words, nature/dhamma is forgiving.
Buddhism recommends patient endurance, mindfulness & ready-wisdom for such states; to avoid a relapse into addictive behaviours.
Mindfulness means 'remembering'. Mindfulness remembers the ready-wisdom (sampajjana) that addictive behaviours lead to sadness & despair. The Buddha said:
They bring little enjoyment and much suffering and disappointment. The perils in them are greater.
Always remembering the Buddha's advice is recommended. This is the meaning of mindfulness.