Read "The power of full engagement" Tony Swartz's. It has a very useful video on youtube.
Force yourself to use facebook or read hackernews ONLY when you had finished your work(evening-night), never when you start it(morning). Your short memory is clean after sleeping(it gets backed up in medium and long memory), and you don't want to fill it with pasive stuff but with active info. Creating something(code, making things, writing a book...) takes way more resources than consuming something(reading code, using things, reading a book or what someone has updated on facebook). You should do what is harder first.
Your work environment should be at the right temperature, if it is too hot(now the north hemisphere), your brain shuts down(so you don't generate more heat), if too cold your body wants to move(not good for programming).
Maybe you are in love, who knows.
Don't worry too much, it's normal, the only problem you face is to know consciously what your mind knows unconsciously as the origin of the problem. Once you know, it will be easy to solve.
Bye