The problems occurred when online interaction got mediated by corporations with profit motives that use dark patterns, automated systems and algorithms to extract more revenue from its users.
Most of my real-life friends are people I've first met online, or as a consequence of having met someone online. Those online sites have mostly been run by enthusiasts, driven by some hobby, fandom or other interest. A couple of them have risen highly in popularity and attracted many thousands of users, and also served news and allowed vendors to use their site for interactions with customers.
Those communities that have thrived have made sure that discourse does not get poisoned. They have had active, strong but fair moderators. Many have strict rules against discussing politics or religion, but people have a need to discuss that too sometimes — and being identifiable e.g. between subreddits could put people off from doing that.
Also, where do you draw the line to what is an online community and what is "social media"? I've avoided Facebook and X-twitter, but I know genuine communities exist there too.