{
const files = e.target.files;
if (!files || files.length === 0) {
return;
}
let reader = new FileReader();
reader.onload = async (event) => {
try {
let suggestions = JSON.parse(event.target.result);
if (
!Array.isArray(suggestions) ||
suggestions.some(
(s) =>
!s ||
typeof s.content !== 'string' ||
(s.title != null &&
typeof s.title !== 'string' &&
(!Array.isArray(s.title) || s.title.some((part) => typeof part !== 'string')))
)
)
throw new Error('Invalid prompt suggestions');
suggestions = suggestions.map((s) => {
if (typeof s.title === 'string') {
s.title = [s.title, ''];
} else if (!Array.isArray(s.title)) {
s.title = ['', ''];
}
return s;
});
updateSuggestions([...promptSuggestions, ...suggestions]);
} catch (error) {
toast.error($i18n.t('Invalid JSON file'));
return;
}
};
reader.readAsText(files[0]);
e.target.value = ''; // Reset the input value
}}
/>
{#if promptSuggestions.length}
{/if}