Housing is way cheaper than treatment and a lot of research has shown that if you provide homeless housing, their overall utilization of expensive forms of healthcare (inpatient treatment, ER visits) goes way down.
It's always been a balancing act in regards to the rights of the mentally ill. We used to throw anyone who was labelled "a problem" or "deviant" into insane asylums, including for reasons like a person being gay. It was very difficult and sometimes impossible for these people to escape these institutions, and the 1950s/60s was notorious for their shitty treatment of the mentally ill in institutions. Interestingly enough, around 1890 ish the mentally ill were far better off as they were shipped to farms etc and helped out with manual labor, and were generally treated much better than they were during the 1950s/60s.
Imo the laws are fine as they are now, if you care about what the civil commitment standards are by state they are here:
https://www.treatmentadvocacycenter....Procedures.pdf What should happen is that once a person's behavior becomes a risk to themselves or others then they are involuntarily committed, then if necessary placed under a conservatorship with the state or with a relative, then the individual is placed in the care of that relative or placed in a group home or similar facility.
Really the reason why the homeless are homeless is that no one wants to pay to house them, about 40% of all homeless in the US are on disability and about
half have a serious mental health or substance abuse issue, or both.