feat: read requireAnalyticsOptIn setting from visual editor config#1059
feat: read requireAnalyticsOptIn setting from visual editor config#1059jwartofsky-yext merged 2 commits intomainfrom
Conversation
WalkthroughThis change reads Sequence Diagram(s)sequenceDiagram
participant Browser
participant DocumentGlobal as document.__
participant Template as (directory|locator|main).tsx
participant AnalyticsProvider
Browser->>Template: render template
Template->>DocumentGlobal: read visualEditorConfig (string?)
alt config present
Template->>Template: JSON.parse(config) (try/catch)
alt parse success
Template->>Template: extract requireAnalyticsOptIn (default false)
else parse error
Template->>Browser: console.error(...)
Template->>Template: set requireAnalyticsOptIn = false
end
else config missing
Template->>Template: set requireAnalyticsOptIn = false
end
Template->>AnalyticsProvider: render with requireOptIn=requireAnalyticsOptIn
AnalyticsProvider->>AnalyticsProvider: use requireOptIn for analytics behavior
Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (2)
packages/visual-editor/src/vite-plugin/templates/locator.tsx (1)
149-153:⚠️ Potential issue | 🟠 MajorSame unguarded
JSON.parsecrash risk as indirectory.tsx.Identical issue:
JSON.parse(document.__.visualEditorConfig)is called inline without a try/catch, and the?.requireAnalyticsOptInchain can yieldundefinedinstead offalse. Apply the same fix described fordirectory.tsx.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/visual-editor/src/vite-plugin/templates/locator.tsx` around lines 149 - 153, The inline JSON.parse call for document.__.visualEditorConfig used to compute requireOptIn in locator.tsx is unguarded and can throw or return undefined; wrap the parse in a safe try/catch (or reuse the same safe parser/helper used in directory.tsx) and explicitly default requireAnalyticsOptIn to false on any parse error or missing value so requireOptIn always becomes a boolean; update the expression that sets requireOptIn (referencing requireOptIn and document.__.visualEditorConfig) to call the safe parser and return parsed.requireAnalyticsOptIn ?? false.packages/visual-editor/src/vite-plugin/templates/main.tsx (1)
147-151:⚠️ Potential issue | 🟠 MajorSame unguarded
JSON.parsecrash risk as indirectory.tsx.Identical issue:
JSON.parse(document.__.visualEditorConfig)is called inline without a try/catch, and the?.requireAnalyticsOptInchain can yieldundefinedinstead offalse. Apply the same fix described fordirectory.tsx.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/visual-editor/src/vite-plugin/templates/main.tsx` around lines 147 - 151, The inline JSON.parse call on document.__.visualEditorConfig used to set the requireOptIn prop can throw and also yields undefined instead of a boolean; update the code that computes requireOptIn (the expression referencing document.__.visualEditorConfig and requireOptIn) to parse safely inside a try/catch (or use a small safeParse helper), default to an empty object on parse failure, and then read .requireAnalyticsOptIn with a fallback to false so requireOptIn always becomes a boolean.
🧹 Nitpick comments (1)
packages/visual-editor/src/vite-plugin/templates/directory.tsx (1)
137-141: Consider extracting a shared helper for this duplicated parsing logic.The identical
JSON.parse(visualEditorConfig)?.requireAnalyticsOptInpattern is copy-pasted across all three template files. Since these are autogenerated, the fix belongs in the generator — a shared utility (e.g.,getRequireOptIn(document)) would centralise the logic and error handling in one place.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/visual-editor/src/vite-plugin/templates/directory.tsx` around lines 137 - 141, The duplicated JSON.parse(...) logic used to derive requireOptIn from document.__.visualEditorConfig should be extracted into a shared helper (e.g., getRequireOptIn(document)) in the generator/runtime so templates reuse it; implement getRequireOptIn to safely access document.__.visualEditorConfig, try/catch JSON.parse, return a boolean (default false), and replace the inline expression in templates like the requireOptIn prop in directory.tsx with a call to getRequireOptIn(document).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/visual-editor/src/vite-plugin/templates/directory.tsx`:
- Around line 137-141: The JSX currently calls JSON.parse on
document.__.visualEditorConfig without error handling and uses optional chaining
that can yield undefined; update the logic that computes the requireOptIn value
(the expression using document.__.visualEditorConfig and requireAnalyticsOptIn)
to first read the raw string, then try/catch JSON.parse to avoid throwing during
render, and coerce the final value with ?? false so it always supplies a boolean
to the requireOptIn prop; modify the code around the requireOptIn expression in
templates/directory.tsx accordingly (use a local const before the return that
falls back to false on parse error or missing key).
---
Duplicate comments:
In `@packages/visual-editor/src/vite-plugin/templates/locator.tsx`:
- Around line 149-153: The inline JSON.parse call for
document.__.visualEditorConfig used to compute requireOptIn in locator.tsx is
unguarded and can throw or return undefined; wrap the parse in a safe try/catch
(or reuse the same safe parser/helper used in directory.tsx) and explicitly
default requireAnalyticsOptIn to false on any parse error or missing value so
requireOptIn always becomes a boolean; update the expression that sets
requireOptIn (referencing requireOptIn and document.__.visualEditorConfig) to
call the safe parser and return parsed.requireAnalyticsOptIn ?? false.
In `@packages/visual-editor/src/vite-plugin/templates/main.tsx`:
- Around line 147-151: The inline JSON.parse call on
document.__.visualEditorConfig used to set the requireOptIn prop can throw and
also yields undefined instead of a boolean; update the code that computes
requireOptIn (the expression referencing document.__.visualEditorConfig and
requireOptIn) to parse safely inside a try/catch (or use a small safeParse
helper), default to an empty object on parse failure, and then read
.requireAnalyticsOptIn with a fallback to false so requireOptIn always becomes a
boolean.
---
Nitpick comments:
In `@packages/visual-editor/src/vite-plugin/templates/directory.tsx`:
- Around line 137-141: The duplicated JSON.parse(...) logic used to derive
requireOptIn from document.__.visualEditorConfig should be extracted into a
shared helper (e.g., getRequireOptIn(document)) in the generator/runtime so
templates reuse it; implement getRequireOptIn to safely access
document.__.visualEditorConfig, try/catch JSON.parse, return a boolean (default
false), and replace the inline expression in templates like the requireOptIn
prop in directory.tsx with a call to getRequireOptIn(document).
ℹ️ Review info
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/visual-editor/src/vite-plugin/templates/directory.tsxpackages/visual-editor/src/vite-plugin/templates/locator.tsxpackages/visual-editor/src/vite-plugin/templates/main.tsx
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/visual-editor/src/vite-plugin/templates/main.tsx (1)
142-151: Consider explicit boolean coercion and consistent optional chaining.Two minor points:
Type safety –
JSON.parse(...)?. requireAnalyticsOptIn ?? falseproducesany(becauseJSON.parsereturnsany). TypeScript silently allows assigninganyto the inferred-booleanvariable, so a mis-typed JSON value (e.g.1,"true") would propagate unchecked to therequireOptInprop. Coerce explicitly to boolean.Optional-chaining consistency – Line 143 uses
document.__?.visualEditorConfigbut line 146 usesdocument.__.visualEditorConfig. The guard makes the inner access safe, but the inconsistency is unnecessary noise.♻️ Proposed fix
let requireAnalyticsOptIn = false; if (document.__?.visualEditorConfig) { try { requireAnalyticsOptIn = - JSON.parse(document.__.visualEditorConfig)?.requireAnalyticsOptIn ?? - false; + Boolean(JSON.parse(document.__?.visualEditorConfig)?.requireAnalyticsOptIn); } catch (e) { console.error("Failed to parse visualEditorConfig JSON:", e); } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/visual-editor/src/vite-plugin/templates/main.tsx` around lines 142 - 151, The code that reads document.__?.visualEditorConfig into requireAnalyticsOptIn should (1) use consistent optional chaining for both checks (e.g., reference document.__?.visualEditorConfig in both places) and (2) explicitly coerce the parsed value to a boolean instead of trusting JSON.parse's any; update the block around requireAnalyticsOptIn to parse the config into a local variable (using document.__?.visualEditorConfig), then set requireAnalyticsOptIn = Boolean(parsed?.requireAnalyticsOptIn) or an explicit comparison (e.g., parsed?.requireAnalyticsOptIn === true) to ensure a strict boolean is assigned.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/visual-editor/src/vite-plugin/templates/main.tsx`:
- Around line 142-151: The code that reads document.__?.visualEditorConfig into
requireAnalyticsOptIn should (1) use consistent optional chaining for both
checks (e.g., reference document.__?.visualEditorConfig in both places) and (2)
explicitly coerce the parsed value to a boolean instead of trusting JSON.parse's
any; update the block around requireAnalyticsOptIn to parse the config into a
local variable (using document.__?.visualEditorConfig), then set
requireAnalyticsOptIn = Boolean(parsed?.requireAnalyticsOptIn) or an explicit
comparison (e.g., parsed?.requireAnalyticsOptIn === true) to ensure a strict
boolean is assigned.
ℹ️ Review info
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/visual-editor/src/vite-plugin/templates/directory.tsxpackages/visual-editor/src/vite-plugin/templates/locator.tsxpackages/visual-editor/src/vite-plugin/templates/main.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/visual-editor/src/vite-plugin/templates/directory.tsx
- packages/visual-editor/src/vite-plugin/templates/locator.tsx
commit: |
No description provided.