Make migration only start once the "Next" button is pressed.

This commit is contained in:
ajnart
2023-01-08 13:00:25 +09:00
parent 6586914ff5
commit c2a9ff44fd
2 changed files with 29 additions and 17 deletions

21
src/pages/api/migrate.ts Normal file
View File

@@ -0,0 +1,21 @@
import { NextApiRequest, NextApiResponse } from 'next';
import fs from 'fs';
import { backendMigrateConfig } from '../../tools/config/backendMigrateConfig';
export default async (req: NextApiRequest, res: NextApiResponse) => {
// Get all the configs in the /data/configs folder
const configs = fs.readdirSync('./data/configs');
// If there is no config, redirect to the index
configs.every((config) => {
const configData = JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8'));
if (!configData.schemaVersion) {
// Migrate the config
backendMigrateConfig(configData, config.replace('.json', ''));
}
return config;
});
return res.status(200).json({
success: true,
message: 'Configs migrated',
});
};

View File

@@ -40,6 +40,7 @@ import { motion } from 'framer-motion';
import { Logo } from '../components/layout/Logo'; import { Logo } from '../components/layout/Logo';
import { usePrimaryGradient } from '../components/layout/useGradient'; import { usePrimaryGradient } from '../components/layout/useGradient';
import { backendMigrateConfig } from '../tools/config/backendMigrateConfig'; import { backendMigrateConfig } from '../tools/config/backendMigrateConfig';
import axios from 'axios';
const useStyles = createStyles((theme) => ({ const useStyles = createStyles((theme) => ({
root: { root: {
@@ -245,8 +246,7 @@ function SwitchToggle() {
export async function getServerSideProps({ req, res, locale }: GetServerSidePropsContext) { export async function getServerSideProps({ req, res, locale }: GetServerSidePropsContext) {
// Get all the configs in the /data/configs folder // Get all the configs in the /data/configs folder
const configs = await fs.readdirSync('./data/configs'); const configs = fs.readdirSync('./data/configs');
// If there is no config, redirect to the index
if (configs.length === 0) { if (configs.length === 0) {
res.writeHead(302, { res.writeHead(302, {
Location: '/', Location: '/',
@@ -265,27 +265,15 @@ export async function getServerSideProps({ req, res, locale }: GetServerSideProp
}); });
res.end(); res.end();
return { return {
props: { processed: true,
...(await serverSideTranslations(locale ?? 'en', [])),
},
}; };
} }
configs.every((config) => {
const configData = JSON.parse(fs.readFileSync(`./data/configs/${config}`, 'utf8'));
if (!configData.schemaVersion) {
// Migrate the config
backendMigrateConfig(configData, config.replace('.json', ''));
}
return config;
});
return { return {
props: { props: {
configs: configs.map( configs: configs.map(
// Get all the file names in ./data/configs // Get all the file names in ./data/configs
(config) => config.replace('.json', '') (config) => config.replace('.json', '')
), ),
...(await serverSideTranslations(locale!, [])), ...(await serverSideTranslations(locale!, [])),
// Will be passed to the page component as props // Will be passed to the page component as props
}, },
@@ -309,6 +297,10 @@ export function StatsCard({
const [treatedConfigs, setTreatedConfigs] = useState<string[]>([]); const [treatedConfigs, setTreatedConfigs] = useState<string[]>([]);
// Stop the progress at 100% // Stop the progress at 100%
useEffect(() => { useEffect(() => {
const data = axios.post('/api/migrate').then((response) => {
setProgress(100);
});
const interval = setInterval(() => { const interval = setInterval(() => {
if (configs.length === 0) { if (configs.length === 0) {
clearInterval(interval); clearInterval(interval);
@@ -321,8 +313,7 @@ export function StatsCard({
configs.pop(); configs.pop();
}, 500); }, 500);
return () => clearInterval(interval); return () => clearInterval(interval);
}, []); }, [configs]);
// TODO: Actually use the configs
return ( return (
<Paper radius="md" className={classes.card} mt={ICON_SIZE / 3}> <Paper radius="md" className={classes.card} mt={ICON_SIZE / 3}>