Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sidebar: recent sorting #1205

Merged
merged 3 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions desk/app/chat.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -1112,8 +1112,6 @@
(put:log-on:c log.chat time d)
=. ca-core
(ca-give-updates time d)
=. cor
(give-brief flag/flag ca-brief)
?- -.d
%add-sects
=* p perm.chat
Expand All @@ -1131,6 +1129,7 @@
::
%writs
=. pact.chat (reduce:ca-pact time p.d)
=. cor (give-brief flag/flag ca-brief)
?- -.q.p.d
?(%del %add-feel %del-feel) ca-core
%add
Expand Down
1 change: 1 addition & 0 deletions desk/app/diary.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@
?- -.dif
%notes
=. notes.diary (reduce:di-notes time p.dif)
=. cor (give-brief flag di-brief)
=/ cons=(list (list content:ha))
(hark:di-notes our.bowl p.dif)
=. cor
Expand Down
1 change: 1 addition & 0 deletions desk/app/heap.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@
?- -.d
%curios
=. curios.heap (reduce:he-curios time q.p.d)
=. cor (give-brief flag he-brief)
?- -.q.p.d
?(%edit %del %add-feel %del-feel) he-core
%add
Expand Down
12 changes: 9 additions & 3 deletions ui/src/chat/ChatUnreadAlerts.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useCallback } from 'react';
import { format, isToday } from 'date-fns';
import { useLocation, useNavigate } from 'react-router';
import { daToUnix, udToDec } from '@urbit/api';
import bigInt from 'big-integer';
import XIcon from '@/components/icons/XIcon';
import { pluralize } from '../logic/utils';
import { useChatState, useGetFirstUnreadID } from '../state/chat';
import { pluralize } from '@/logic/utils';
import { useChatState, useGetFirstUnreadID } from '@/state/chat';
import { useChatInfo } from './useChatStore';

