mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 16:19:43 +08:00
feat: backend module awareness & optional request (#5909)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { serverConfigQuery, ServerDeploymentType } from '@affine/graphql';
|
||||
import type { ServerFeature } from '@affine/graphql';
|
||||
import { serverConfigQuery } from '@affine/graphql';
|
||||
import type { BareFetcher, Middleware } from 'swr';
|
||||
|
||||
import { useQueryImmutable } from '../use-query';
|
||||
@@ -25,20 +26,22 @@ const useServerConfig = () => {
|
||||
return config.serverConfig;
|
||||
};
|
||||
|
||||
export const useServerType = () => {
|
||||
type LowercaseServerFeature = Lowercase<ServerFeature>;
|
||||
type ServerFeatureRecord = {
|
||||
[key in LowercaseServerFeature]: boolean;
|
||||
};
|
||||
|
||||
export const useServerFeatures = (): ServerFeatureRecord => {
|
||||
const config = useServerConfig();
|
||||
|
||||
if (!config) {
|
||||
return 'local';
|
||||
return {} as ServerFeatureRecord;
|
||||
}
|
||||
|
||||
return config.type;
|
||||
};
|
||||
|
||||
export const useSelfHosted = () => {
|
||||
const serverType = useServerType();
|
||||
|
||||
return ['local', ServerDeploymentType.Selfhosted].includes(serverType);
|
||||
return Array.from(new Set(config.features)).reduce((acc, cur) => {
|
||||
acc[cur.toLowerCase() as LowercaseServerFeature] = true;
|
||||
return acc;
|
||||
}, {} as ServerFeatureRecord);
|
||||
};
|
||||
|
||||
export const useServerBaseUrl = () => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
|
||||
import { type SubscriptionQuery, subscriptionQuery } from '@affine/graphql';
|
||||
|
||||
import { useSelfHosted } from './affine/use-server-config';
|
||||
import { useServerFeatures } from './affine/use-server-config';
|
||||
import { useQuery } from './use-query';
|
||||
|
||||
export type Subscription = NonNullable<
|
||||
@@ -14,10 +14,10 @@ const selector = (data: SubscriptionQuery) =>
|
||||
data.currentUser?.subscription ?? null;
|
||||
|
||||
export const useUserSubscription = () => {
|
||||
const isSelfHosted = useSelfHosted();
|
||||
const { data, mutate } = useQuery({
|
||||
query: subscriptionQuery,
|
||||
});
|
||||
const { payment: hasPaymentFeature } = useServerFeatures();
|
||||
const { data, mutate } = useQuery(
|
||||
hasPaymentFeature ? { query: subscriptionQuery } : undefined
|
||||
);
|
||||
|
||||
const set: SubscriptionMutator = useAsyncCallback(
|
||||
async (update?: Partial<Subscription>) => {
|
||||
@@ -39,8 +39,8 @@ export const useUserSubscription = () => {
|
||||
[mutate]
|
||||
);
|
||||
|
||||
if (isSelfHosted) {
|
||||
return [selector(data), () => {}] as const;
|
||||
if (!hasPaymentFeature) {
|
||||
return [null, () => {}] as const;
|
||||
}
|
||||
|
||||
return [selector(data), set] as const;
|
||||
|
||||
Reference in New Issue
Block a user