Skip to content

Commit

Permalink
Persist datasets to data file
Browse files Browse the repository at this point in the history
  • Loading branch information
abrenneke committed Sep 19, 2023
1 parent 18247f4 commit 0079941
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 109 deletions.
27 changes: 25 additions & 2 deletions packages/app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
windows_subsystem = "windows"
)]

use tauri::{CustomMenuItem, Menu, MenuItem, Submenu};
use std::path::Path;

use tauri::{AppHandle, CustomMenuItem, InvokeError, Manager, Menu, MenuItem, Submenu};
mod plugins;

fn main() {
Expand All @@ -12,7 +14,8 @@ fn main() {
.plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![
get_environment_variable,
plugins::extract_package_plugin_tarball
plugins::extract_package_plugin_tarball,
allow_data_file_scope
])
.menu(create_menu())
.on_menu_event(|event| match event.menu_item_id() {
Expand All @@ -34,6 +37,26 @@ fn get_environment_variable(name: &str) -> String {
std::env::var(name).unwrap_or_default()
}

#[tauri::command]
fn allow_data_file_scope(
app_handle: AppHandle,
project_file_path: &str,
) -> Result<(), InvokeError> {
let scope = app_handle.fs_scope();

let folder_path = Path::new(project_file_path).parent().unwrap();
let file_name_no_extension = Path::new(project_file_path)
.file_stem()
.unwrap()
.to_str()
.unwrap();
let data_file_path = folder_path.join(format!("{}.rivet-data", file_name_no_extension));

scope.allow_file(&data_file_path)?;

Ok(())
}

fn create_menu() -> Menu {
let about_menu = Submenu::new(
"App",
Expand Down
11 changes: 10 additions & 1 deletion packages/app/src/hooks/useDatasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import { useRecoilState } from 'recoil';
export function useDatasets(projectId: ProjectId) {
const [datasets, updateDatasets] = useRecoilState(datasetsState);

const initDatasets = async () => {
try {
await datasetProvider.loadDatasets(projectId);
await reloadDatasets();
} catch (err) {
toast.error(getError(err).message);
}
};

const reloadDatasets = async () => {
try {
const datasets = await datasetProvider.getDatasetsForProject(projectId);
Expand All @@ -18,7 +27,7 @@ export function useDatasets(projectId: ProjectId) {
};

useEffect(() => {
reloadDatasets();
initDatasets();
}, [projectId]);

const putDataset = async (dataset: DatasetMetadata) => {
Expand Down
47 changes: 25 additions & 22 deletions packages/app/src/hooks/useLoadProject.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useSetRecoilState } from 'recoil';
import { loadedProjectState, projectDataState, projectState } from '../state/savedGraphs.js';
import { emptyNodeGraph } from '@ironclad/rivet-core';
import { emptyNodeGraph, getError } from '@ironclad/rivet-core';
import { graphState } from '../state/graph.js';
import { ioProvider } from '../utils/globals.js';
import { trivetState } from '../state/trivet.js';
import { useStaticDataDatabase } from './useStaticDataDatabase';
import { entries } from '../../../core/src/utils/typeSafety';
import { useSetStaticData } from './useSetStaticData';
import { toast } from 'react-toastify';

export function useLoadProject() {
const setProject = useSetRecoilState(projectState);
Expand All @@ -15,30 +14,34 @@ export function useLoadProject() {
const setTrivetState = useSetRecoilState(trivetState);
const setStaticData = useSetStaticData();

return () => {
ioProvider.loadProjectData(({ project, testData, path }) => {
const { data, ...projectData } = project;
return async () => {
try {
await ioProvider.loadProjectData(({ project, testData, path }) => {
const { data, ...projectData } = project;

setProject(projectData);
setProject(projectData);

if (data) {
setStaticData(data);
}
if (data) {
setStaticData(data);
}

setGraphData(emptyNodeGraph());
setGraphData(emptyNodeGraph());

setLoadedProjectState({
path,
loaded: true,
});
setLoadedProjectState({
path,
loaded: true,
});

setTrivetState({
testSuites: testData.testSuites,
selectedTestSuiteId: undefined,
editingTestCaseId: undefined,
recentTestResults: undefined,
runningTests: false,
setTrivetState({
testSuites: testData.testSuites,
selectedTestSuiteId: undefined,
editingTestCaseId: undefined,
recentTestResults: undefined,
runningTests: false,
});
});
});
} catch (err) {
toast.error(`Failed to load project: ${getError(err).message}`);
}
};
}
25 changes: 24 additions & 1 deletion packages/app/src/hooks/useSaveProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRecoilState, useRecoilValue } from 'recoil';
import { loadedProjectState, projectState } from '../state/savedGraphs.js';
import { useSaveCurrentGraph } from './useSaveCurrentGraph.js';
import { produce } from 'immer';
import { toast } from 'react-toastify';
import { toast, Id as ToastId } from 'react-toastify';
import { ioProvider } from '../utils/globals.js';
import { trivetState } from '../state/trivet.js';

Expand All @@ -23,7 +23,19 @@ export function useSaveProject() {
draft.graphs[savedGraph.metadata!.id!] = savedGraph;
});

// Large datasets can save slowly because of indexeddb, so show a "saving..." toast if it's a slow save
let saving: ToastId | undefined;
const savingTimeout = setTimeout(() => {
saving = toast.info('Saving project');
}, 500);

await ioProvider.saveProjectDataNoPrompt(newProject, { testSuites }, loadedProject.path);

if (saving != null) {
toast.dismiss(saving);
}
clearTimeout(savingTimeout);

toast.success('Project saved');
setLoadedProject({
loaded: true,
Expand All @@ -38,8 +50,19 @@ export function useSaveProject() {
draft.graphs[savedGraph.metadata!.id!] = savedGraph;
});

// Large datasets can save slowly because of indexeddb, so show a "saving..." toast if it's a slow save
let saving: ToastId | undefined;
const savingTimeout = setTimeout(() => {
saving = toast.info('Saving project');
}, 500);

const filePath = await ioProvider.saveProjectData(newProject, { testSuites });

if (saving != null) {
toast.dismiss(saving);
}
clearTimeout(savingTimeout);

if (filePath) {
toast.success('Project saved');
setLoadedProject({
Expand Down
Loading

0 comments on commit 0079941

Please sign in to comment.