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

Fix / sometimes being cleared from chatbox #5831

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/react-components/room/ChatSidebarContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,18 @@ ChatContextProvider.propTypes = {
messageDispatch: PropTypes.object
};

export function ChatSidebarContainer({ scene, canSpawnMessages, presences, occupantCount, inputEffect, onClose }) {
export function ChatSidebarContainer({
scene,
canSpawnMessages,
presences,
occupantCount,
initialValue,
autoFocus,
onClose
}) {
const { messageGroups, sendMessage, setMessagesRead } = useContext(ChatContext);
const [onScrollList, listRef, scrolledToBottom] = useMaintainScrollPosition(messageGroups);
const [message, setMessage] = useState("");
const [message, setMessage] = useState(initialValue || "");
const [isCommand, setIsCommand] = useState(false);
const { text_chat: canTextChat } = usePermissions();
const isMod = useRole("owner");
Expand Down Expand Up @@ -262,7 +270,16 @@ export function ChatSidebarContainer({ scene, canSpawnMessages, presences, occup
[setMessage, inputRef]
);

useEffect(() => inputEffect(inputRef.current), [inputEffect, inputRef]);
useEffect(() => {
if (autoFocus) {
inputRef.current.focus();
const len = inputRef.current.value.length;
inputRef.current.setSelectionRange(len, len);
}
// We only want this effect to run on initial mount even if autoFocus were to change.
// This does not happen in practice, but this is more correct.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (scrolledToBottom) {
Expand Down Expand Up @@ -363,7 +380,8 @@ ChatSidebarContainer.propTypes = {
occupantCount: PropTypes.number.isRequired,
scene: PropTypes.object.isRequired,
onClose: PropTypes.func.isRequired,
inputEffect: PropTypes.func.isRequired
autoFocus: PropTypes.bool,
initialValue: PropTypes.string
};

export function ChatToolbarButtonContainer(props) {
Expand Down
18 changes: 10 additions & 8 deletions src/react-components/ui-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ class UIRoot extends Component {
objectSrc: "",
sidebarId: null,
presenceCount: 0,
chatInputEffect: () => {}
chatPrefix: "",
chatAutofocus: false
};

constructor(props) {
Expand Down Expand Up @@ -768,7 +769,7 @@ class UIRoot extends Component {
pushHistoryState = (k, v) => pushHistoryState(this.props.history, k, v);

setSidebar(sidebarId, otherState) {
this.setState({ sidebarId, chatInputEffect: () => {}, selectedUserId: null, ...otherState });
this.setState({ sidebarId, chatPrefix: "", chatAutofoucs: false, selectedUserId: null, ...otherState });
}

toggleSidebar(sidebarId, otherState) {
Expand All @@ -785,10 +786,8 @@ class UIRoot extends Component {

onFocusChat = e => {
this.setSidebar("chat", {
chatInputEffect: input => {
input.focus();
input.value = e.detail.prefix;
}
chatPrefix: e.detail.prefix,
chatAutofocus: true
});
};

Expand Down Expand Up @@ -1475,7 +1474,8 @@ class UIRoot extends Component {
canSpawnMessages={entered && this.props.hubChannel.can("spawn_and_move_media")}
scene={this.props.scene}
onClose={() => this.setSidebar(null)}
inputEffect={this.state.chatInputEffect}
autoFocus={this.state.chatAutofocus}
initialValue={this.state.chatPrefix}
/>
)}
{this.state.sidebarId === "objects" && (
Expand Down Expand Up @@ -1609,7 +1609,9 @@ class UIRoot extends Component {
)}
</>
)}
<ChatToolbarButtonContainer onClick={() => this.toggleSidebar("chat")} />
<ChatToolbarButtonContainer
onClick={() => this.toggleSidebar("chat", { chatPrefix: "", chatAutofocus: false })}
/>
{entered && isMobileVR && (
<ToolbarButton
className={styleUtils.hideLg}
Expand Down