feat: use baseurl from server config (#5369)

This commit is contained in:
DarkSky
2023-12-21 12:52:38 +00:00
parent 9fbd9b39d6
commit aa4d42b36c
16 changed files with 88 additions and 54 deletions
@@ -8,7 +8,7 @@ import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { useWorkspace } from '@toeverything/hooks/use-workspace';
import { useWorkspaceInfo } from '@toeverything/hooks/use-workspace-info';
import { useSelfHosted } from '../../../hooks/affine/use-server-flavor';
import { useSelfHosted } from '../../../hooks/affine/use-server-config';
import { DeleteLeaveWorkspace } from './delete-leave-workspace';
import { EnableCloudPanel } from './enable-cloud';
import { ExportPanel } from './export';
@@ -34,7 +34,7 @@ import {
openSignOutModalAtom,
} from '../../../../atoms';
import { useCurrentUser } from '../../../../hooks/affine/use-current-user';
import { useSelfHosted } from '../../../../hooks/affine/use-server-flavor';
import { useSelfHosted } from '../../../../hooks/affine/use-server-config';
import { useUserSubscription } from '../../../../hooks/use-subscription';
import { validateAndReduceImage } from '../../../../utils/reduce-image';
import { Upload } from '../../../pure/file-upload';
@@ -8,7 +8,7 @@ import {
import type { ReactElement, SVGProps } from 'react';
import { useCurrentLoginStatus } from '../../../../hooks/affine/use-current-login-status';
import { useSelfHosted } from '../../../../hooks/affine/use-server-flavor';
import { useSelfHosted } from '../../../../hooks/affine/use-server-config';
import { AboutAffine } from './about';
import { AppearanceSettings } from './appearance';
import { BillingSettings } from './billing';
@@ -19,7 +19,7 @@ export const ShareExport = ({
const t = useAFFiNEI18N();
const workspaceId = workspace.id;
const pageId = currentPage.id;
const { onClickCopyLink } = useSharingUrl({
const { sharingUrl, onClickCopyLink } = useSharingUrl({
workspaceId,
pageId,
urlType: 'workspace',
@@ -57,6 +57,7 @@ export const ShareExport = ({
onClick={onClickCopyLink}
icon={<LinkIcon />}
type="plain"
disabled={!sharingUrl}
>
{t['com.affine.share-menu.copy-private-link']()}
</Button>
@@ -18,6 +18,7 @@ import { useCallback } from 'react';
import type { PageMode } from '../../../../atoms';
import { currentModeAtom } from '../../../../atoms/mode';
import { useIsSharedPage } from '../../../../hooks/affine/use-is-shared-page';
import { useServerBaseUrl } from '../../../../hooks/affine/use-server-config';
import * as styles from './index.css';
import type { ShareMenuProps } from './share-menu';
import { useSharingUrl } from './use-share-url';
@@ -98,6 +99,7 @@ export const AffineSharePage = (props: ShareMenuProps) => {
pageId,
urlType: 'share',
});
const baseUrl = useServerBaseUrl();
const t = useAFFiNEI18N();
const onClickCreateLink = useCallback(() => {
@@ -140,9 +142,13 @@ export const AffineSharePage = (props: ShareMenuProps) => {
lineHeight: '20px',
}}
value={
isSharedPage
? sharingUrl
: `${location.protocol}//${location.hostname}/...`
(isSharedPage && sharingUrl) ||
`${
baseUrl ||
`${location.protocol}${
location.port ? `:${location.port}` : ''
}//${location.hostname}`
}/...`
}
readOnly
/>
@@ -151,6 +157,7 @@ export const AffineSharePage = (props: ShareMenuProps) => {
onClick={onClickCopyLink}
data-testid="share-menu-copy-link-button"
style={{ padding: '4px 12px', whiteSpace: 'nowrap' }}
disabled={!sharingUrl}
>
{t.Copy()}
</Button>
@@ -1,4 +1,5 @@
import { toast } from '@affine/component';
import { useServerBaseUrl } from '@affine/core/hooks/affine/use-server-config';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { useCallback, useMemo } from 'react';
@@ -10,22 +11,27 @@ type UseSharingUrl = {
urlType: UrlType;
};
export const generateUrl = ({
workspaceId,
pageId,
urlType,
}: UseSharingUrl) => {
// to generate a private url like https://affine.app/workspace/123/456
// to generate a public url like https://affine.app/share/123/456
// or https://affine.app/share/123/456?mode=edgeless
const useGenerateUrl = ({ workspaceId, pageId, urlType }: UseSharingUrl) => {
// to generate a private url like https://app.affine.app/workspace/123/456
// to generate a public url like https://app.affine.app/share/123/456
// or https://app.affine.app/share/123/456?mode=edgeless
const { protocol, hostname, port } = window.location;
const url = new URL(
`${protocol}//${hostname}${
port ? `:${port}` : ''
}/${urlType}/${workspaceId}/${pageId}`
);
return url.toString();
const baseUrl = useServerBaseUrl();
const url = useMemo(() => {
// baseUrl is null when running in electron and without network
if (!baseUrl) return null;
try {
return new URL(
`${baseUrl}/${urlType}/${workspaceId}/${pageId}`
).toString();
} catch (e) {
return null;
}
}, [baseUrl, pageId, urlType, workspaceId]);
return url;
};
export const useSharingUrl = ({
@@ -34,20 +40,21 @@ export const useSharingUrl = ({
urlType,
}: UseSharingUrl) => {
const t = useAFFiNEI18N();
const sharingUrl = useMemo(
() => generateUrl({ workspaceId, pageId, urlType }),
[workspaceId, pageId, urlType]
);
const sharingUrl = useGenerateUrl({ workspaceId, pageId, urlType });
const onClickCopyLink = useCallback(() => {
navigator.clipboard
.writeText(sharingUrl)
.then(() => {
toast(t['Copied link to clipboard']());
})
.catch(err => {
console.error(err);
});
if (sharingUrl) {
navigator.clipboard
.writeText(sharingUrl)
.then(() => {
toast(t['Copied link to clipboard']());
})
.catch(err => {
console.error(err);
});
} else {
toast('Network not available');
}
}, [sharingUrl, t]);
return {