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

feat(ui): start adding use authenticate calls to the modal #649

Merged
merged 5 commits into from May 10, 2024

Conversation

moldy530
Copy link
Collaborator

@moldy530 moldy530 commented May 6, 2024

Pull Request Checklist


PR-Codex overview

This PR focuses on adding new dependencies, updating UI components, and refactoring file structure in the Alchemy project.

Detailed summary

  • Added new dependencies like @tanstack/react-form and @tanstack/zod-form-adapter
  • Updated UI components like AlchemyLogo, ChevronRight, MailIcon
  • Refactored file structure for better organization

The following files were skipped due to too many changes: packages/alchemy/src/react/icons/hourglass.tsx, packages/alchemy/src/tailwind/components/buttons.ts, examples/ui-demo/env.mjs, packages/alchemy/src/react/components/auth/hooks/usePasskeyVerify.ts, packages/alchemy/src/react/components/AuthModal/AuthSection.tsx, packages/alchemy/src/react/hooks/useAuthenticate.ts, packages/alchemy/src/react/components/auth/card/main.tsx, examples/ui-demo/src/app/providers.tsx, packages/alchemy/src/react/components/button.tsx, examples/ui-demo/src/app/page.tsx, packages/alchemy/src/react/components/auth/card/add-passkey.tsx, packages/alchemy/src/react/components/auth/card/loading/passkey.tsx, packages/alchemy/src/react/components/auth/card/index.tsx, packages/alchemy/src/react/icons/passkey.tsx, packages/alchemy/src/react/components/auth/sections/EmailAuth.tsx, packages/alchemy/src/react/components/auth/card/loading/email.tsx, yarn.lock, packages/alchemy/src/react/context.tsx

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

@moldy530 moldy530 force-pushed the moldy/useAuth-inModal branch 2 times, most recently from 2e6f5ba to 9144aae Compare May 7, 2024 21:28
Copy link
Contributor

@dphilipson dphilipson left a comment

Choose a reason for hiding this comment

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

Looks great! One comment for discussion.

);
},
[header, sections]
);
Copy link
Contributor

@dphilipson dphilipson May 7, 2024

Choose a reason for hiding this comment

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

We may want to write this so that AuthModal is the same instance even if header or sections change. As written, if either of those change, then AuthModal will be a different function instance which will trigger a remount. I can especially see this happening with sections, where someone might end up writing their code in a way that constructs new array instances to pass in on every render.

One way we could write this:

const latestHeader = useLatestRef(header);
const latestSections = useLatestRef(sections);

// Ensure same instance on every render.
const AuthModal = useRef(
  ({ className }: ModalProps) => {
    // ...
    <AuthCard header={latestHeader.current} sections={latestSections.current} />
    // ...
  }
).current;

where useLatestRef does what it sounds like:

function useLatestRef<T>(value: T): MutableRefObject<T> {
  const ref = useRef(value);
  ref.current = value;
  return ref;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

There may be some edge cases where that proposed solution won't work, since changing header or sections wouldn't trigger a re-render in AuthModal. Normally this would be fine since if you're calling useAuthModal chances are you're about re-render some JSX that has the AuthModal in it, but I could imagine some kinds of application structure where you don't use AuthModal immediately and it not re-rendering could be a problem. If you think that's worth worrying about, we probably need some mechanism to send the new values to the AuthModal to trigger a re-render.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh hmmm I actually ran into this not with section and headers since I was expecting these to be mostly static. Even in the tech spec one proposal is to bake this uiConfig into the createConfig function which is supposed to be a static reference.

I ran into this with the input component. If you have the input focused and the input validator generates an error then the whole input component is re-rendered because the error prop is different. This causes the input to lose focus. For Input I need to come back to handle that better, probably with the way you've proposed.

For AuthModal or similar components in other libs, my expectation would be that changes to those props would result in a re-render.

One thing I was thinking about is actually NOT returning the <AuthModal /> component to the dev at all. Instead, I think the hook should inject the modal into the dom (likely using a portal). Then all of these edge cases with re-renders are confined within the hooks implementation and we don't have to make assumptions about external consumers. I think this is how most UI libraries do it normally. What are your thoughts on taking this approach instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dphilipson what do you think about: #655? I've not addressed the re-renders that can happen when ui-config changes, but now at least the rendering of the dialog is done in the same component that receives the props which should match expectations better?

dphilipson
dphilipson previously approved these changes May 10, 2024
Copy link
Contributor

@dphilipson dphilipson left a comment

Choose a reason for hiding this comment

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

With the change in #655 looks great!

@moldy530 moldy530 merged commit ab6bad3 into main May 10, 2024
3 checks passed
@moldy530 moldy530 deleted the moldy/useAuth-inModal branch May 10, 2024 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants