Compare commits
3 Commits
615292ac24
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d2f25ef04 | |||
| 9462a74437 | |||
| 1683c0d467 |
55
src/Cache.ts
Normal file
55
src/Cache.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { InMemoryCache } from "@apollo/client"
|
||||
import ObjID from "./types/ObjID"
|
||||
|
||||
const cache = new InMemoryCache({
|
||||
typePolicies:{
|
||||
File:{
|
||||
fields:{
|
||||
id:{
|
||||
merge(_,incomming){
|
||||
// HACK: i use the merge function to change the id from a string to ObjID object.
|
||||
// afaik apollo does not yet support custom scalar types.
|
||||
if (!incomming){
|
||||
return incomming
|
||||
}else if (incomming instanceof ObjID){
|
||||
return incomming
|
||||
}else{
|
||||
return ObjID.fromString(incomming as string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Directory:{
|
||||
fields:{
|
||||
id:{
|
||||
merge(_,incomming){
|
||||
if (!incomming){
|
||||
return incomming
|
||||
}else if (incomming instanceof ObjID){
|
||||
return incomming
|
||||
}else{
|
||||
return ObjID.fromString(incomming as string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Query: {
|
||||
fields: {
|
||||
files: {
|
||||
merge(existing, incoming){
|
||||
return incoming
|
||||
}
|
||||
},
|
||||
directorys:{
|
||||
merge(existing, incoming){
|
||||
return incoming
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export default cache
|
||||
@@ -16,6 +16,7 @@ import { ReactComponent as Spinner } from "./../assets/spinner.svg"
|
||||
import FileBrowserList from "./FileBrowserList"
|
||||
import MoreMenu from "./MoreMenu"
|
||||
import ObjID from "../types/ObjID"
|
||||
import cache from "../Cache"
|
||||
|
||||
const FileBrowser: React.FC<RouteComponentProps> = (props) => {
|
||||
const path = ObjID.fromURI(props.location.pathname)
|
||||
@@ -86,6 +87,11 @@ const FileBrowser: React.FC<RouteComponentProps> = (props) => {
|
||||
if (pasteAction === Action.DirMove && srcID){
|
||||
await moveDirMutation({variables:{src:srcID,dest:path}})
|
||||
refetchDir()
|
||||
|
||||
cache.evict({
|
||||
id: `Directory:${srcID.toString()}` // TODO: check for a better way to generate cache ids
|
||||
})
|
||||
|
||||
setSrcID(null)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,62 +2,13 @@ import React from "react"
|
||||
import ReactDOM from "react-dom"
|
||||
import "./index.scss"
|
||||
import App from "./App"
|
||||
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client"
|
||||
import { ApolloClient, ApolloProvider } from "@apollo/client"
|
||||
import "react-contexify/dist/ReactContexify.css"
|
||||
import ObjID from "./types/ObjID"
|
||||
import cache from "./Cache"
|
||||
|
||||
const client = new ApolloClient({
|
||||
uri: "/api/graphql",
|
||||
cache: new InMemoryCache({
|
||||
typePolicies:{
|
||||
File:{
|
||||
fields:{
|
||||
id:{
|
||||
merge(_,incomming){
|
||||
// HACK: i use the merge function to change the id from a string to ObjID object.
|
||||
// afaik apollo does not yet support custom scalar types.
|
||||
if (!incomming){
|
||||
return incomming
|
||||
}else if (incomming instanceof ObjID){
|
||||
return incomming
|
||||
}else{
|
||||
return ObjID.fromString(incomming as string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Directory:{
|
||||
fields:{
|
||||
id:{
|
||||
merge(_,incomming){
|
||||
if (!incomming){
|
||||
return incomming
|
||||
}else if (incomming instanceof ObjID){
|
||||
return incomming
|
||||
}else{
|
||||
return ObjID.fromString(incomming as string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Query: {
|
||||
fields: {
|
||||
files: {
|
||||
merge(existing, incoming){
|
||||
return incoming
|
||||
}
|
||||
},
|
||||
directorys:{
|
||||
merge(existing, incoming){
|
||||
return incoming
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
cache: cache,
|
||||
})
|
||||
|
||||
// Disable drag and drop behaviour on document
|
||||
|
||||
@@ -53,6 +53,30 @@ class ObjID {
|
||||
return new ObjID(this.bucket,parts.join("/"))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the parent of the object. If Obj is a file then the containing directory
|
||||
* if Obj is a directory it returns the parent
|
||||
* @returns parent ObjID or null if already at root
|
||||
*/
|
||||
public parent(): ObjID | null {
|
||||
if (this.key == "/") {
|
||||
// Already at root. We dont have a parent
|
||||
return null
|
||||
}
|
||||
|
||||
if (this.isDirectory()) {
|
||||
const parts = this.key.split("/")
|
||||
const parent = new ObjID(this.bucket, parts.slice(0,-2).join("/") + "/")
|
||||
parent.normalize()
|
||||
return parent
|
||||
} else {
|
||||
const parts = this.key.split("/")
|
||||
const parent = new ObjID(this.bucket, parts.slice(0,-1).join("/") + "/")
|
||||
parent.normalize()
|
||||
return parent
|
||||
}
|
||||
}
|
||||
|
||||
public static fromString(from: string): ObjID{
|
||||
const match = stringRegex.exec(from)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user