-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MS] Replaced workspaces by recent workspaces in sidebar
- Loading branch information
Showing
12 changed files
with
179 additions
and
72 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS | ||
|
||
import { DetectedFileType } from '@/common/fileTypes'; | ||
import { | ||
ConnectionHandle, | ||
EntryID, | ||
EntryName, | ||
FsPath, | ||
getClientInfo, | ||
listWorkspaces, | ||
UserID, | ||
WorkspaceHandle, | ||
WorkspaceID, | ||
WorkspaceInfo, | ||
} from '@/parsec'; | ||
import { StorageManager } from '@/services/storageManager'; | ||
import { Ref, ref } from 'vue'; | ||
|
||
interface RecentFile { | ||
entryId: EntryID; | ||
workspaceHandle: WorkspaceHandle; | ||
path: FsPath; | ||
name: EntryName; | ||
contentType?: DetectedFileType; | ||
} | ||
|
||
type RecentWorkspace = WorkspaceInfo; | ||
|
||
const FILE_HISTORY_SIZE = 5; | ||
const WORKSPACE_HISTORY_SIZE = 5; | ||
|
||
interface RecentDocumentStorageData { | ||
workspaces: Array<WorkspaceID>; | ||
} | ||
|
||
class RecentDocumentManager { | ||
files: Ref<Array<RecentFile>> = ref([]); | ||
workspaces: Ref<Array<RecentWorkspace>> = ref([]); | ||
|
||
constructor() { | ||
this.files.value = new Array<RecentFile>(); | ||
this.workspaces.value = new Array<RecentWorkspace>(); | ||
} | ||
|
||
private _getStorageDataKey(userId: UserID): string { | ||
return `recentDocuments_${userId}`; | ||
} | ||
|
||
async loadFromStorage(storage: StorageManager, handle: ConnectionHandle | null = null): Promise<void> { | ||
const clientInfoResult = await getClientInfo(handle); | ||
if (!clientInfoResult.ok) { | ||
window.electronAPI.log('error', `Failed to load recent workspaces: ${JSON.stringify(clientInfoResult.error)}`); | ||
return; | ||
} | ||
const dataKey = this._getStorageDataKey(clientInfoResult.value.userId); | ||
const storedData = await storage.retrieveComponentData<RecentDocumentStorageData>(dataKey, { workspaces: [] }); | ||
if (!storedData || !storedData.workspaces) { | ||
return; | ||
} | ||
const workspacesResult = await listWorkspaces(handle); | ||
if (!workspacesResult.ok) { | ||
window.electronAPI.log('error', `Failed to load recent workspaces: ${JSON.stringify(workspacesResult.error)}`); | ||
return; | ||
} | ||
for (const workspace of workspacesResult.value) { | ||
if (storedData.workspaces.includes(workspace.id)) { | ||
this.addWorkspace(workspace); | ||
} | ||
} | ||
console.log('Loaded', this.workspaces.value); | ||
} | ||
|
||
async saveToStorage(storage: StorageManager): Promise<void> { | ||
const clientInfoResult = await getClientInfo(); | ||
if (!clientInfoResult.ok) { | ||
window.electronAPI.log('error', `Failed to save recent workspaces: ${JSON.stringify(clientInfoResult.error)}`); | ||
return; | ||
} | ||
const dataKey = this._getStorageDataKey(clientInfoResult.value.userId); | ||
await storage.storeComponentData<RecentDocumentStorageData>(dataKey, { | ||
workspaces: this.workspaces.value.map((workspace) => workspace.id), | ||
}); | ||
} | ||
|
||
addFile(file: RecentFile): void { | ||
const exists = this.files.value.find((item) => item.entryId === file.entryId) !== undefined; | ||
if (exists) { | ||
return; | ||
} | ||
if (this.files.value.unshift(file) > FILE_HISTORY_SIZE) { | ||
this.files.value.pop(); | ||
} | ||
} | ||
|
||
addWorkspace(workspace: RecentWorkspace): void { | ||
const exists = this.workspaces.value.find((item) => item.id === workspace.id) !== undefined; | ||
if (exists) { | ||
return; | ||
} | ||
if (this.workspaces.value.unshift(workspace) > WORKSPACE_HISTORY_SIZE) { | ||
this.workspaces.value.pop(); | ||
} | ||
} | ||
|
||
removeFile(file: RecentFile): void { | ||
const index = this.files.value.findIndex((item) => item.entryId === file.entryId); | ||
if (index !== -1) { | ||
this.files.value.splice(index, 1); | ||
} | ||
} | ||
|
||
removeWorkspace(workspace: RecentWorkspace): void { | ||
const index = this.workspaces.value.findIndex((item) => item.id === workspace.id); | ||
if (index !== -1) { | ||
this.workspaces.value.splice(index, 1); | ||
} | ||
} | ||
|
||
resetHistory(): void { | ||
this.files.value = new Array<RecentFile>(); | ||
this.workspaces.value = new Array<RecentWorkspace>(); | ||
} | ||
|
||
getFiles(): Array<RecentFile> { | ||
return this.files.value; | ||
} | ||
|
||
getWorkspaces(): Array<RecentWorkspace> { | ||
return this.workspaces.value; | ||
} | ||
} | ||
|
||
const recentDocumentManager = new RecentDocumentManager(); | ||
|
||
export { recentDocumentManager, RecentFile, RecentWorkspace }; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.