mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-02 20:23:32 +08:00
feature (state): revamp state management via indexedDB
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
"use strict";
|
||||
import { setup_cache_state } from ".";
|
||||
|
||||
const DB_VERSION = 4;
|
||||
const FILE_PATH = "file_path";
|
||||
@ -308,6 +308,7 @@ DataFromIndexedDB.prototype.destroy = function() {
|
||||
purgeAll(db, FILE_CONTENT);
|
||||
// We keep FILE_TAG as this was user generated and potentially frustrating
|
||||
// for users if they were to lose this
|
||||
setup_cache_state("");
|
||||
});
|
||||
done();
|
||||
|
||||
@ -330,9 +331,7 @@ export function setup_cache() {
|
||||
cache = new DataFromMemory();
|
||||
if ("indexedDB" in window && window.indexedDB !== null) {
|
||||
cache = new DataFromIndexedDB();
|
||||
return new Promise((done) => {
|
||||
cache.db.then(() => done());
|
||||
});
|
||||
return Promise.all([cache.db, setup_cache_state()])
|
||||
}
|
||||
return Promise.resolve();
|
||||
return setup_cache_state();
|
||||
}
|
||||
|
||||
25
client/helpers/cache_state.js
Normal file
25
client/helpers/cache_state.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { Session } from "../model";
|
||||
|
||||
let backendID = null;
|
||||
|
||||
export function currentShare() {
|
||||
return findParams("share");
|
||||
}
|
||||
|
||||
export function currentBackend() {
|
||||
return backendID || "";
|
||||
}
|
||||
|
||||
export function findParams(p) {
|
||||
return new window.URL(location.href).searchParams.get(p) || "";
|
||||
}
|
||||
|
||||
export function setup_cache_state(_backendID = null) {
|
||||
if (_backendID !== null) {
|
||||
backendID = _backendID;
|
||||
return;
|
||||
}
|
||||
return Session.currentUser().then((r) => {
|
||||
backendID = r["backendID"]
|
||||
}).catch(() => backendID = null);
|
||||
}
|
||||
@ -6,9 +6,9 @@ export { opener } from "./mimetype";
|
||||
export { debounce, throttle } from "./backpressure";
|
||||
export { event } from "./events";
|
||||
export { cache, setup_cache } from "./cache";
|
||||
export { currentShare, currentBackend, findParams, setup_cache_state } from "./cache_state";
|
||||
export {
|
||||
pathBuilder, basename, dirname, absoluteToRelative, filetype,
|
||||
currentShare, currentBackend, findParams, appendShareToUrl,
|
||||
pathBuilder, basename, dirname, absoluteToRelative, filetype, appendShareToUrl,
|
||||
} from "./path";
|
||||
export { memory } from "./memory";
|
||||
export { prepare } from "./navigate";
|
||||
|
||||
@ -36,19 +36,6 @@ export function absoluteToRelative(from, to) {
|
||||
return r;
|
||||
}
|
||||
|
||||
export function currentShare() {
|
||||
return findParams("share");
|
||||
}
|
||||
|
||||
export function currentBackend() {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
export function findParams(p) {
|
||||
return new window.URL(location.href).searchParams.get(p) || "";
|
||||
}
|
||||
|
||||
export function appendShareToUrl(link) {
|
||||
let url = new window.URL(location.href);
|
||||
const share = url.searchParams.get("share");
|
||||
|
||||
@ -54,9 +54,9 @@ class FileSystem {
|
||||
path: path,
|
||||
results: null,
|
||||
access_count: 0,
|
||||
metadata: null,
|
||||
permissions: null,
|
||||
}, _files);
|
||||
store.metadata = response.permissions;
|
||||
store.permissions = response.permissions;
|
||||
store.results = response.results;
|
||||
|
||||
if (_files && _files.results) {
|
||||
@ -104,7 +104,7 @@ class FileSystem {
|
||||
this.obs && this.obs.next({
|
||||
status: "ok",
|
||||
results: response.results,
|
||||
metadata: response.metadata,
|
||||
permissions: response.permissions,
|
||||
});
|
||||
}
|
||||
return response;
|
||||
@ -117,7 +117,7 @@ class FileSystem {
|
||||
this.obs && this.obs.next({
|
||||
status: "ok",
|
||||
results: response.results,
|
||||
metadata: response.metadata,
|
||||
permissions: response.permissions,
|
||||
});
|
||||
}
|
||||
response.last_access = new Date();
|
||||
|
||||
@ -2,8 +2,6 @@ import { cache, currentShare, currentBackend } from "../helpers/";
|
||||
|
||||
class TagManager {
|
||||
all(tagPath = "/", maxSize = -1) {
|
||||
// return Promise.resolve([]); // TODO: Remove this when ready
|
||||
|
||||
return cache.get(cache.FILE_TAG, [currentBackend(), currentShare()]).then((DB) => {
|
||||
if (DB === null) {
|
||||
return [];
|
||||
|
||||
@ -4,7 +4,7 @@ import "./connectpage.scss";
|
||||
import { Session } from "../model/";
|
||||
import { Container, NgShow, Loader, ErrorPage } from "../components/";
|
||||
import { ForkMe, PoweredByFilestash, Form } from "./connectpage/";
|
||||
import { cache, notify, urlParams } from "../helpers/";
|
||||
import { cache, notify, urlParams, setup_cache_state } from "../helpers/";
|
||||
|
||||
function ConnectPageComponent({ error, history }) {
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
@ -23,7 +23,7 @@ function ConnectPageComponent({ error, history }) {
|
||||
user["home"] = user["home"].replace(/^\/?(.*?)\/?$/, "$1").trim();
|
||||
if (user["home"] !== "") url = `${url}${user["home"]}/`;
|
||||
}
|
||||
cache.destroy();
|
||||
cache.destroy().then(() => setup_cache_state(user["backendID"]));
|
||||
history.push(url);
|
||||
});
|
||||
};
|
||||
|
||||
@ -42,7 +42,7 @@ export class FilesPageComponent extends React.Component {
|
||||
is_search: false,
|
||||
files: [],
|
||||
selected: [],
|
||||
metadata: null,
|
||||
permissions: null,
|
||||
frequents: null,
|
||||
tags: null,
|
||||
page_number: PAGE_NUMBER_INIT,
|
||||
@ -60,7 +60,7 @@ export class FilesPageComponent extends React.Component {
|
||||
// subscriptions
|
||||
this.props.subscribe("file.create", function() {
|
||||
return onCreate.apply(this, arguments).then(() => {
|
||||
if (this.state.metadata && this.state.metadata.refresh_on_create === true) {
|
||||
if (this.state.permissions && this.state.permissions.refresh_on_create === true) {
|
||||
this.onRefresh(this.state.path, "directory");
|
||||
}
|
||||
return Promise.resolve();
|
||||
@ -131,7 +131,7 @@ export class FilesPageComponent extends React.Component {
|
||||
return;
|
||||
}
|
||||
this.setState({
|
||||
metadata: res.metadata,
|
||||
permissions: res.permissions,
|
||||
files: sort(res.results, this.state.sort),
|
||||
selected: [],
|
||||
loading: false,
|
||||
@ -219,7 +219,7 @@ export class FilesPageComponent extends React.Component {
|
||||
this.setState({
|
||||
files: sort(f, this.state.sort),
|
||||
loading: false,
|
||||
metadata: {
|
||||
permissions: {
|
||||
can_rename: false,
|
||||
can_delete: false,
|
||||
can_share: false,
|
||||
@ -228,7 +228,7 @@ export class FilesPageComponent extends React.Component {
|
||||
}, (err) => {
|
||||
this.setState({
|
||||
loading: false,
|
||||
metadata: {
|
||||
permissions: {
|
||||
can_rename: false,
|
||||
can_delete: false,
|
||||
can_share: false,
|
||||
@ -292,7 +292,7 @@ export class FilesPageComponent extends React.Component {
|
||||
onSearch={this.onSearch.bind(this)}
|
||||
onViewUpdate={(value) => this.onView(value)}
|
||||
onSortUpdate={(value) => this.onSort(value)}
|
||||
accessRight={this.state.metadata || {}}
|
||||
accessRight={this.state.permissions || {}}
|
||||
selected={this.state.selected} />
|
||||
<NgIf cond={!this.state.loading}>
|
||||
<FileSystem
|
||||
@ -300,7 +300,7 @@ export class FilesPageComponent extends React.Component {
|
||||
view={this.state.view} selected={this.state.selected}
|
||||
files={this.state.files.slice(0, this.state.page_number * LOAD_PER_SCROLL)}
|
||||
isSearch={this.state.is_search}
|
||||
metadata={this.state.metadata || {}}
|
||||
metadata={this.state.permissions || {}}
|
||||
onSort={this.onSort.bind(this)}
|
||||
onView={this.onView.bind(this)} />
|
||||
</NgIf>
|
||||
@ -309,7 +309,7 @@ export class FilesPageComponent extends React.Component {
|
||||
<NgIf cond={this.state.loading === true}>
|
||||
<Loader/>
|
||||
</NgIf>
|
||||
<MobileFileUpload path={this.state.path} accessRight={this.state.metadata || {}} />
|
||||
<MobileFileUpload path={this.state.path} accessRight={this.state.permissions || {}} />
|
||||
</div>
|
||||
</div>
|
||||
</SelectableGroup>
|
||||
|
||||
Reference in New Issue
Block a user