interface ChatUnreadAlertsProps {
Expand Down Expand Up @@ -32,7 +34,11 @@ export default function ChatUnreadAlerts({ whom }: ChatUnreadAlertsProps) {
}

const { brief } = chatInfo.unread;
const date = brief ? new Date(brief.last) : new Date();
const readId = brief['read-id'];
const udTime = readId
? daToUnix(bigInt(udToDec(readId.split('/')[1])))
: null;
const date = udTime ? new Date(udTime) : new Date();
const since = isToday(date)
? `${format(date, 'HH:mm')} today`
: format(date, 'LLLL d');
Expand Down
2 changes: 0 additions & 2 deletions ui/src/groups/GroupSidebar/ChannelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ export default function ChannelList({ flag, className }: ChannelListProps) {
const isMobile = useIsMobile();
const { isChannelUnread } = useIsChannelUnread();

usePrefetchChannels(flag);

if (!group) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports[`ChannelList > renders as expected 1`] = `
<span
class="mr-2 pl-1"
>
Sort: A → Z
Sort: Arranged
</span>
</span>
<svg
Expand All @@ -54,7 +54,11 @@ exports[`ChannelList > renders as expected 1`] = `
</div>
<ul
class="space-y-1"
/>
>
<div
class="space-y-1"
/>
</ul>
</div>
</DocumentFragment>
`;
55 changes: 2 additions & 53 deletions ui/src/logic/useChannelSort.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,21 @@
import { get } from 'lodash';
import { useCallback } from 'react';
import { useGroup, useRouteGroup } from '@/state/groups';
import { GroupChannel, Channels, Zone } from '@/types/groups';
import { useGetLatestChat } from '@/state/chat';
import { ChatWhom } from '@/types/chat';
import { useGetLatestNote } from '@/state/diary';
import { useGetLatestCurio } from '@/state/heap/heap';
import useSidebarSort, {
ALPHABETICAL,
DEFAULT,
RECENT,
sortAlphabetical,
Sorter,
useRecentSort,
} from './useSidebarSort';
import { nestToFlag } from './utils';

const UNZONED = 'default';

function useGetLatestPost() {
const getLatestChat = useGetLatestChat();
const getLatestCurio = useGetLatestCurio();
const getLatestNote = useGetLatestNote();

return (flag: string) => {
const [chType, _chFlag] = nestToFlag(flag);

switch (chType) {
case 'chat':
return (getLatestChat(flag)[0] ?? Number.NEGATIVE_INFINITY).toString();

case 'diary':
return (getLatestNote(flag)[0] ?? Number.NEGATIVE_INFINITY).toString();

case 'heap':
return (getLatestCurio(flag)[0] ?? Number.NEGATIVE_INFINITY).toString();

default:
return Number.NEGATIVE_INFINITY.toString();
}
};
}

function useRecentChannelSort() {
const getLatestPost = useGetLatestPost();

const sortRecent = useCallback(
(a: ChatWhom, b: ChatWhom) => {
const aLast = getLatestPost(a);
const bLast = getLatestPost(b);

if (aLast < bLast) {
return -1;
}
if (aLast > bLast) {
return 1;
}
return 0;
},
[getLatestPost]
);

return sortRecent;
}

export default function useChannelSort() {
const groupFlag = useRouteGroup();
const group = useGroup(groupFlag);
const sortRecent = useRecentChannelSort();
const sortRecent = useRecentSort();

const sortDefault = (a: Zone, b: Zone) => {
if (!group) {
Expand Down
6 changes: 5 additions & 1 deletion ui/src/logic/useGroupSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export default function useGroupSort() {
},
};

return sortRecordsBy(groups, accessors[sortFn], sortFn === RECENT);
return sortRecordsBy(
groups,
accessors[sortFn] || accessors[ALPHABETICAL],
sortFn === RECENT
);
}

return {
Expand Down
18 changes: 9 additions & 9 deletions ui/src/logic/useSidebarSort.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useCallback } from 'react';
import { useBriefs } from '@/state/chat';
import { ChatWhom } from '@/types/chat';
import {
SettingsState,
useSettingsState,
useGroupSideBarSort,
} from '@/state/settings';
import useAllBriefs from './useAllBriefs';

export const ALPHABETICAL = 'A → Z';
export const DEFAULT = 'Arranged';
Expand All @@ -21,19 +20,19 @@ interface UseSidebarSort {
flag?: string;
}

export const sortAlphabetical = (a: ChatWhom, b: ChatWhom) =>
a.localeCompare(b);
export const sortAlphabetical = (aNest: string, bNest: string) =>
aNest.localeCompare(bNest);

const selSideBarSort = (s: SettingsState) => ({
sideBarSort: s.groups.sideBarSort,
});

export function useRecentSort() {
const briefs = useBriefs();
const briefs = useAllBriefs();
const sortRecent = useCallback(
(a: ChatWhom, b: ChatWhom) => {
const aLast = briefs[a]?.last ?? Number.NEGATIVE_INFINITY;
const bLast = briefs[b]?.last ?? Number.NEGATIVE_INFINITY;
(aNest: string, bNest: string) => {
const aLast = briefs[aNest]?.last ?? Number.NEGATIVE_INFINITY;
const bLast = briefs[bNest]?.last ?? Number.NEGATIVE_INFINITY;
if (aLast < bLast) {
return -1;
}
Expand Down Expand Up @@ -89,7 +88,8 @@ export default function useSidebarSort({
const aVal = accessor(aKey, aObj);
const bVal = accessor(bKey, bObj);

return sortOptions[sortFn ?? 'A → Z'](aVal, bVal);
const sorter = sortOptions[sortFn] ?? sortOptions[ALPHABETICAL];
return sorter(aVal, bVal);
});

return reverse ? entries.reverse() : entries;
Expand Down
4 changes: 2 additions & 2 deletions ui/src/state/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const useSettingsState = createState<BaseSettingsState>(
},
groups: {
orderedGroupPins: [],
sideBarSort: ALPHABETICAL,
sideBarSort: DEFAULT,
groupSideBarSort: '{"~": "A → Z"}' as Stringified<GroupSideBarSort>,
},
talk: {
Expand Down Expand Up @@ -243,7 +243,7 @@ export function useHeapSortMode(flag: string): HeapSortMode {
export function useHeapDisplayMode(flag: string): HeapDisplayMode {
const settings = useHeapSettings();
const heapSetting = getSetting(settings, flag);
return heapSetting?.displayMode ?? 'list';
return heapSetting?.displayMode ?? 'grid';
}

const selDiarySettings = (s: SettingsState) => s.diary.settings;
Expand Down