🚑 Fix UUID by using crypto

This commit is contained in:
ajnart
2022-05-21 01:26:24 +02:00
parent ecfb89de40
commit c3b11be2d0
5 changed files with 21 additions and 4 deletions

View File

@@ -122,7 +122,7 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } &
const form = useForm({ const form = useForm({
initialValues: { initialValues: {
id: props.id ?? Date.now(), id: props.id ?? crypto.randomUUID(),
type: props.type ?? 'Other', type: props.type ?? 'Other',
name: props.name ?? '', name: props.name ?? '',
icon: props.icon ?? '/favicon.svg', icon: props.icon ?? '/favicon.svg',

View File

@@ -7,6 +7,7 @@ import { useRouter } from 'next/router';
import { setCookies } from 'cookies-next'; import { setCookies } from 'cookies-next';
import { useConfig } from '../../tools/state'; import { useConfig } from '../../tools/state';
import { Config } from '../../tools/types'; import { Config } from '../../tools/types';
import { migrateToIdConfig } from '../../tools/migrate';
function getIconColor(status: DropzoneStatus, theme: MantineTheme) { function getIconColor(status: DropzoneStatus, theme: MantineTheme) {
return status.accepted return status.accepted
@@ -84,7 +85,8 @@ export default function LoadConfigComponent(props: any) {
message: undefined, message: undefined,
}); });
setCookies('config-name', newConfig.name, { maxAge: 60 * 60 * 24 * 30 }); setCookies('config-name', newConfig.name, { maxAge: 60 * 60 * 24 * 30 });
setConfig(newConfig); const migratedConfig = migrateToIdConfig(newConfig);
setConfig(migratedConfig);
}); });
}} }}
accept={['application/json']} accept={['application/json']}

View File

@@ -7,6 +7,7 @@ import AppShelf from '../components/AppShelf/AppShelf';
import LoadConfigComponent from '../components/Config/LoadConfig'; import LoadConfigComponent from '../components/Config/LoadConfig';
import { Config } from '../tools/types'; import { Config } from '../tools/types';
import { useConfig } from '../tools/state'; import { useConfig } from '../tools/state';
import { migrateToIdConfig } from '../tools/migrate';
export async function getServerSideProps({ export async function getServerSideProps({
req, req,
@@ -48,7 +49,8 @@ export default function HomePage(props: any) {
const { config: initialConfig }: { config: Config } = props; const { config: initialConfig }: { config: Config } = props;
const { config, loadConfig, setConfig, getConfigs } = useConfig(); const { config, loadConfig, setConfig, getConfigs } = useConfig();
useEffect(() => { useEffect(() => {
setConfig(initialConfig); const migratedConfig = migrateToIdConfig(initialConfig);
setConfig(migratedConfig);
}, [initialConfig]); }, [initialConfig]);
return ( return (
<> <>

13
src/tools/migrate.ts Normal file
View File

@@ -0,0 +1,13 @@
import { Config } from './types';
export function migrateToIdConfig(config: Config): Config {
// Set the config and add an ID to all the services that don't have one
const services = config.services.map((service) => ({
...service,
id: service.id ?? crypto.randomUUID(),
}));
return {
...config,
services,
};
}

View File

@@ -29,7 +29,7 @@ export type ServiceType =
| 'Emby'; | 'Emby';
export interface serviceItem { export interface serviceItem {
id: number; id: string;
name: string; name: string;
type: string; type: string;
url: string; url: string;