2024-01-16 03:49:09 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
2024-06-04 21:40:54 +08:00
|
|
|
import { useMutation } from '@apollo/client';
|
2024-01-16 03:49:09 +08:00
|
|
|
import { HAS_PAD_SUBSCRIPTION, HasPadSubscriptionResponse } from './queries';
|
|
|
|
import { PAD_SESSION_SUBSCRIPTION, PadSessionSubscriptionResponse } from './sessions/queries';
|
|
|
|
import { CREATE_SESSION } from './mutations';
|
|
|
|
import Service from './service';
|
|
|
|
import Styled from './styles';
|
|
|
|
import PadContent from './content/component';
|
2024-06-04 21:40:54 +08:00
|
|
|
import useDeduplicatedSubscription from '/imports/ui/core/hooks/useDeduplicatedSubscription';
|
2024-01-16 03:49:09 +08:00
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
hint: {
|
|
|
|
id: 'app.pads.hint',
|
|
|
|
description: 'Label for hint on how to escape iframe',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
interface PadContainerGraphqlProps {
|
|
|
|
externalId: string;
|
|
|
|
hasPermission: boolean;
|
|
|
|
isResizing: boolean;
|
|
|
|
isRTL: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PadGraphqlProps extends Omit<PadContainerGraphqlProps, 'hasPermission'> {
|
|
|
|
hasSession: boolean;
|
|
|
|
sessionIds: Array<string>;
|
|
|
|
padId: string | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PadGraphql: React.FC<PadGraphqlProps> = (props) => {
|
|
|
|
const {
|
|
|
|
externalId,
|
|
|
|
hasSession,
|
|
|
|
isResizing,
|
|
|
|
isRTL,
|
|
|
|
sessionIds,
|
|
|
|
padId,
|
|
|
|
} = props;
|
|
|
|
const [padURL, setPadURL] = useState<string | undefined>();
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!padId) {
|
|
|
|
setPadURL(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setPadURL(Service.buildPadURL(padId, sessionIds));
|
|
|
|
}, [isRTL, hasSession]);
|
|
|
|
|
|
|
|
if (!hasSession) {
|
|
|
|
return <PadContent externalId={externalId} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.Pad>
|
|
|
|
<Styled.IFrame
|
|
|
|
title="pad"
|
|
|
|
src={padURL}
|
|
|
|
aria-describedby="padEscapeHint"
|
|
|
|
style={{
|
|
|
|
pointerEvents: isResizing ? 'none' : 'inherit',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Styled.Hint
|
|
|
|
id="padEscapeHint"
|
|
|
|
aria-hidden
|
|
|
|
>
|
|
|
|
{intl.formatMessage(intlMessages.hint)}
|
|
|
|
</Styled.Hint>
|
|
|
|
</Styled.Pad>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PadContainerGraphql: React.FC<PadContainerGraphqlProps> = (props) => {
|
|
|
|
const {
|
|
|
|
externalId,
|
|
|
|
hasPermission,
|
|
|
|
isRTL,
|
|
|
|
isResizing,
|
|
|
|
} = props;
|
|
|
|
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data: hasPadData } = useDeduplicatedSubscription<HasPadSubscriptionResponse>(
|
2024-01-16 03:49:09 +08:00
|
|
|
HAS_PAD_SUBSCRIPTION,
|
|
|
|
{ variables: { externalId } },
|
|
|
|
);
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data: padSessionData } = useDeduplicatedSubscription<PadSessionSubscriptionResponse>(
|
|
|
|
PAD_SESSION_SUBSCRIPTION,
|
|
|
|
);
|
2024-01-16 03:49:09 +08:00
|
|
|
const [createSession] = useMutation(CREATE_SESSION);
|
|
|
|
|
|
|
|
const sessionData = padSessionData?.sharedNotes_session ?? [];
|
|
|
|
const session = sessionData.find((s) => s.sharedNotesExtId === externalId);
|
|
|
|
const hasPad = !!hasPadData && hasPadData.sharedNotes.length > 0;
|
|
|
|
const hasSession = !!session?.sessionId;
|
|
|
|
const sessionIds = new Set<string>(sessionData.map((s) => s.sessionId));
|
|
|
|
|
|
|
|
if (hasPad && !hasSession && hasPermission) {
|
|
|
|
createSession({ variables: { externalId } });
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PadGraphql
|
|
|
|
hasSession={hasSession}
|
|
|
|
externalId={externalId}
|
|
|
|
isRTL={isRTL}
|
|
|
|
isResizing={isResizing}
|
|
|
|
sessionIds={Array.from(sessionIds)}
|
|
|
|
padId={session?.sharedNotes?.padId}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PadContainerGraphql;
|