Skip to content

Commit

Permalink
perf: Improve cache when upload / delete plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Sep 14, 2024
1 parent 2bd7195 commit 83faa99
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AdminAuthGuards, OnlyForDevelopment } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

import { ShowAdminPlugins } from '../show/show.dto';
import { UploadAdminPluginsArgs } from './upload.dto';
import { UploadAdminPluginsService } from './upload.service';

Expand Down
27 changes: 26 additions & 1 deletion packages/backend/src/core/admin/plugins/upload/upload.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { core_plugins } from '@/database/schema/plugins';
import { generateRandomString } from '@/functions/generate-random-string';
import { InternalDatabaseService } from '@/utils/database/internal_database.service';
import { Injectable } from '@nestjs/common';
import { eq } from 'drizzle-orm';
import { existsSync } from 'fs';
import { copyFile, cp, mkdir, readFile, rm } from 'fs/promises';
import { join } from 'path';
Expand Down Expand Up @@ -240,6 +242,29 @@ export class UploadAdminPluginsService {
await this.removeTempFolder();
this.changeFilesService.setServerToRestartConfig();

return 'Success!';
if (code) {
await this.databaseService.db
.update(core_plugins)
.set({
updated: new Date(),
name: config.name,
description: config.description,
support_url: config.support_url,
author: config.author,
author_url: config.author_url,
version: config.version,
version_code: config.version_code,
allow_default: config.allow_default,
})
.where(eq(core_plugins.code, code));

return 'Plugin updated';
}

await this.databaseService.db.insert(core_plugins).values({
...config,
});

return 'Plugin uploaded';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export const ItemPreviewFilesInput = ({
const t = useTranslations('core');

const size = React.useMemo(() => {
const sizeInKb = file instanceof File ? file.size : file.file_size / 1024;
if (sizeInKb < 1024) return `${sizeInKb.toFixed(2)} KB`;
const sizeInBytes = file instanceof File ? file.size : file.file_size;
const sizeInKb = sizeInBytes / 1024;
if (sizeInBytes < 2048) return `${Math.ceil(sizeInKb)} KB`;

const sizeInMb = sizeInKb / 1024;
if (sizeInMb < 1024) return `${sizeInMb.toFixed(2)} MB`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Metadata } from 'next';
import { useTranslations } from 'next-intl';
import { getTranslations } from 'next-intl/server';

import { WarnReqRestartServer } from '../plugins/warn-req-restart-server';
import { ActionsDiagnosticTools } from './actions/actions';

export const generateMetadataDiagnosticAdmin = async (): Promise<Metadata> => {
Expand All @@ -17,8 +18,12 @@ export const DiagnosticToolsView = () => {
const t = useTranslations('admin.core.diagnostic');

return (
<HeaderContent desc={t('desc')} h1={t('title')}>
<ActionsDiagnosticTools />
</HeaderContent>
<>
<HeaderContent desc={t('desc')} h1={t('title')}>
<ActionsDiagnosticTools />
</HeaderContent>

<WarnReqRestartServer />
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Admin__Core_Plugins__ShowQueryVariables,
} from '@/graphql/queries/admin/plugins/admin__core_plugins__show.generated';
import { ShowAdminPluginsSortingColumnEnum } from '@/graphql/types';
import { CONFIG } from '@/helpers/config-with-env';
import { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';

Expand All @@ -29,7 +28,10 @@ const getData = async (variables: Admin__Core_Plugins__ShowQueryVariables) => {
>({
query: Admin__Core_Plugins__Show,
variables,
cache: CONFIG.node_development ? 'default' : 'force-cache',
cache: 'force-cache',
next: {
tags: ['Admin__Core_Plugins__Show'],
},
});

return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Admin__Core_Plugins__DeleteMutation,
Admin__Core_Plugins__DeleteMutationVariables,
} from '@/graphql/mutations/admin/plugins/admin__core_plugins__delete.generated';
import { revalidatePath } from 'next/cache';
import { revalidatePath, revalidateTag } from 'next/cache';

export const mutationApi = async (
variables: Admin__Core_Plugins__DeleteMutationVariables,
Expand All @@ -28,4 +28,5 @@ export const mutationApi = async (
}

revalidatePath('/', 'layout');
revalidateTag('Admin__Core_Plugins__Show');
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export const useDeletePluginAdmin = ({ code }: { code: string }) => {
toast.success(t('success.title'), {
description: t('success.desc'),
});
window.location.reload();

// Wait 3 seconds before reloading the page
setTimeout(() => {
window.location.reload();
}, 3000);
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Admin__Core_Plugins__UploadMutation,
Admin__Core_Plugins__UploadMutationVariables,
} from '@/graphql/mutations/admin/plugins/admin__core_plugins__upload.generated';
import { revalidatePath } from 'next/cache';
import { revalidatePath, revalidateTag } from 'next/cache';

export const mutationApi = async (formData: FormData) => {
const files = formData.get('file') as File;
Expand Down Expand Up @@ -36,4 +36,5 @@ export const mutationApi = async (formData: FormData) => {
}

revalidatePath('/', 'layout');
revalidateTag('Admin__Core_Plugins__Show');
};
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ export const useUploadPluginAdmin = ({ data }: UploadPluginAdminProps) => {
return;
}

toast.success(t(data ? 'success.update' : 'success.title'));
setOpen?.(false);
window.location.reload();
toast.success(t(data ? 'success.update' : 'success.title'), {
description: t('success.desc'),
});

// Wait 3 seconds before reloading the page
setTimeout(() => {
window.location.reload();
}, 3000);
};

return { onSubmit, formSchema };
Expand Down

0 comments on commit 83faa99

Please sign in to comment.