-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optional payment for frontend (#5056)
- Loading branch information
1 parent
13e7121
commit f04ec50
Showing
14 changed files
with
146 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ResolveField, Resolver } from '@nestjs/graphql'; | ||
|
||
import { UserSubscriptionType } from './payment/resolver'; | ||
import { | ||
SubscriptionPlan, | ||
SubscriptionRecurring, | ||
SubscriptionStatus, | ||
} from './payment/service'; | ||
import { UserType } from './users'; | ||
|
||
const YEAR = 1000 * 60 * 60 * 24 * 30 * 12; | ||
|
||
@Resolver(() => UserType) | ||
export class SelfHostedDummyResolver { | ||
private readonly start = new Date(); | ||
private readonly end = new Date(Number(this.start) + YEAR); | ||
constructor() {} | ||
|
||
@ResolveField(() => UserSubscriptionType) | ||
async subscription() { | ||
return { | ||
stripeSubscriptionId: 'dummy', | ||
plan: SubscriptionPlan.SelfHosted, | ||
recurring: SubscriptionRecurring.Yearly, | ||
status: SubscriptionStatus.Active, | ||
start: this.start, | ||
end: this.end, | ||
createdAt: this.start, | ||
updatedAt: this.start, | ||
}; | ||
} | ||
} | ||
|
||
@Module({ | ||
providers: [SelfHostedDummyResolver], | ||
}) | ||
export class SelfHostedModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,7 @@ enum SubscriptionPlan { | |
Pro | ||
Team | ||
Enterprise | ||
SelfHosted | ||
} | ||
|
||
type UserSubscription { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/frontend/core/src/hooks/affine/use-server-flavor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { serverConfigQuery } from '@affine/graphql'; | ||
import { useQuery } from '@affine/workspace/affine/gql'; | ||
import type { BareFetcher, Middleware } from 'swr'; | ||
|
||
const wrappedFetcher = (fetcher: BareFetcher<any> | null, ...args: any[]) => | ||
fetcher?.(...args).catch(() => null); | ||
|
||
const errorHandler: Middleware = useSWRNext => (key, fetcher, config) => { | ||
return useSWRNext(key, wrappedFetcher.bind(null, fetcher), config); | ||
}; | ||
|
||
export const useServerFlavor = () => { | ||
const { data: config, error } = useQuery( | ||
{ query: serverConfigQuery }, | ||
{ use: [errorHandler] } | ||
); | ||
|
||
if (error || !config) { | ||
return 'local'; | ||
} | ||
|
||
return config.serverConfig.flavor; | ||
}; | ||
|
||
export const useSelfHosted = () => { | ||
const serverFlavor = useServerFlavor(); | ||
|
||
return ['local', 'selfhosted'].includes(serverFlavor); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters