If doing the work by yourself, and your are satisfied with the frame, never.
If you replace the whole bike, you have to purchase each and every of its parts in a package. Usually the package is not very good: for example, many manufacturers save money by using non-Shimano parts in a less visible location like wheel hub. Those wheel hubs will be hard to replace -- in fact, replacement of the hub is probably one of the hardest replacement jobs in a bike.
If you replace only a failed part, you have to purchase replacement just for that particular part.
Not only that, but by replacing failed parts, you have the opportunity to customize your bike by selecting which replacement part to purchase. You won't have that opportunity if you purchase a whole bike. Very often, after observing a failure, you have the opportunity to purchase a more durable part to replace the cheap part that failed.
I would argue that purchase of a new bike is a good idea only if its frame can do something your current frame cannot do. For example, some modern frames are asymmetric in the rear to allow even spoke tension in the rear wheel. If rear wheel is what isn't working for you, perhaps this asymmetry could help. Similarly, new frames may support disc brakes whereas old frames probably do not. Also, many new bikes have electric assist whereas old frames cannot accept electric assist motors.
As an example, I bought a new drop bar e-bike that hasn't arrived yet. I am not happy with its component choices. It is not a good bike. It is the least poor e-bike I found. I bought it because my current frame does not accept a Bosch motor, because the frame was designed before the era of motors.