Skip to content

Commit

Permalink
refactor(core): support multiple servers
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Nov 10, 2024
1 parent e2f281a commit bf0a5e6
Show file tree
Hide file tree
Showing 77 changed files with 1,113 additions and 512 deletions.
4 changes: 2 additions & 2 deletions packages/frontend/apps/ios/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* for support arraybuffer response type
*/
import { FetchProvider } from '@affine/core/modules/cloud/provider/fetch';
import { RawFetchProvider } from '@affine/core/modules/cloud/provider/fetch';
import { CapacitorHttp } from '@capacitor/core';
import type { Framework } from '@toeverything/infra';

Expand Down Expand Up @@ -121,7 +121,7 @@ function base64ToUint8Array(base64: string) {
return new Uint8Array(binaryArray);
}
export function configureFetchProvider(framework: Framework) {
framework.override(FetchProvider, {
framework.override(RawFetchProvider, {
fetch: async (input, init) => {
const request = new Request(input, init);
const { method } = request;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export * from './auth-page-container';
export * from './back-button';
export * from './change-email-page';
export * from './change-password-page';
export * from './confirm-change-email';
export * from './count-down-render';
export * from './email-verified-email';
export * from './modal';
export * from './modal-header';
export * from './onboarding-page';
Expand Down
14 changes: 7 additions & 7 deletions packages/frontend/core/src/components/affine/auth/oauth.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Skeleton } from '@affine/component';
import { Button } from '@affine/component/ui/button';
import { ServerService } from '@affine/core/modules/cloud';
import { UrlService } from '@affine/core/modules/url';
import { OAuthProviderType } from '@affine/graphql';
import { GithubIcon, GoogleDuotoneIcon } from '@blocksuite/icons/rc';
import { useLiveData, useService } from '@toeverything/infra';
import { type ReactElement, useCallback } from 'react';

import { ServerConfigService } from '../../../modules/cloud';

const OAuthProviderMap: Record<
OAuthProviderType,
{
Expand All @@ -29,11 +28,11 @@ const OAuthProviderMap: Record<
};

export function OAuth({ redirectUrl }: { redirectUrl?: string }) {
const serverConfig = useService(ServerConfigService).serverConfig;
const serverService = useService(ServerService);
const urlService = useService(UrlService);
const oauth = useLiveData(serverConfig.features$.map(r => r?.oauth));
const oauth = useLiveData(serverService.server.features$.map(r => r?.oauth));
const oauthProviders = useLiveData(
serverConfig.config$.map(r => r?.oauthProviders)
serverService.server.config$.map(r => r?.oauthProviders)
);
const scheme = urlService.getClientScheme();

Expand Down Expand Up @@ -65,6 +64,7 @@ function OAuthProvider({
scheme?: string;
popupWindow: (url: string) => void;
}) {
const serverService = useService(ServerService);
const { icon } = OAuthProviderMap[provider];

const onClick = useCallback(() => {
Expand All @@ -84,10 +84,10 @@ function OAuthProvider({
// if (BUILD_CONFIG.isAndroid) {}

const oauthUrl =
BUILD_CONFIG.serverUrlPrefix + `/oauth/login?${params.toString()}`;
serverService.server.baseUrl + `/oauth/login?${params.toString()}`;

popupWindow(oauthUrl);
}, [popupWindow, provider, redirectUrl, scheme]);
}, [popupWindow, provider, redirectUrl, scheme, serverService]);

return (
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useLiveData, useService } from '@toeverything/infra';
import { useCallback, useState } from 'react';

import { useMutation } from '../../../components/hooks/use-mutation';
import { ServerConfigService } from '../../../modules/cloud';
import { ServerService } from '../../../modules/cloud';
import type { AuthPanelProps } from './index';

const useEmailTitle = (emailType: AuthPanelProps<'sendEmail'>['emailType']) => {
Expand Down Expand Up @@ -142,10 +142,10 @@ export const SendEmail = ({
// todo(@pengx17): impl redirectUrl for sendEmail?
}: AuthPanelProps<'sendEmail'>) => {
const t = useI18n();
const serverConfig = useService(ServerConfigService).serverConfig;
const serverService = useService(ServerService);

const passwordLimits = useLiveData(
serverConfig.credentialsRequirement$.map(r => r?.password)
serverService.server.credentialsRequirement$.map(r => r?.password)
);
const [hasSentEmail, setHasSentEmail] = useState(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@ import { useCatchEventCallback } from '@affine/core/components/hooks/use-catch-e
import { GlobalDialogService } from '@affine/core/modules/dialogs';
import { SubscriptionPlan } from '@affine/graphql';
import { useI18n } from '@affine/i18n';
import { useLiveData, useService, useServices } from '@toeverything/infra';
import { useLiveData, useService } from '@toeverything/infra';
import { useEffect } from 'react';

import {
ServerConfigService,
SubscriptionService,
} from '../../../modules/cloud';
import { ServerService, SubscriptionService } from '../../../modules/cloud';
import * as styles from './style.css';

export const UserPlanButton = () => {
const { serverConfigService, subscriptionService } = useServices({
ServerConfigService,
SubscriptionService,
});
const serverService = useService(ServerService);
const subscriptionService = useService(SubscriptionService);

const hasPayment = useLiveData(
serverConfigService.serverConfig.features$.map(r => r?.payment)
serverService.server.features$.map(r => r?.payment)
);
const plan = useLiveData(
subscriptionService.subscription.pro$.map(subscription =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useSharingUrl,
} from '@affine/core/components/hooks/affine/use-share-url';
import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks';
import { ServerConfigService } from '@affine/core/modules/cloud';
import { ServerService } from '@affine/core/modules/cloud';
import { GlobalDialogService } from '@affine/core/modules/dialogs';
import { EditorService } from '@affine/core/modules/editor';
import { WorkspacePermissionService } from '@affine/core/modules/permissions';
Expand Down Expand Up @@ -70,13 +70,13 @@ export const AFFiNESharePage = (props: ShareMenuProps) => {
const currentMode = useLiveData(editor.mode$);
const editorContainer = useLiveData(editor.editorContainer$);
const shareInfoService = useService(ShareInfoService);
const serverConfig = useService(ServerConfigService).serverConfig;
const serverService = useService(ServerService);
useEffect(() => {
shareInfoService.shareInfo.revalidate();
}, [shareInfoService]);
const isSharedPage = useLiveData(shareInfoService.shareInfo.isShared$);
const sharedMode = useLiveData(shareInfoService.shareInfo.sharedMode$);
const baseUrl = useLiveData(serverConfig.config$.map(c => c?.baseUrl));
const baseUrl = serverService.server.baseUrl;
const isLoading =
isSharedPage === null || sharedMode === null || baseUrl === null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import {
cleanupCopilotSessionMutation,
createCopilotMessageMutation,
createCopilotSessionMutation,
fetcher as defaultFetcher,
forkCopilotSessionMutation,
getBaseUrl,
getCopilotHistoriesQuery,
getCopilotHistoryIdsQuery,
getCopilotSessionsQuery,
gqlFetcherFactory,
GraphQLError,
type GraphQLQuery,
type QueryOptions,
Expand All @@ -22,6 +21,26 @@ import {
} from '@blocksuite/affine/blocks';
import { getCurrentStore } from '@toeverything/infra';

/**
* @deprecated will be removed soon
*/
export function getBaseUrl(): string {
if (BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS || BUILD_CONFIG.isAndroid) {
return BUILD_CONFIG.serverUrlPrefix;
}
if (typeof window === 'undefined') {
// is nodejs
return '';
}
const { protocol, hostname, port } = window.location;
return `${protocol}//${hostname}${port ? `:${port}` : ''}`;
}

/**
* @deprecated will be removed soon
*/
const defaultFetcher = gqlFetcherFactory(getBaseUrl() + '/graphql');

type OptionsField<T extends GraphQLQuery> =
RequestOptions<T>['variables'] extends { options: infer U } ? U : never;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AIProvider } from '@affine/core/blocksuite/presets/ai';
import { toggleGeneralAIOnboarding } from '@affine/core/components/affine/ai-onboarding/apis';
import { authAtom } from '@affine/core/components/atoms';
import {
getBaseUrl,
type getCopilotHistoriesQuery,
type RequestOptions,
} from '@affine/graphql';
Expand All @@ -11,6 +10,7 @@ import { assertExists } from '@blocksuite/affine/global/utils';
import { getCurrentStore } from '@toeverything/infra';
import { z } from 'zod';

import { getBaseUrl } from './copilot-client';
import type { PromptKey } from './prompt';
import {
cleanupSessions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
generateUrl,
type UseSharingUrl,
} from '@affine/core/components/hooks/affine/use-share-url';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { ServerService } from '@affine/core/modules/cloud';
import { EditorService } from '@affine/core/modules/editor';
import { I18n } from '@affine/i18n';
import { track } from '@affine/track';
Expand Down Expand Up @@ -40,9 +40,7 @@ function createCopyLinkToBlockMenuItem(
return mode === 'edgeless';
},
select: () => {
const baseUrl = getAffineCloudBaseUrl();
if (!baseUrl) return;

const serverService = framework.get(ServerService);
const pageId = model.doc.id;
const { editor } = framework.get(EditorService);
const mode = editor.mode$.value;
Expand All @@ -57,7 +55,10 @@ function createCopyLinkToBlockMenuItem(
blockIds: [model.id],
};

const str = generateUrl(options);
const str = generateUrl({
...options,
baseUrl: serverService.server.baseUrl,
});
if (!str) return;

const type = model.flavour;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
generateUrl,
type UseSharingUrl,
} from '@affine/core/components/hooks/affine/use-share-url';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { ServerService } from '@affine/core/modules/cloud';
import { EditorService } from '@affine/core/modules/editor';
import { I18n } from '@affine/i18n';
import { track } from '@affine/track';
Expand Down Expand Up @@ -78,11 +78,7 @@ function createCopyLinkToBlockMenuItem(
return {
...item,
action: (ctx: MenuContext) => {
const baseUrl = getAffineCloudBaseUrl();
if (!baseUrl) {
ctx.close();
return;
}
const serverService = framework.get(ServerService);

const { editor } = framework.get(EditorService);
const mode = editor.mode$.value;
Expand All @@ -108,7 +104,10 @@ function createCopyLinkToBlockMenuItem(
}
}

const str = generateUrl(options);
const str = generateUrl({
...options,
baseUrl: serverService.server.baseUrl,
});
if (!str) {
ctx.close();
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { notify } from '@affine/component';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { ServerService } from '@affine/core/modules/cloud';
import { toURLSearchParams } from '@affine/core/modules/navigation';
import { useI18n } from '@affine/i18n';
import { track } from '@affine/track';
import { type EditorHost } from '@blocksuite/affine/block-std';
import { GfxBlockElementModel } from '@blocksuite/affine/block-std/gfx';
import type { DocMode, EdgelessRootService } from '@blocksuite/affine/blocks';
import { useService } from '@toeverything/infra';
import { useCallback } from 'react';

export type UseSharingUrl = {
Expand All @@ -23,17 +24,14 @@ export type UseSharingUrl = {
* https://app.affine.pro/workspace/workspaceId/docId?mode=DocMode&elementIds=seletedElementIds&blockIds=selectedBlockIds
*/
export const generateUrl = ({
baseUrl,
workspaceId,
pageId,
blockIds,
elementIds,
shareMode: mode,
xywh, // not needed currently
}: UseSharingUrl) => {
// Base URL construction
const baseUrl = getAffineCloudBaseUrl();
if (!baseUrl) return null;

}: UseSharingUrl & { baseUrl: string }) => {
try {
const url = new URL(`/workspace/${workspaceId}/${pageId}`, baseUrl);
const search = toURLSearchParams({ mode, blockIds, elementIds, xywh });
Expand Down Expand Up @@ -129,10 +127,12 @@ export const getSelectedNodes = (

export const useSharingUrl = ({ workspaceId, pageId }: UseSharingUrl) => {
const t = useI18n();
const serverService = useService(ServerService);

const onClickCopyLink = useCallback(
(shareMode?: DocMode, blockIds?: string[], elementIds?: string[]) => {
const sharingUrl = generateUrl({
baseUrl: serverService.server.baseUrl,
workspaceId,
pageId,
blockIds,
Expand Down Expand Up @@ -164,7 +164,7 @@ export const useSharingUrl = ({ workspaceId, pageId }: UseSharingUrl) => {
});
}
},
[pageId, t, workspaceId]
[pageId, serverService, t, workspaceId]
);

return {
Expand Down
Loading

0 comments on commit bf0a5e6

Please sign in to comment.