From 8b0d099b1a06728f627eb8d582eaab718fc0785f Mon Sep 17 00:00:00 2001 From: Kristina Date: Thu, 23 May 2024 21:16:26 +0400 Subject: [PATCH] Save last location state of inbox (#5656) Signed-off-by: Kristina Fefelova --- packages/ui/src/location.ts | 24 ++++++++++++- .../src/components/chat/Chat.svelte | 25 +++++-------- .../src/components/chat/utils.ts | 35 +------------------ .../src/components/inbox/Inbox.svelte | 14 +++++++- 4 files changed, 46 insertions(+), 52 deletions(-) diff --git a/packages/ui/src/location.ts b/packages/ui/src/location.ts index ab7ddc5366..dc6b15f4ab 100644 --- a/packages/ui/src/location.ts +++ b/packages/ui/src/location.ts @@ -14,8 +14,10 @@ // import { Analytics } from '@hcengineering/analytics' -import { clone } from '@hcengineering/core' +import { clone, type Ref, type Space } from '@hcengineering/core' import { derived, get, writable } from 'svelte/store' +import { type Plugin } from '@hcengineering/platform' + import { closePopup } from './popups' import { type Location as PlatformLocation } from './types' @@ -207,3 +209,23 @@ export const setTreeCollapsed = (_id: any, collapsed: boolean): void => { const key = getCollapsedKey(_id) collapsed ? localStorage.setItem(key, COLLAPSED) : localStorage.removeItem(key) } + +export function restoreLocation (loc: PlatformLocation, app: Plugin): void { + const last = localStorage.getItem(`${locationStorageKeyId}_${app}`) + + if (last !== null) { + const newLocation: PlatformLocation = JSON.parse(last) + + if (newLocation.path[3] != null) { + loc.path[3] = newLocation.path[3] as Ref + loc.path[4] = newLocation.path[4] + if (loc.path[4] == null) { + loc.path.length = 4 + } else { + loc.path.length = 5 + } + + navigate(loc) + } + } +} diff --git a/plugins/chunter-resources/src/components/chat/Chat.svelte b/plugins/chunter-resources/src/components/chat/Chat.svelte index c8cebe0ac3..4e36f82ca4 100644 --- a/plugins/chunter-resources/src/components/chat/Chat.svelte +++ b/plugins/chunter-resources/src/components/chat/Chat.svelte @@ -22,7 +22,8 @@ location, navigate, Separator, - Location + Location, + restoreLocation } from '@hcengineering/ui' import { NavigatorModel, SpecialNavModel } from '@hcengineering/workbench' @@ -33,7 +34,7 @@ import ChatNavigator from './navigator/ChatNavigator.svelte' import ChannelView from '../ChannelView.svelte' - import { chatSpecials, loadSavedAttachments, storeChannel, openedChannelStore, clearChannel } from './utils' + import { chatSpecials, loadSavedAttachments } from './utils' import { SelectChannelEvent } from './types' import { decodeChannelURI, openChannel } from '../../navigation' @@ -60,16 +61,6 @@ syncLocation(loc) }) - openedChannelStore.subscribe((data) => { - if (data === undefined) { - selectedData = undefined - object = undefined - } else if (selectedData?._id !== data._id) { - selectedData = data - openChannel(data._id, data._class, data.thread) - } - }) - $: void loadObject(selectedData?._id, selectedData?._class) async function loadObject (_id?: Ref, _class?: Ref>): Promise { @@ -98,18 +89,20 @@ if (!id) { currentSpecial = undefined - clearChannel() + selectedData = undefined + object = undefined + restoreLocation(loc, chunterId) return } currentSpecial = navigatorModel?.specials?.find((special) => special.id === id) if (currentSpecial !== undefined) { - clearChannel() + selectedData = undefined + object = undefined } else { const [_id, _class] = decodeChannelURI(loc.path[3]) - - storeChannel(_id, _class, loc.path[4] as Ref) + selectedData = { _id, _class } } } diff --git a/plugins/chunter-resources/src/components/chat/utils.ts b/plugins/chunter-resources/src/components/chat/utils.ts index a03eb2e76f..c998de8c2d 100644 --- a/plugins/chunter-resources/src/components/chat/utils.ts +++ b/plugins/chunter-resources/src/components/chat/utils.ts @@ -14,8 +14,6 @@ // import notification, { type DocNotifyContext } from '@hcengineering/notification' import { - type Class, - type Doc, generateId, type Ref, SortingOrder, @@ -32,7 +30,7 @@ import { get, writable } from 'svelte/store' import view from '@hcengineering/view' import workbench, { type SpecialNavModel } from '@hcengineering/workbench' import attachment, { type SavedAttachments } from '@hcengineering/attachment' -import activity, { type ActivityMessage } from '@hcengineering/activity' +import activity from '@hcengineering/activity' import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources' import { type Action, showPopup } from '@hcengineering/ui' import contact, { type PersonAccount } from '@hcengineering/contact' @@ -41,34 +39,15 @@ import { type DirectMessage } from '@hcengineering/chunter' import { type ChatNavGroupModel, type ChatNavItemModel, type SortFnOptions } from './types' import chunter from '../../plugin' -const channelStorageKey = 'chunter.openedChannel' const navigatorStateStorageKey = 'chunter.navigatorState' -interface ChannelMetadata { - _id: Ref - _class: Ref> - thread?: Ref -} interface NavigatorState { collapsedSections: string[] } export const savedAttachmentsStore = writable>>([]) -export const openedChannelStore = writable(restoreChannel()) export const navigatorStateStore = writable(restoreNavigatorState()) -function restoreChannel (): ChannelMetadata | undefined { - const raw = localStorage.getItem(channelStorageKey) - - if (raw == null) return undefined - - try { - return JSON.parse(raw) as ChannelMetadata - } catch (e) { - return undefined - } -} - function restoreNavigatorState (): NavigatorState { const raw = localStorage.getItem(navigatorStateStorageKey) @@ -93,18 +72,6 @@ export function toggleSections (_id: string): void { navigatorStateStore.set(result) } -export function clearChannel (): void { - localStorage.removeItem(channelStorageKey) - openedChannelStore.set(undefined) -} - -export function storeChannel (_id: Ref, _class: Ref>, thread?: Ref): void { - const data: ChannelMetadata = { _id, _class, thread } - - localStorage.setItem(channelStorageKey, JSON.stringify(data)) - openedChannelStore.set(data) -} - export const chatSpecials: SpecialNavModel[] = [ { id: 'threads', diff --git a/plugins/notification-resources/src/components/inbox/Inbox.svelte b/plugins/notification-resources/src/components/inbox/Inbox.svelte index ec337d2c57..99b1631142 100644 --- a/plugins/notification-resources/src/components/inbox/Inbox.svelte +++ b/plugins/notification-resources/src/components/inbox/Inbox.svelte @@ -17,7 +17,8 @@ ActivityInboxNotification, decodeObjectURI, DocNotifyContext, - InboxNotification + InboxNotification, + notificationId } from '@hcengineering/notification' import { ActionContext, createQuery, getClient } from '@hcengineering/presentation' import view from '@hcengineering/view' @@ -28,6 +29,7 @@ Label, location as locationStore, Location, + restoreLocation, Scroller, Separator, TabItem, @@ -147,6 +149,16 @@ async function syncLocation (newLocation: Location): Promise { const loc = await resolveLocation(newLocation) + if (loc?.loc.path[2] !== notificationId) { + return + } + + if (loc?.loc.path[3] == null) { + selectedContext = undefined + restoreLocation(newLocation, notificationId) + return + } + const [_id] = decodeObjectURI(loc?.loc.path[3] ?? '') const context = $contextByDocStore.get(_id)