Unpopular opinion: As a software developer, if you are working in a way where someone interrupting you will cause an hour of wasted time, you’re doing a bad job. [1]
Write stuff down, refactor the code so you never need to do such complicated tasks in the first place, take regular breaks. Start by doing 25/5 pomodoros, and if that makes your work harder, think about why and make the necessary changes.
When I work on complex tasks, I can still make an incremental commit every five to ten minutes, if I want to. If someone interrupts me, I need a minute to get back into it. (ADHD notwithstanding, that can make it harder on a bad brain day.)
Not saying that context switches are unproblematic, but a good setup can make them less terrible.
[1] The prime directive “everyone is doing the best job they can, given the circumstances they are in” applies.