You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/components/leftpane/overlay/OverlayCommunity.tsx

125 lines
4.0 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-misused-promises */
import { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import useKey from 'react-use/lib/useKey';
import { SessionJoinableRooms } from './SessionJoinableDefaultRooms';
import {
joinOpenGroupV2WithUIEvents,
JoinSogsRoomUICallbackArgs,
} from '../../../session/apis/open_group_api/opengroupV2/JoinOpenGroupV2';
import { openGroupV2CompleteURLRegex } from '../../../session/apis/open_group_api/utils/OpenGroupUtils';
import { resetLeftOverlayMode } from '../../../state/ducks/section';
import { SessionButton } from '../../basic/SessionButton';
import { SessionSpinner } from '../../loading';
import {
markConversationInitialLoadingInProgress,
openConversationWithMessages,
} from '../../../state/ducks/conversations';
import { getLeftOverlayMode } from '../../../state/selectors/section';
import { Spacer2XL } from '../../basic/Text';
import { SessionInput } from '../../inputs';
import { StyledLeftPaneOverlay } from './OverlayMessage';
import LIBSESSION_CONSTANTS from '../../../session/utils/libsession/libsession_constants';
async function joinOpenGroup(
serverUrl: string,
errorHandler: (error: string) => void,
uiCallback?: (args: JoinSogsRoomUICallbackArgs) => void
) {
// guess if this is an open
if (serverUrl.match(openGroupV2CompleteURLRegex)) {
const groupCreated = await joinOpenGroupV2WithUIEvents(
serverUrl,
false,
false,
uiCallback,
errorHandler
);
return groupCreated;
}
throw new Error(window.i18n('communityEnterUrlErrorInvalid'));
}
export const OverlayCommunity = () => {
const dispatch = useDispatch();
const [groupUrl, setGroupUrl] = useState('');
const [groupUrlError, setGroupUrlError] = useState<string | undefined>(undefined);
const [loading, setLoading] = useState(false);
const overlayModeIsCommunity = useSelector(getLeftOverlayMode) === 'open-group';
function closeOverlay() {
dispatch(resetLeftOverlayMode());
}
async function onTryJoinRoom(completeUrl?: string) {
try {
if (loading) {
return;
}
setGroupUrlError(undefined);
const url = (completeUrl && completeUrl.trim()) || (groupUrl && groupUrl.trim());
await joinOpenGroup(url, setGroupUrlError, joinSogsUICallback);
} catch (e) {
setGroupUrlError(e.message);
window.log.warn(e);
} finally {
setLoading(false);
}
}
function joinSogsUICallback(args: JoinSogsRoomUICallbackArgs) {
setLoading(args.loadingState === 'started');
if (args.conversationKey) {
dispatch(
markConversationInitialLoadingInProgress({
conversationKey: args.conversationKey,
isInitialFetchingInProgress: true,
})
);
}
if (args.loadingState === 'finished' && overlayModeIsCommunity && args.conversationKey) {
closeOverlay();
void openConversationWithMessages({ conversationKey: args.conversationKey, messageId: null }); // open to last unread for a session run sogs
}
}
useKey('Escape', closeOverlay);
return (
<StyledLeftPaneOverlay
container={true}
flexDirection={'column'}
flexGrow={1}
alignItems={'center'}
padding={'var(--margins-md)'}
>
<SessionInput
autoFocus={true}
type="text"
Merge branch 'unstable' into standardised_strings_merge # Conflicts: # .gitignore # _locales/en/messages.json # ts/components/DebugLogView.tsx # ts/components/SessionWrapperModal.tsx # ts/components/basic/SessionHTMLRenderer.tsx # ts/components/basic/SessionRadio.tsx # ts/components/buttons/MenuButton.tsx # ts/components/conversation/SessionConversation.tsx # ts/components/conversation/SubtleNotification.tsx # ts/components/conversation/TimerNotification.tsx # ts/components/conversation/composition/CompositionBox.tsx # ts/components/conversation/message/message-content/MessageText.tsx # ts/components/conversation/message/message-item/InteractionNotification.tsx # ts/components/conversation/message/reactions/ReactionPopup.tsx # ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/DisappearingModes.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/OverlayDisappearingMessages.tsx # ts/components/dialog/BanOrUnbanUserDialog.tsx # ts/components/dialog/DeleteAccountModal.tsx # ts/components/dialog/EditProfileDialog.tsx # ts/components/dialog/ModeratorsAddDialog.tsx # ts/components/dialog/OnionStatusPathDialog.tsx # ts/components/dialog/ReactListModal.tsx # ts/components/dialog/SessionSeedModal.tsx # ts/components/dialog/SessionSetPasswordDialog.tsx # ts/components/dialog/UserDetailsDialog.tsx # ts/components/leftpane/LeftPaneSectionHeader.tsx # ts/components/leftpane/LeftPaneSettingSection.tsx # ts/components/leftpane/conversation-list-item/InteractionItem.tsx # ts/components/leftpane/overlay/OverlayClosedGroup.tsx # ts/components/leftpane/overlay/OverlayCommunity.tsx # ts/components/leftpane/overlay/OverlayMessage.tsx # ts/components/leftpane/overlay/SessionJoinableDefaultRooms.tsx # ts/components/leftpane/overlay/choose-action/ContactsListWithBreaks.tsx # ts/components/leftpane/overlay/choose-action/OverlayChooseAction.tsx # ts/components/menu/Menu.tsx # ts/components/registration/RegistrationStages.tsx # ts/components/registration/RegistrationUserDetails.tsx # ts/components/registration/SignInTab.tsx # ts/components/registration/SignUpTab.tsx # ts/components/settings/SessionSettings.tsx # ts/components/settings/SessionSettingsHeader.tsx # ts/components/settings/ZoomingSessionSlider.tsx # ts/components/settings/section/CategoryAppearance.tsx # ts/components/settings/section/CategoryHelp.tsx # ts/components/settings/section/CategoryPermissions.tsx # ts/components/settings/section/CategoryPrivacy.tsx # ts/hooks/useParamSelector.ts # ts/mains/main_renderer.tsx # ts/models/message.ts # ts/node/menu.ts # ts/node/tray_icon.ts # ts/session/constants.ts # ts/session/disappearing_messages/timerOptions.ts # ts/session/utils/Toast.tsx # ts/state/selectors/search.ts # ts/test/session/unit/selectors/conversations_test.ts # ts/types/LocalizerKeys.ts # ts/types/Util.ts # ts/window.d.ts # yarn.lock
8 months ago
placeholder={window.i18n('communityEnterUrl')}
value={groupUrl}
onValueChanged={setGroupUrl}
onEnterPressed={onTryJoinRoom}
editable={!loading}
error={groupUrlError}
// - 1 for null terminator
maxLength={LIBSESSION_CONSTANTS.COMMUNITY_FULL_URL_MAX_LENGTH - 1}
textSize="md"
centerText={true}
monospaced={true}
isTextArea={true}
/>
<Spacer2XL />
<SessionButton text={window.i18n('join')} disabled={!groupUrl} onClick={onTryJoinRoom} />
{!loading ? <Spacer2XL /> : null}
<SessionSpinner loading={loading} />
<SessionJoinableRooms onJoinClick={onTryJoinRoom} alreadyJoining={loading} />
</StyledLeftPaneOverlay>
);
};