-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebApp Redesign: Interact with sites list via redux (#1679)
## Motivation for the change, related issues This is a PR to start exploring site storage. In order to start from a concrete place, this PR starts by setting up the site manager sidebar to interact with the sites list via redux. Related to #1659 ## Implementation details This PR adds a `site-storage` module that provides functions for adding, removing, and listing sites. It currently only supports writing to sites stored in OPFS. Today, Safari only appears to support writing to OPFS files from worker threads, so this PR adds a `site-storage-metadata-worker.ts` module that the UI thread can spawn to write site metadata to OPFS. This PR adds a `siteListing` property to our web app's redux state. It looks like: ```ts { status: SiteListingStatus; sites: SiteInfo[]; } ``` `SiteListingStatus` can reflect whether the listing is loading, loaded, or in an error state. The site-manager-sidebar has been updated to select sites state from redux and interact with the sites list via redux actions. Currently, the loading status is ignored, but we should show it to the user in a follow-up PR. ## Testing Instructions (or ideally a Blueprint) Run `npm run dev` and interact with the sites list in Chrome and Safari. (Unfortunately, Firefox does not yet support loading Service Worker modules, and that feature is required for our current dev setup)
- Loading branch information
1 parent
6be65fb
commit aea0181
Showing
8 changed files
with
531 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/playground/website/src/lib/site-storage-metadata-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* This worker module exists to allow writing file content to OPFS from the | ||
* main browser thread. Today (2024-08-17), Safari only appears to support | ||
* writing to OPFS via createSyncAccessHandle(), and that is only supported | ||
* within dedicated workers. | ||
* | ||
* This worker exists so non-worker threads can trigger writing to OPFS files. | ||
*/ | ||
onmessage = async function (event: MessageEvent) { | ||
const filePath: string = event.data.path; | ||
const content: string = event.data.content; | ||
|
||
const pathParts = filePath.split('/').filter((p) => p.length > 0); | ||
|
||
const fileName = pathParts.pop(); | ||
if (fileName === undefined) { | ||
throw new Error(`Invalid path: '${filePath}'`); | ||
} | ||
|
||
let parentDirHandle = await navigator.storage.getDirectory(); | ||
for (const part of pathParts) { | ||
parentDirHandle = await parentDirHandle.getDirectoryHandle(part); | ||
} | ||
|
||
const fileHandle = await parentDirHandle.getFileHandle(fileName, { | ||
create: true, | ||
}); | ||
|
||
const syncAccessHandle = await fileHandle.createSyncAccessHandle(); | ||
try { | ||
const encodedContent = new TextEncoder().encode(content); | ||
syncAccessHandle.write(encodedContent); | ||
postMessage('done'); | ||
} finally { | ||
syncAccessHandle.close(); | ||
} | ||
}; | ||
postMessage('ready'); |
Oops, something went wrong.