I did the classic NiN itinerary in 2022, in the Oslo-to-Bergen direction. Layovers vary, depending on your direction of travel. I chose to spend a couple of nights in Flam, primarily because I wanted to see the Borgund Stave Church and the public-bus schedule wasn't tourist-friendly. I really liked the NiN trip, but I don't think there's a great need to spend several hours in Flam unless you want to go round-trip between Flam and Myrdal--perhaps to ride a bicycle down the mountain--or do some hiking. Flam itself is not spectacular; it's location is great, but only if there's not a monster ship in port. And you will in any case see a lot of the area during the ferry trip from Gudvangen to Flam and the ride on the Flamsbana. When you cover the entire NiN in one day, you don't have time for a lot of activities along the way.
If you happen to end up with extra time in Flam, the little museum about the building of the railroad is perhaps the best place to spend an hour or so. I think there's a luggage-storage option in the area, but I'm not absolutely sure since I didn't need that service.
I found it easy enough to book the individual components of the NiN trip myself. This is what you would need coming from Bergen (assuming the individual schedules work together as I've laid out the segments):
Mainline train Bergen to Voss; buy tickets from vy.no. You'll probably need to pay with PayPal. PayPal will let you charge the tickets to your credit card. Be sure to conduct the transaction in NOK rather than dollars; otherwise, PayPal will take extra money from you.
Public bus from Voss to Gudvangen; I just bought a ticket from the driver, but perhaps one can now do that online. I'm nearly certain the bus schedule is available on the NorwaysBest website, but here's the Skyss website: Skyss Schedule Look-up. You'll be traveling from Voss Stasjon to Gudvangen ferjekai, Aurland. It's too early now to see June departure times; the bus will run more frequently then than it does in the winter and early spring. The 950 goes right down to the ferry dock; there's also Bus 450 that stops up at highway level. Skyss says it's then a 6-minute walk to the dock, but that should be downhill.
Naeroyfjord ferry from Gudvangen to Flam; buy ticket from NorwaysBest.com.
Flamsbana from Flam up to Myrdal. Buy through ticket from Flam all the way to Oslo from vy.no.
Mainline train from Myrdal to Oslo. See immediately above.
The Vy website says tickets go on sale 90 to 120 days ahead, so it appears you'll need to wait a bit. Transportation cannot sell out when tickets aren't yet on sale. There have been comments on this forum about the trains between Oslo and Myrdal sometimes being full (and I've seen that myself when checking schedules online), and the fare on that segment will increase as the travel date approaches. There have been suggestions that the Flamsbana sometimes sells out, too. Therefore, I think it's important to buy the Flam-Oslo tickets well ahead of time. I don't recall reports of ticket-availability issues with the other parts of the NiN itinerary, but once you're locked in on the Flam-Oslo trains, it makes sense to buy the ferry ticket, too. I think the Bergen-Voss trains can't sell out, but you might as well take care of that ahead of time, too.