feat: migrate context layer to TypeScript#26
Open
devin-ai-integration[bot] wants to merge 2 commits into
Open
Conversation
- Remove .babelrc and babel dependencies - Add TypeScript, @types/react, @types/react-dom, @types/node as devDependencies - Create tsconfig.json with strict mode enabled - Create types/index.ts with SanityProduct, CartItem, SanityBanner interfaces - Rename context/StateContext.js to context/StateContext.tsx - Define StateContextType interface with all 14 context properties - Type all function parameters (onAdd, onRemove, toggleCartItemQuanitity) - Add proper null checks for foundProduct in onRemove and toggleCartItemQuanitity - useStateContext hook now returns properly typed object with runtime check Co-Authored-By: Wes Convery <2wconvery@gmail.com>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Co-Authored-By: Wes Convery <2wconvery@gmail.com>
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Migrates
context/StateContext.js→context/StateContext.tsxwith full type safety, and sets up the TypeScript foundation for the project.TypeScript setup:
typescript@^6.0.3,@types/react@^17.0.40,@types/react-dom@^17.0.14,@types/nodeas devDependenciestsconfig.jsonwithstrict: trueandallowJs: true(so existing JS files continue to work).babelrcand Babel dependencies (Next.js handles transpilation natively)types/index.tswithSanityProduct,CartItem,SanityBannerinterfacesContext migration:
StateContextTypeinterface covering all 14 context propertiesonAdd,onRemove,toggleCartItemQuanitity)createContext<StateContextType | undefined>(undefined)with a runtime guard inuseStateContextuseStatecalls are now generic-typed (e.g.useState<CartItem[]>([]))Bug fixes introduced during migration (please verify these are desired):
onAdd: the.map()callback now returnscartProductin the non-matching branch (previously returnedundefined, corrupting the cart array)onAddelse branch: no longer mutates the inputproductparameter directlyonAddtoast: now uses thequantityparameter instead of theqtystate variable — the original code would display the wrong number ifonAddwas called with a quantity different from the currentqtystateonRemove/toggleCartItemQuanitity: added null guards forfoundProduct(previously would crash if the item wasn't found)Review & Testing Checklist for Human
.babelrcremoval doesn't break anything: The project relied on@babel/preset-reactvia.babelrc. Next.js 12+ handles JSX transpilation natively (via SWC), but confirm all pages render correctly after this removal.onAddmap fix (context/StateContext.tsx:46): the original.map()silently returnedundefinedfor non-matching items. The new code addsreturn cartProduct;. This changes runtime behavior — confirm cart update works correctly when adding duplicate products.useStateContextruntime guard: The hook now throws if used outside the provider. If any component consumes the context without being wrapped in<StateContext>, it will crash instead of silently returningundefined.@types/reactversion pinning:@types/react@^17.0.40was chosen because versions ≥17.0.80 have anexportsfield that breaks Next.js 12's type detection under modern Node.js. Runningnpm updatecarelessly could re-break the build.Notes
npx tsc --noEmitandnpm run buildboth pass locallyindexvariable intoggleCartItemQuanititywas carried over from the original code (not introduced by this PR)toggleCartItemQuanitity(missing 't' in "Quantity") is preserved from the original to avoid breaking consumersallowJs: truemeans existing.js/.jsxfiles are included but not strictly type-checked — this is intentional for incremental migrationLink to Devin session: https://app.devin.ai/sessions/90b2a71cbd334160925b40a6e5c905a1
Requested by: @WesternConcrete