move leave room dialog to new sdk
This commit is contained in:
parent
2d31b474fa
commit
d057233c45
2 changed files with 32 additions and 41 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import React, { useCallback, useEffect } from 'react';
|
||||
import React, { useCallback } from 'react';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import {
|
||||
Dialog,
|
||||
|
|
@ -17,15 +17,12 @@ import {
|
|||
Spinner,
|
||||
} from 'folds';
|
||||
import { postLeave } from 'neurone/lib/api/matrix/client/v3/rooms';
|
||||
import {
|
||||
MatrixResponseResult,
|
||||
parseMatrixResp,
|
||||
printMatrixError,
|
||||
} from 'neurone/lib/matrix-request';
|
||||
import { parseMatrixResp } from 'neurone/lib/matrix-request';
|
||||
import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback';
|
||||
import { stopPropagation } from '../../utils/keyboard';
|
||||
import { useClient } from '../../hooks/useClient';
|
||||
import { deleteJoinRooms } from '../../../client/syncWriter/utils';
|
||||
import { useAlive } from '../../hooks/useAlive';
|
||||
|
||||
type LeaveRoomPromptProps = {
|
||||
roomId: string;
|
||||
|
|
@ -34,31 +31,25 @@ type LeaveRoomPromptProps = {
|
|||
};
|
||||
export function LeaveRoomPrompt({ roomId, onDone, onCancel }: LeaveRoomPromptProps) {
|
||||
const ct = useClient();
|
||||
const alive = useAlive();
|
||||
|
||||
const [leaveState, leaveRoom] = useAsyncCallback<MatrixResponseResult<unknown>, Error, []>(
|
||||
const [leaveState, leaveRoom] = useAsyncCallback<void, Error, []>(
|
||||
useCallback(async () => {
|
||||
const response = await ct.requestAuthJSON(postLeave({ pathParams: { roomId } }));
|
||||
deleteJoinRooms(ct, [roomId])
|
||||
.then(() => console.log('=====> deleted'))
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
});
|
||||
return parseMatrixResp(response);
|
||||
deleteJoinRooms(ct, [roomId]);
|
||||
const [err] = await parseMatrixResp(response);
|
||||
if (err) throw new Error(err.data.error ?? err.data.errcode);
|
||||
}, [ct, roomId])
|
||||
);
|
||||
const success = leaveState.status === AsyncStatus.Success;
|
||||
const [requestError] = success ? leaveState.data : [];
|
||||
|
||||
const handleLeave = () => {
|
||||
leaveRoom();
|
||||
leaveRoom().then(() => {
|
||||
if (alive()) {
|
||||
onDone();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (success) {
|
||||
onDone();
|
||||
}
|
||||
}, [success, onDone]);
|
||||
|
||||
return (
|
||||
<Overlay open backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
|
|
@ -94,11 +85,6 @@ export function LeaveRoomPrompt({ roomId, onDone, onCancel }: LeaveRoomPromptPro
|
|||
Failed to leave room! {leaveState.error.message}
|
||||
</Text>
|
||||
)}
|
||||
{requestError && (
|
||||
<Text style={{ color: color.Critical.Main }} size="T300">
|
||||
Failed to leave room! {printMatrixError(requestError)}
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
<Button
|
||||
type="submit"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import React, { useCallback, useEffect } from 'react';
|
||||
import React, { useCallback } from 'react';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import {
|
||||
Dialog,
|
||||
|
|
@ -16,10 +16,13 @@ import {
|
|||
Button,
|
||||
Spinner,
|
||||
} from 'folds';
|
||||
import { MatrixError } from 'matrix-js-sdk';
|
||||
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
||||
import { postLeave } from 'neurone/lib/api/matrix/client/v3/rooms';
|
||||
import { parseMatrixResp } from 'neurone/lib/matrix-request';
|
||||
import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback';
|
||||
import { stopPropagation } from '../../utils/keyboard';
|
||||
import { useClient } from '../../hooks/useClient';
|
||||
import { deleteJoinRooms } from '../../../client/syncWriter/utils';
|
||||
import { useAlive } from '../../hooks/useAlive';
|
||||
|
||||
type LeaveSpacePromptProps = {
|
||||
roomId: string;
|
||||
|
|
@ -27,24 +30,26 @@ type LeaveSpacePromptProps = {
|
|||
onCancel: () => void;
|
||||
};
|
||||
export function LeaveSpacePrompt({ roomId, onDone, onCancel }: LeaveSpacePromptProps) {
|
||||
const mx = useMatrixClient();
|
||||
const ct = useClient();
|
||||
const alive = useAlive();
|
||||
|
||||
const [leaveState, leaveRoom] = useAsyncCallback<undefined, MatrixError, []>(
|
||||
const [leaveState, leaveRoom] = useAsyncCallback<void, Error, []>(
|
||||
useCallback(async () => {
|
||||
mx.leave(roomId);
|
||||
}, [mx, roomId])
|
||||
const response = await ct.requestAuthJSON(postLeave({ pathParams: { roomId } }));
|
||||
deleteJoinRooms(ct, [roomId]);
|
||||
const [err] = await parseMatrixResp(response);
|
||||
if (err) throw new Error(err.data.error ?? err.data.errcode);
|
||||
}, [ct, roomId])
|
||||
);
|
||||
|
||||
const handleLeave = () => {
|
||||
leaveRoom();
|
||||
leaveRoom().then(() => {
|
||||
if (alive()) {
|
||||
onDone();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (leaveState.status === AsyncStatus.Success) {
|
||||
onDone();
|
||||
}
|
||||
}, [leaveState, onDone]);
|
||||
|
||||
return (
|
||||
<Overlay open backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
|
|
|
|||
Loading…
Reference in a new issue