mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
fix: limits syncing issue (#1195)
Exposes `refreshLimits()` to be able to keep the limit in sync when deleting/creating a document.
This commit is contained in:
@ -4,6 +4,7 @@ import { useRouter } from 'next/navigation';
|
|||||||
|
|
||||||
import { match } from 'ts-pattern';
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
|
import { useLimits } from '@documenso/ee/server-only/limits/provider/client';
|
||||||
import { DocumentStatus } from '@documenso/prisma/client';
|
import { DocumentStatus } from '@documenso/prisma/client';
|
||||||
import { trpc as trpcReact } from '@documenso/trpc/react';
|
import { trpc as trpcReact } from '@documenso/trpc/react';
|
||||||
import { Alert, AlertDescription } from '@documenso/ui/primitives/alert';
|
import { Alert, AlertDescription } from '@documenso/ui/primitives/alert';
|
||||||
@ -41,6 +42,7 @@ export const DeleteDocumentDialog = ({
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
const { refreshLimits } = useLimits();
|
||||||
|
|
||||||
const [inputValue, setInputValue] = useState('');
|
const [inputValue, setInputValue] = useState('');
|
||||||
const [isDeleteEnabled, setIsDeleteEnabled] = useState(status === DocumentStatus.DRAFT);
|
const [isDeleteEnabled, setIsDeleteEnabled] = useState(status === DocumentStatus.DRAFT);
|
||||||
@ -48,6 +50,7 @@ export const DeleteDocumentDialog = ({
|
|||||||
const { mutateAsync: deleteDocument, isLoading } = trpcReact.document.deleteDocument.useMutation({
|
const { mutateAsync: deleteDocument, isLoading } = trpcReact.document.deleteDocument.useMutation({
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
router.refresh();
|
router.refresh();
|
||||||
|
void refreshLimits();
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: 'Document deleted',
|
title: 'Document deleted',
|
||||||
|
|||||||
@ -36,7 +36,7 @@ export const UploadDocument = ({ className, team }: UploadDocumentProps) => {
|
|||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
const { quota, remaining } = useLimits();
|
const { quota, remaining, refreshLimits } = useLimits();
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
@ -71,6 +71,8 @@ export const UploadDocument = ({ className, team }: UploadDocumentProps) => {
|
|||||||
teamId: team?.id,
|
teamId: team?.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
void refreshLimits();
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: 'Document uploaded',
|
title: 'Document uploaded',
|
||||||
description: 'Your document has been uploaded successfully.',
|
description: 'Your document has been uploaded successfully.',
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { createContext, useContext, useEffect, useState } from 'react';
|
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { equals } from 'remeda';
|
import { equals } from 'remeda';
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ import { getLimits } from '../client';
|
|||||||
import { FREE_PLAN_LIMITS } from '../constants';
|
import { FREE_PLAN_LIMITS } from '../constants';
|
||||||
import type { TLimitsResponseSchema } from '../schema';
|
import type { TLimitsResponseSchema } from '../schema';
|
||||||
|
|
||||||
export type LimitsContextValue = TLimitsResponseSchema;
|
export type LimitsContextValue = TLimitsResponseSchema & { refreshLimits: () => Promise<void> };
|
||||||
|
|
||||||
const LimitsContext = createContext<LimitsContextValue | null>(null);
|
const LimitsContext = createContext<LimitsContextValue | null>(null);
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ export const useLimits = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type LimitsProviderProps = {
|
export type LimitsProviderProps = {
|
||||||
initialValue?: LimitsContextValue;
|
initialValue?: TLimitsResponseSchema;
|
||||||
teamId?: number;
|
teamId?: number;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
};
|
};
|
||||||
@ -38,7 +38,7 @@ export const LimitsProvider = ({
|
|||||||
}: LimitsProviderProps) => {
|
}: LimitsProviderProps) => {
|
||||||
const [limits, setLimits] = useState(() => initialValue);
|
const [limits, setLimits] = useState(() => initialValue);
|
||||||
|
|
||||||
const refreshLimits = async () => {
|
const refreshLimits = useCallback(async () => {
|
||||||
const newLimits = await getLimits({ teamId });
|
const newLimits = await getLimits({ teamId });
|
||||||
|
|
||||||
setLimits((oldLimits) => {
|
setLimits((oldLimits) => {
|
||||||
@ -48,11 +48,11 @@ export const LimitsProvider = ({
|
|||||||
|
|
||||||
return newLimits;
|
return newLimits;
|
||||||
});
|
});
|
||||||
};
|
}, [teamId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void refreshLimits();
|
void refreshLimits();
|
||||||
}, []);
|
}, [refreshLimits]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onFocus = () => {
|
const onFocus = () => {
|
||||||
@ -64,7 +64,16 @@ export const LimitsProvider = ({
|
|||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('focus', onFocus);
|
window.removeEventListener('focus', onFocus);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [refreshLimits]);
|
||||||
|
|
||||||
return <LimitsContext.Provider value={limits}>{children}</LimitsContext.Provider>;
|
return (
|
||||||
|
<LimitsContext.Provider
|
||||||
|
value={{
|
||||||
|
...limits,
|
||||||
|
refreshLimits,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</LimitsContext.Provider>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
import { headers } from 'next/headers';
|
import { headers } from 'next/headers';
|
||||||
|
|
||||||
import { getLimits } from '../client';
|
import { getLimits } from '../client';
|
||||||
import type { LimitsContextValue } from './client';
|
|
||||||
import { LimitsProvider as ClientLimitsProvider } from './client';
|
import { LimitsProvider as ClientLimitsProvider } from './client';
|
||||||
|
|
||||||
export type LimitsProviderProps = {
|
export type LimitsProviderProps = {
|
||||||
@ -14,7 +13,7 @@ export type LimitsProviderProps = {
|
|||||||
export const LimitsProvider = async ({ children, teamId }: LimitsProviderProps) => {
|
export const LimitsProvider = async ({ children, teamId }: LimitsProviderProps) => {
|
||||||
const requestHeaders = Object.fromEntries(headers().entries());
|
const requestHeaders = Object.fromEntries(headers().entries());
|
||||||
|
|
||||||
const limits: LimitsContextValue = await getLimits({ headers: requestHeaders, teamId });
|
const limits = await getLimits({ headers: requestHeaders, teamId });
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ClientLimitsProvider initialValue={limits} teamId={teamId}>
|
<ClientLimitsProvider initialValue={limits} teamId={teamId}>
|
||||||
|
|||||||
Reference in New Issue
Block a user