Skip to content
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

Don't show dismissible elements until we are sure of their status #5570

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MitjaBezensek
Copy link
Contributor

@MitjaBezensek MitjaBezensek commented Mar 6, 2025

We used to default to "user didn't dismiss" this yet. Which meant that we always showed a dismissible element first, then we read the actual value, and we then might have dismissed the elements (if that's what the stored state dictated). We now have a "not initialized" state during which we don't show the elements.

Side effect of this change is that some users that have already dismissed these elements might see them again. I guess we could add some sort of migration of the state, but meh, doesn't seem worth the hassle?

Change type

  • improvement

Copy link

linear bot commented Mar 6, 2025

Copy link

vercel bot commented Mar 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
examples ✅ Ready (Inspect) Visit Preview Mar 6, 2025 4:19pm
tldraw-docs ✅ Ready (Inspect) Visit Preview Mar 6, 2025 4:19pm

@@ -63,7 +63,9 @@ function CookieConsent({
}
}

if (hasConsent !== 'unknown') return null
if (hasConsent === 'not-initialized' || hasConsent !== 'unknown') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this extra logic of not-initialized get us anything here?
'not-initialized' !== 'unknown' so you can just leave this line as:
if (hasConsent !== 'unknown') return null

but then, why do we add not-initialized? :) seems like we might not need the extra complexity here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, we can drop the additional check.

We do need it tough. Go the https://tldraw.dev/, accept the cookies, then to a hard reload. You'll see the consent for a split second.

* @public
*/
export function useLocalStorageState<T = any>(key: string, defaultValue: T, initialValue?: T) {
const [state, setState] = useState(initialValue ?? defaultValue)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think something about overloading the initialized/not-initialized state makes this code hard to reason about. The issue in my head is that it starts with the initialValue but then the developer is supposed to notice that the defaultValue overrides it shortly thereafter in the useLayoutEffect.

i think it feels a little too complex/leaky to the caller for what this hook is doing.

i propose maybe making a separate state: [isInitialized, isNotInitialized]

and then this hook would return:

const [surveyStatus, setSurveyStatus, isInitialized] = useLocalStorageState<SurveyStatus>(
		'devrel-survey',
		'not-dismissed',
	)

or alternatively:

const {surveyStatus, setSurveyStatus, isInitialized}

I think that would help us avoid any of the not-initialized values maybe accidentally getting stored or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can take a look on how to improve the api of this. It feels like we definitely need to know if we read the value from the storage or not yet. We can't default to just showing the element, since that causes the issue.

Will probably take this up when I'm back from vacation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Yeah, I'm just pushing on splitting it out from the enum of possible values so that the state of being loaded/not-loaded isn't tied to what is actually expected in the cookie value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Product improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants