For the last years I have had flexible office jobs that has allowed me to work at odd hours, less on bad days.
As long as I can somewhat concentrate, working has been an escape and a distraction for me, and it has worked out fairly well. I really think working is good for our mental health.
When I am severely depressed I tend to set everything on flames by trying to do it all and fail spectacularly. Am in such a process at the moment, I don't see myself being able to return to working life anymore.
I don't know what is the best course of action, but I think having good mental health care professional who can help you out with your needs during though times can be vital. And also management that can adjust your workload when needed. Both of these are probably difficult for many though, and having both probably demands pure luck. Will follow this thread to see others opinion on this matter.