Skip to content

Commit

Permalink
avoid constantly updating UsernameInput props (does nothing)
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko committed Aug 27, 2024
1 parent 4df2392 commit 799fbdf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 8 additions & 1 deletion resources/js/chat/create-announcement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type Props = Record<string, never>;

@observer
export default class CreateAnnouncement extends React.Component<Props> {
private usernameInputInitialProps: typeof this.model.propsForUsernameInput = {
// dummy value so the field isn't nullablel runInAction in contructor doesn't count as initialized.
initialUsers: [],
initialValue: '',
};

@computed
private get canSend() {
return core.dataStore.chatState.isReady && !core.dataStore.chatState.isAddingChannel && this.model.isValid;
Expand All @@ -34,6 +40,7 @@ export default class CreateAnnouncement extends React.Component<Props> {

runInAction(() => {
this.model.initialize();
this.usernameInputInitialProps = this.model.propsForUsernameInput;
});
}

Expand Down Expand Up @@ -85,7 +92,7 @@ export default class CreateAnnouncement extends React.Component<Props> {
onBlur={this.handleBlur}
onValidUsersChanged={this.handleValidUsersChanged}
onValueChanged={this.handleUsernameInputValueChanged}
{...this.model.propsForUsernameInput}
{...this.usernameInputInitialProps}
/>
</div>
</InputContainer>
Expand Down
4 changes: 3 additions & 1 deletion resources/js/components/username-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export default class UsernameInput extends React.PureComponent<Props> {
@observable private readonly validUsers = new Map<number, UserJson>();
private xhr?: ReturnType<typeof apiLookup>;


constructor(props: Props) {
super(props);

makeObservable(this);

// Does not accept input value updates outside of construction.
// The component manages its own state so it can handle user and text
// conversion without bouncing values back and forth.
if (this.props.initialUsers != null) {
for (const user of this.props.initialUsers) {
this.validUsers.set(user.id, user);
Expand Down

0 comments on commit 799fbdf

Please sign in to comment.