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] VoIP button gets disabled whenever user status changes #24789

Merged
merged 4 commits into from
Mar 17, 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
4 changes: 4 additions & 0 deletions client/lib/voip/VoIPUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,8 @@ export class VoIPUser extends Emitter<VoipEvents> implements OutgoingRequestDele
getRegistrarState(): string | undefined {
return this.registerer?.state.toString().toLocaleLowerCase();
}

clear(): void {
this.userAgent?.stop();
}
}
35 changes: 27 additions & 8 deletions client/providers/CallProvider/hooks/useVoipClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IRegistrationInfo } from '../../../../definition/voip/IRegistrationInfo
import { WorkflowTypes } from '../../../../definition/voip/WorkflowTypes';
import { useEndpoint } from '../../../contexts/ServerContext';
import { useSetting } from '../../../contexts/SettingsContext';
import { useUser } from '../../../contexts/UserContext';
import { useUser, useUserId } from '../../../contexts/UserContext';
import { SimpleVoipUser } from '../../../lib/voip/SimpleVoipUser';
import { VoIPUser } from '../../../lib/voip/VoIPUser';
import { useWebRtcServers } from './useWebRtcServers';
Expand All @@ -33,17 +33,18 @@ export const useVoipClient = (): UseVoipClientResult => {
const registrationInfo = useEndpoint('GET', 'connector.extension.getRegistrationInfoByUserId');
const membership = useEndpoint('GET', 'voip/queues.getMembershipSubscription');
const user = useUser();
const iceServers = useWebRtcServers();
const userId = useUserId();
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey 👀 not sure of a different approach here, but, I did use "user.extension" prop on other fix to prevent this call from happening if the user doesn't have an extension associated.

But, that breaks the fix 🤔 since I was using the "useUser" hook, unless we have a "useUserExtension" hook somewhere (or we create one)

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, If you are using other data from the user object, we can return the code to the way it was before my review

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KevLehman Can you please point me to the task? There are few of such kind which are dependent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As @KevLehman pointed out, the fix was breaking #24752. Next commit fixes this.

const [extension, setExtension] = useSafely(useState<string | null>(null));

const iceServers = useWebRtcServers();
const [result, setResult] = useSafely(useState<UseVoipClientResult>({}));

useEffect(() => {
if (!user || !user?._id || !user?.extension || !voipEnabled) {
if (!userId || !extension || !voipEnabled) {
setResult({});
return;
}

registrationInfo({ id: user._id }).then(
let client: VoIPUser;
registrationInfo({ id: userId }).then(
(data) => {
let parsedData: IRegistrationInfo;
if (isSignedResponse(data)) {
Expand All @@ -59,7 +60,7 @@ export const useVoipClient = (): UseVoipClientResult => {
callServerConfig: { websocketPath },
} = parsedData;

let client: VoIPUser;
// let client: VoIPUser;
(async (): Promise<void> => {
try {
const subscription = await membership({ extension });
Expand All @@ -81,8 +82,26 @@ export const useVoipClient = (): UseVoipClientResult => {
return (): void => {
// client?.disconnect();
// TODO how to close the client? before creating a new one?
if (client) {
client.clear();
}
};
}, [user, iceServers, registrationInfo, setResult, membership, voipEnabled]);
}, [userId, iceServers, registrationInfo, setResult, membership, voipEnabled, extension]);

useEffect(() => {
if (!user) {
setResult({});
return;
}
if (user.extension) {
setExtension(user.extension);
} else {
setExtension(null);
if (!isUseVoipClientResultError(result) && !isUseVoipClientResultLoading(result)) {
const { voipClient } = result as UseVoipClientResultResolved;
voipClient.clear();
}
}
}, [result, setExtension, setResult, user]);
return result;
};