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
@@ -9,7 +9,7 @@ const errorHandler: Middleware = useSWRNext => (key, fetcher, config) => {
return useSWRNext(key, wrappedFetcher.bind(null, fetcher), config);
};
export const useServerFlavor = () => {
const useServerConfig = () => {
const { data: config, error } = useQueryImmutable(
{ query: serverConfigQuery },
{
@@ -18,10 +18,20 @@ export const useServerFlavor = () => {
);
if (error || !config) {
return null;
}
return config.serverConfig;
};
export const useServerFlavor = () => {
const config = useServerConfig();
if (!config) {
return 'local';
}
return config.serverConfig.flavor;
return config.flavor;
};
export const useSelfHosted = () => {
@@ -29,3 +39,18 @@ export const useSelfHosted = () => {
return ['local', 'selfhosted'].includes(serverFlavor);
};
export const useServerBaseUrl = () => {
const config = useServerConfig();
if (!config) {
if (environment.isDesktop) {
// don't use window.location in electron
return null;
}
const { protocol, hostname, port } = window.location;
return `${protocol}//${hostname}${port ? `:${port}` : ''}`;
}
return config.baseUrl;
};
@@ -1,15 +0,0 @@
'use client';
import { useMemo } from 'react';
export function useShareLink(workspaceId: string): string {
return useMemo(() => {
if (environment.isServer) {
throw new Error('useShareLink is not available on server side');
}
if (environment.isDesktop) {
return '???';
} else {
return origin + '/share/' + workspaceId;
}
}, [workspaceId]);
}
@@ -2,7 +2,7 @@ import { type SubscriptionQuery, subscriptionQuery } from '@affine/graphql';
import { useQuery } from '@affine/workspace/affine/gql';
import { useAsyncCallback } from '@toeverything/hooks/affine-async-hooks';
import { useSelfHosted } from './affine/use-server-flavor';
import { useSelfHosted } from './affine/use-server-config';
export type Subscription = NonNullable<
NonNullable<SubscriptionQuery['currentUser']>['subscription']