-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
project save/load to browser #158
base: main
Are you sure you want to change the base?
Changes from 5 commits
6819225
fa97940
cc62b22
b415613
d0eb52a
e8fe625
041176a
67b7ba5
8188815
1a098fb
6ba92cf
ffb6874
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
export class BrowserProjectsInterface { | ||
constructor( | ||
private dbName: string = "stan-playground", | ||
private storeName: string = "projects", | ||
) {} | ||
async loadProject(title: string) { | ||
const objectStore = await this.openObjectStore("readonly"); | ||
const filename = `${title}.json`; | ||
const content = await this.getTextFile(objectStore, filename); | ||
if (!content) return null; | ||
return JSON.parse(content); | ||
} | ||
async saveProject(title: string, fileManifest: { [name: string]: string }) { | ||
const objectStore = await this.openObjectStore("readwrite"); | ||
const filename = `${title}.json`; | ||
return await this.setTextFile( | ||
objectStore, | ||
filename, | ||
JSON.stringify(fileManifest, null, 2), | ||
); | ||
} | ||
async listProjects(): Promise<string[]> { | ||
const objectStore = await this.openObjectStore("readonly"); | ||
return new Promise<string[]>((resolve, reject) => { | ||
const request = objectStore.getAllKeys(); | ||
request.onsuccess = () => { | ||
resolve( | ||
request.result.map((key) => { | ||
return key.toString().replace(/\.json$/, ""); | ||
}), | ||
); | ||
}; | ||
request.onerror = () => { | ||
reject(request.error); | ||
}; | ||
}); | ||
} | ||
async deleteProject(title: string) { | ||
const objectStore = await this.openObjectStore("readwrite"); | ||
const filename = `${title}.json`; | ||
await this.deleteTextFile(objectStore, filename); | ||
} | ||
private async openDatabase() { | ||
return new Promise<IDBDatabase>((resolve, reject) => { | ||
const request = indexedDB.open(this.dbName); | ||
request.onupgradeneeded = () => { | ||
const db = request.result; | ||
if (!db.objectStoreNames.contains(this.storeName)) { | ||
db.createObjectStore(this.storeName, { keyPath: "name" }); | ||
} | ||
}; | ||
request.onsuccess = () => { | ||
resolve(request.result); | ||
}; | ||
request.onerror = () => { | ||
reject(request.error); | ||
}; | ||
}); | ||
} | ||
private async openObjectStore(mode: IDBTransactionMode) { | ||
const db = await this.openDatabase(); | ||
const transaction = db.transaction(this.storeName, mode); | ||
return transaction.objectStore(this.storeName); | ||
} | ||
private async getTextFile(objectStore: IDBObjectStore, filename: string) { | ||
return new Promise<string | null>((resolve, reject) => { | ||
const getRequest = objectStore.get(filename); | ||
getRequest.onsuccess = () => { | ||
resolve(getRequest.result?.content || null); | ||
}; | ||
getRequest.onerror = () => { | ||
reject(getRequest.error); | ||
}; | ||
}); | ||
} | ||
private async setTextFile( | ||
objectStore: IDBObjectStore, | ||
filename: string, | ||
content: string, | ||
) { | ||
return new Promise<void>((resolve, reject) => { | ||
const file = { name: filename, content: content }; | ||
const putRequest = objectStore.put(file); | ||
putRequest.onsuccess = () => { | ||
resolve(); | ||
}; | ||
putRequest.onerror = () => { | ||
reject(putRequest.error); | ||
}; | ||
}); | ||
} | ||
private async deleteTextFile(objectStore: IDBObjectStore, filename: string) { | ||
return new Promise<void>((resolve, reject) => { | ||
const deleteRequest = objectStore.delete(filename); | ||
deleteRequest.onsuccess = () => { | ||
resolve(); | ||
}; | ||
deleteRequest.onerror = () => { | ||
reject(deleteRequest.error); | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
export default BrowserProjectsInterface; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import Button from "@mui/material/Button"; | ||
import { | ||
FieldsContentsMap, | ||
FileNames, | ||
|
@@ -8,13 +7,18 @@ import { | |
import { ProjectContext } from "@SpCore/ProjectContextProvider"; | ||
import { deserializeZipToFiles, parseFile } from "@SpCore/ProjectSerialization"; | ||
import UploadFilesArea from "@SpPages/UploadFilesArea"; | ||
import { SmallIconButton } from "@fi-sci/misc"; | ||
import { Delete } from "@mui/icons-material"; | ||
import { Link } from "@mui/material/Link"; | ||
import Button from "@mui/material/Button"; | ||
import { | ||
FunctionComponent, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
import BrowserProjectsInterface from "./BrowserProjectsInterface"; | ||
|
||
type LoadProjectWindowProps = { | ||
onClose: () => void; | ||
|
@@ -103,9 +107,37 @@ const LoadProjectWindow: FunctionComponent<LoadProjectWindowProps> = ({ | |
} | ||
}, [filesUploaded, importUploadedFiles]); | ||
|
||
const [browserProjectTitles, setBrowserProjectTitles] = useState<string[]>( | ||
[], | ||
); | ||
useEffect(() => { | ||
const bpi = new BrowserProjectsInterface(); | ||
bpi.listProjects().then((titles) => { | ||
setBrowserProjectTitles(titles); | ||
}); | ||
}, []); | ||
|
||
const handleOpenBrowserProject = useCallback( | ||
async (title: string) => { | ||
const bpi = new BrowserProjectsInterface(); | ||
const fileManifest = await bpi.loadProject(title); | ||
if (!fileManifest) { | ||
alert("Failed to load project"); | ||
return; | ||
} | ||
update({ | ||
type: "loadFiles", | ||
files: mapFileContentsToModel(fileManifest), | ||
clearExisting: true, | ||
}); | ||
onClose(); | ||
}, | ||
[update, onClose], | ||
); | ||
|
||
return ( | ||
<div> | ||
<h3>Load project</h3> | ||
<h3>Upload project</h3> | ||
<div> | ||
You can upload: | ||
<ul> | ||
|
@@ -142,6 +174,45 @@ const LoadProjectWindow: FunctionComponent<LoadProjectWindowProps> = ({ | |
</Button> | ||
</div> | ||
)} | ||
<h3>Load from browser</h3> | ||
{browserProjectTitles.length > 0 ? ( | ||
<table> | ||
<tbody> | ||
{browserProjectTitles.map((title) => ( | ||
<tr key={title}> | ||
<td> | ||
<SmallIconButton | ||
icon={<Delete />} | ||
onClick={async () => { | ||
const ok = window.confirm( | ||
`Delete project "${title}" from browser?`, | ||
); | ||
if (!ok) return; | ||
const bpi = new BrowserProjectsInterface(); | ||
await bpi.deleteProject(title); | ||
const titles = await bpi.listProjects(); | ||
setBrowserProjectTitles(titles); | ||
}} | ||
/> | ||
</td> | ||
<td> | ||
<Link | ||
onClick={() => { | ||
handleOpenBrowserProject(title); | ||
}} | ||
component="button" | ||
underline="none" | ||
> | ||
{title} | ||
</Link> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me like it would also be incredibly helpful to store a timestamp (we could put this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it would be helpful. But I'm wary about including it in meta.json as there's nothing that is enforcing the accuracy of that information if someone edits the project outside of SP. And for Gists, this information would be available independent of the content of meta.json. Let me put the timestamp in the browser storage record and we'll see how that looks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a good idea (modulo all the usual complications about timestamps/time zones) but might be for a different PR? I'd want to see the other forms of persistence be aware of it also. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Didn't see your message Jeff until just now.) I added timestamps for the saved browser projects, but it's not in meta.json. The timestamps are now shown (in the form of "time-ago-strings", like "3 min ago"). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
) : ( | ||
<div>No projects found in browser storage</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that there is a race condition here if you have two tabs open: saving (or deleting) a project in one will not update the other.
I think this is probably okay, it's not really an intended operational mode or likely to come up, but I wanted to point it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean, but this is not going to be a problem since retrieving from browser storage is fast enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's a matter of save/retrieve speed--it's if you have the window open already, there isn't anything triggering an update. (At least that's how it looked when I was playing around with it.)
Again, I think the steps to trigger are kind of far-fetched, so I'm not worried, but just want to note everything I see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see! Didn't read carefully enough :)