-
Notifications
You must be signed in to change notification settings - Fork 0
Fix QR code verification error #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| 'use client'; | ||
|
|
||
| import { useState, useEffect } from 'react'; | ||
| import { use, useState, useEffect } from 'react'; | ||
| import Link from 'next/link'; | ||
| import Image from 'next/image'; | ||
|
|
||
|
|
@@ -16,18 +16,19 @@ interface VerificationData { | |
| verified: boolean; | ||
| } | ||
|
|
||
| export default function VerifyPage({ params }: { params: { hashId: string } }) { | ||
| export default function VerifyPage({ params }: { params: Promise<{ hashId: string }> }) { | ||
| const { hashId } = use(params); | ||
| const [data, setData] = useState<VerificationData | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(''); | ||
|
|
||
| useEffect(() => { | ||
| fetchVerificationData(); | ||
| }, [params.hashId]); | ||
| }, [hashId]); | ||
|
Comment on lines
24
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While the current implementation works, it doesn't fully align with the rules of hooks, which can make the code more fragile. The To address this without causing an infinite re-render loop, you can wrap Here's an example of how you could refactor it: const fetchVerificationData = useCallback(async () => {
// ... fetch logic using hashId
}, [hashId]);
useEffect(() => {
fetchVerificationData();
}, [fetchVerificationData]);This change would make your component more robust and strictly follow React's best practices for handling side effects. |
||
|
|
||
| const fetchVerificationData = async () => { | ||
| try { | ||
| const response = await fetch(`/api/verify/${params.hashId}`); | ||
| const response = await fetch(`/api/verify/${hashId}`); | ||
| const result = await response.json(); | ||
|
|
||
| if (result.success) { | ||
|
|
@@ -154,17 +155,30 @@ export default function VerifyPage({ params }: { params: { hashId: string } }) { | |
| <h3 className="text-sm font-semibold text-blue-300 mb-1"> | ||
| Environmental Entropy | ||
| </h3> | ||
| <div className="text-sm text-blue-200"> | ||
| {data?.entropyMetadata?.weather && ( | ||
| <div className="text-sm text-blue-200 space-y-1"> | ||
| {data?.entropyMetadata?.weather ? ( | ||
| <p> | ||
| Weather: {data.entropyMetadata.weather.temperature}°C,{' '} | ||
| {data.entropyMetadata.weather.conditions} | ||
| </p> | ||
| )} | ||
| {data?.entropyMetadata?.location && ( | ||
| <p> | ||
| Location: {data.entropyMetadata.location.city},{' '} | ||
| {data.entropyMetadata.location.country} | ||
| ) : null} | ||
| {data?.entropyMetadata?.location ? ( | ||
| <> | ||
| <p> | ||
| Location: {data.entropyMetadata.location.city} | ||
| {data.entropyMetadata.location.country && `, ${data.entropyMetadata.location.country}`} | ||
| </p> | ||
| {data.entropyMetadata.location.coordinates && ( | ||
| <p className="text-slate-400 text-xs"> | ||
| Approx. coordinates: {Math.floor(data.entropyMetadata.location.coordinates.lat)}°, {Math.floor(data.entropyMetadata.location.coordinates.lon)}° | ||
| <span className="italic"> (truncated for privacy - precise coordinates used in hash)</span> | ||
| </p> | ||
| )} | ||
| </> | ||
| ) : null} | ||
| {!data?.entropyMetadata?.weather && !data?.entropyMetadata?.location && ( | ||
| <p className="text-slate-400 italic"> | ||
| No environmental data collected for this hash | ||
| </p> | ||
| )} | ||
| </div> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The verify page is a
'use client'component, so itsparamsprop is sent from the server as a fully resolved, serializable object. Changing the signature toparams: Promise<{ hashId: string }>and callingconst { hashId } = use(params);passes that plain object into React’suse()hook, which immediately throwsInvalid use() callbecauseuse()only accepts promises/contexts. As a result/verify/[hashId]now crashes before issuing the API request. Keepparamstyped as{ hashId: string }and read the value directly instead of callinguse().Useful? React with 👍 / 👎.