Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions snippets/calculator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ export const PricingCalculator = () => {
const prevPriceRef = useRef(null);
const hasInteracted = useRef(false);

useEffect(() => {
// Read after hydration so the server and first client render agree.
if (hasInteracted.current) return;
const params = new URLSearchParams(window.location.search);
const readCount = (key, fallback) => {
const value = params.get(key);
if (value === null || !/^\d+$/.test(value)) return fallback;
const count = Number(value);
return Number.isSafeInteger(count) ? count : fallback;
};
const requestedPlan = params.get('plan');
const requestedBrowserType = params.get('browserType');
const restoredBrowserType = ['headless', 'headful', 'gpu'].includes(requestedBrowserType)
? requestedBrowserType : defaults.browserType;
let restoredPlan = ['free', 'hobbyist', 'startup', 'enterprise'].includes(requestedPlan)
? requestedPlan : defaults.plan;
if (restoredBrowserType === 'gpu' && restoredPlan !== 'startup' && restoredPlan !== 'enterprise') {
restoredPlan = 'startup';
}
setPlan(restoredPlan);
setBrowserType(restoredBrowserType);
setAvgSessionLength(readCount('duration', defaults.avgSessionLength));
setNumSessions(readCount('sessions', defaults.numSessions));
// Restoration isn't an interaction: leave the incoming URL untouched.
}, []);

useEffect(() => {
if (!hasInteracted.current) return;
var url = new URL(window.location);
Expand Down
Loading