Files
quick-dataset/src/routes/create/+page.svelte

116 lines
3.3 KiB
Svelte

<script lang="ts">
import { afterNavigate, goto } from '$app/navigation';
import Close from '$lib/components/icons/Close.svelte';
import { openedDatasetDir } from '$lib/stores/dataset';
import { currentActivate } from '$lib/stores/navigate';
import type { DatasetMeta } from '$lib/types/meta';
import { join } from '@tauri-apps/api/path';
import { message, open } from '@tauri-apps/plugin-dialog';
import { readDir, writeTextFile } from '@tauri-apps/plugin-fs';
import { isNil } from 'es-toolkit';
afterNavigate(() => {
currentActivate.set('start');
});
let storePath = $state('');
let datasetName = $state('');
async function backToBoot() {
await goto('/boot');
}
async function selectStorePath() {
try {
const selectResult = await open({
directory: true,
multiple: false,
title: 'Select Dataset store path',
});
let selectedPath = '';
if (Array.isArray(selectResult)) {
selectedPath = selectResult[0] ?? '';
} else if (typeof selectResult === 'string') {
selectedPath = selectResult;
} else {
return;
}
if (!selectedPath) {
return;
}
const entries = await readDir(selectedPath);
if (entries.length > 0) {
await message('The selected directory is not empty. Please choose an empty directory.', {
kind: 'warning',
title: 'Directory Unavailable',
});
return;
}
storePath = selectedPath;
} catch (error) {
console.error('Failed to select store path:', error);
}
}
async function createDataset() {
if (!isNil(storePath) && !isNil(datasetName) && storePath.trim() && datasetName.trim()) {
const datasetMeta: DatasetMeta = {
name: datasetName.trim(),
targetModel: '',
loraType: '',
unifiedImageSize: true,
unifiedImageRatio: true,
imageSize: [512, 512],
triggerWords: [],
};
try {
const metaPath = await join(storePath, 'meta.json');
await writeTextFile(metaPath, JSON.stringify(datasetMeta, null, 2));
openedDatasetDir.set(storePath);
await goto('/dataset');
} catch (error) {
console.error('Failed to create dataset:', error);
await message('Failed to create dataset. Please check permissions and try again.', {
kind: 'error',
title: 'Create Dataset Failed',
});
}
} else {
await message('Store path and dataset name are required.', {
kind: 'warning',
title: 'Missing Required Fields',
});
}
}
</script>
<button
class="btn btn-circle btn-error btn-sm fixed top-12 right-6 z-10"
title="Back to boot"
aria-label="Back to boot"
onclick={backToBoot}>
<Close width="24" />
</button>
<main class="flex flex-col gap-2 p-4 self-center">
<h2>Create new dataset</h2>
<div class="join">
<label class="input input-md focus-within:outline-0 w-fit join-item">
<span class="label min-w-[10em]">Store Path</span>
<input type="text" class="min-w-[25em]" readonly value={storePath} />
</label>
<button class="btn join-item" onclick={selectStorePath}>Select</button>
</div>
<label class="input input-md focus-within:outline-0 w-fit">
<span class="label min-w-[10em]">Dataset Name</span>
<input type="text" class="min-w-[20em]" bind:value={datasetName} />
</label>
<button class="btn btn-primary btn-md w-fit self-center" onclick={createDataset}>Create</button>
</main>