From the Manual Sharing section of the document "A Guide to Sharing Architecture"
Manual sharing is removed when the record owner changes or when the
sharing access granted doesn't grant additional access beyond the
object's organization-wide sharing default access ...
When the owner changes, sharing calculations are performed sometime around the final commit, long after your triggers. This is what causes your shares to disappear despite your best efforts. You would need something like a future method or queueable to persist those changes correctly. That said, you might consider using Account Team Members, which tend to ...
You can't use a String as a type name directly. Instead, you must dynamically resolve the type using Type.forName(), and then create a new instance with newInstance():
sObject so = (sObject)Type.forName(shareObj).newInstance();
Note that since you will not have a concrete sObject instance, you'll have to use the put() method to populate fields.
The Type ...
I've just faced a similar issue to share cases created by external users and routed to internal users.
There are Sharing sets in Community Settings. You can play around with that and set up access to Case by Contact matching to community User Contact.
To do so:
Go to Community Settings, sharing sets and create a new one
Select Case as a supported object and ...