I attempted suicide in undergrad. In the same year, I was hospitalised once for a mixed state, once for depression, and once for mania. It was a pretty shitty year.
I graduated because I took time off. I was not in the headspace (nor had the cognitive function) to study. But taking that time off was vital. I got used to the medications I was on, they started to work, and I used the limited function I had to learn how to manage my illness and look for warning patterns.
I've finished the first year of my PhD and despite what everyone says about grad school, I am healthier than I ever was in undergrad. I attribute that the time I took off several years ago. Managing mental illness is hard. Managing it while struggling in school is harder.
If taking time off isn't possible, get yourself in to see professionals. Sleep at the same time each night if you can, and wake up at the same time each morning. Drag yourself on a short walk each day. Fill the fridge with healthy, low-prep food for you to eat (carrots and hummus are good). Figure out the disabilities office at your school and get accommodations. See friends. There are online chats and hotlines to call if you need someone to break you out of the cycle of ideation.