From 867037804d4b6c057ea5f3203f56004c9de1a55c Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Mon, 3 May 2021 17:23:50 +1000 Subject: [PATCH] messages: fix last updated sorting --- pkg/interface/src/logic/lib/util.ts | 9 ++++++-- pkg/interface/src/logic/state/graph.ts | 2 +- .../src/views/apps/chat/DmResource.tsx | 3 ++- .../components/Sidebar/SidebarList.tsx | 21 ++++++++++++------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/interface/src/logic/lib/util.ts b/pkg/interface/src/logic/lib/util.ts index eb79f2bcd..e6150b26f 100644 --- a/pkg/interface/src/logic/lib/util.ts +++ b/pkg/interface/src/logic/lib/util.ts @@ -1,8 +1,9 @@ import { useEffect, useState, useCallback, useMemo } from 'react'; import _ from 'lodash'; +import { patp2dec } from 'urbit-ob'; import f, { compose, memoize } from 'lodash/fp'; import bigInt, { BigInteger } from 'big-integer'; -import { Association, Contact } from '@urbit/api'; +import { Association, Contact, Patp } from '@urbit/api'; import useLocalState from '../state/local'; import produce, { enableMapSet } from 'immer'; import useSettingsState from '../state/settings'; @@ -369,11 +370,15 @@ export function numToUd(num: number) { f.reverse, f.chunk(3), f.reverse, - f.map(s => s.join('')), + f.map(f.flow(f.reverse, f.join(''))), f.join('.') )(num.toString()); } +export function patpToUd(patp: Patp) { + return numToUd(patp2dec(patp)) +} + export function usePreventWindowUnload(shouldPreventDefault: boolean, message = 'You have unsaved changes. Are you sure you want to exit?') { useEffect(() => { if (!shouldPreventDefault) diff --git a/pkg/interface/src/logic/state/graph.ts b/pkg/interface/src/logic/state/graph.ts index d8f32b642..8792de1da 100644 --- a/pkg/interface/src/logic/state/graph.ts +++ b/pkg/interface/src/logic/state/graph.ts @@ -152,7 +152,7 @@ export function useGraphForAssoc(association: Association) { window.useGraphState = useGraphState; export function useInbox() { - return useGraphState(s => s.graphs[`${window.ship}/inbox`]); + return useGraphState(s => s.graphs[`${window.ship}/inbox`] || new BigIntOrderedMap()); } export function useDM(ship: string) { diff --git a/pkg/interface/src/views/apps/chat/DmResource.tsx b/pkg/interface/src/views/apps/chat/DmResource.tsx index 696580e72..1b3fd6af1 100644 --- a/pkg/interface/src/views/apps/chat/DmResource.tsx +++ b/pkg/interface/src/views/apps/chat/DmResource.tsx @@ -9,6 +9,7 @@ import useGraphState, { useDM } from '~/logic/state/graph'; import useHarkState, { useHarkDm } from '~/logic/state/hark'; import useSettingsState, { selectCalmState } from '~/logic/state/settings'; import { ChatPane } from './components/ChatPane'; +import {patpToUd} from '~/logic/lib/util'; interface DmResourceProps { ship: string; @@ -36,7 +37,7 @@ export function DmResource(props: DmResourceProps) { const nickname = showNickname ? contact!.nickname : cite(ship) ?? ship; useEffect(() => { - api.graph.getNewest(`~${window.ship}`, 'inbox', 100, `/${patp2dec(ship)}`); + api.graph.getNewest(`~${window.ship}`, 'inbox', 100, `/${patpToUd(ship)}`); }, [ship]); const fetchMessages = useCallback( diff --git a/pkg/interface/src/views/landscape/components/Sidebar/SidebarList.tsx b/pkg/interface/src/views/landscape/components/Sidebar/SidebarList.tsx index 49e1765e0..99a6d0ecc 100644 --- a/pkg/interface/src/views/landscape/components/Sidebar/SidebarList.tsx +++ b/pkg/interface/src/views/landscape/components/Sidebar/SidebarList.tsx @@ -1,6 +1,6 @@ import React, { ReactElement } from 'react'; -import { Associations, AppAssociations, Groups, Rolodex, Graph } from '@urbit/api'; -import { patp } from 'urbit-ob'; +import { Associations, AppAssociations, Groups, Rolodex, Graph, UnreadStats } from '@urbit/api'; +import { patp, patp2dec } from 'urbit-ob'; import { alphabeticalOrder } from '~/logic/lib/util'; import { SidebarAppConfigs, SidebarListConfig, SidebarSort } from './types'; @@ -8,10 +8,12 @@ import { SidebarAssociationItem, SidebarDmItem } from './SidebarItem'; import { Workspace } from '~/types/workspace'; import useMetadataState from '~/logic/state/metadata'; import {useInbox} from '~/logic/state/graph'; +import useHarkState from '~/logic/state/hark'; function sidebarSort( associations: AppAssociations, - apps: SidebarAppConfigs + apps: SidebarAppConfigs, + inboxUnreads: Record ): Record number> { const alphabetical = (a: string, b: string) => { const aAssoc = associations[a]; @@ -28,8 +30,12 @@ function sidebarSort( const aAppName = aAssoc?.['app-name']; const bAppName = bAssoc?.['app-name']; - const aUpdated = apps[aAppName]?.lastUpdated(a) || 0; - const bUpdated = apps[bAppName]?.lastUpdated(b) || 0; + const aUpdated = a.startsWith('~') + ? (inboxUnreads?.[`/${patp2dec(a)}`]?.last) + : apps[aAppName]?.lastUpdated(a) || 0; + const bUpdated = b.startsWith('~') + ? (inboxUnreads?.[`/${patp2dec(b)}`]?.last || 0) + : apps[bAppName]?.lastUpdated(b) || 0; return bUpdated - aUpdated || alphabetical(a, b); }; @@ -80,10 +86,11 @@ export function SidebarList(props: { const { selected, group, config, workspace } = props; const associations = useMetadataState(state => state.associations); const inbox = useInbox(); + const unreads = useHarkState(s => s.unreads.graph?.[`/ship/~${window.ship}/inbox`]); - const ordered = getItems(associations, workspace, inbox); - //.sort(sidebarSort(associations, props.apps)[config.sortBy]); + const ordered = getItems(associations, workspace, inbox) + .sort(sidebarSort(associations, props.apps, unreads)[config.sortBy]); return ( <>