-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
450 additions
and
86 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
40 changes: 40 additions & 0 deletions
40
packages/frontend/core/src/components/page-list/group-definitions.css.ts
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,40 @@ | ||
import { cssVar } from '@toeverything/theme'; | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const groupLabelWrapper = style({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
gap: '4px', | ||
}); | ||
|
||
export const tagIcon = style({ | ||
width: '8px', | ||
height: '8px', | ||
borderRadius: '50%', | ||
marginLeft: '4px', | ||
marginRight: '6px', | ||
}); | ||
|
||
export const groupLabel = style({ | ||
fontSize: cssVar('fontSm'), | ||
lineHeight: '1.5em', | ||
color: cssVar('textPrimaryColor'), | ||
}); | ||
|
||
export const pageCount = style({ | ||
fontSize: cssVar('fontBase'), | ||
lineHeight: '1.6em', | ||
color: cssVar('textSecondaryColor'), | ||
}); | ||
|
||
export const favouritedIcon = style({ | ||
color: cssVar('primaryColor'), | ||
marginRight: '6px', | ||
fontSize: '16px', | ||
}); | ||
|
||
export const notFavouritedIcon = style({ | ||
color: cssVar('iconColor'), | ||
marginRight: '6px', | ||
fontSize: '16px', | ||
}); |
168 changes: 168 additions & 0 deletions
168
packages/frontend/core/src/components/page-list/group-definitions.tsx
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,168 @@ | ||
import { TagService } from '@affine/core/modules/tag'; | ||
import { useAFFiNEI18N } from '@affine/i18n/hooks'; | ||
import { FavoritedIcon, FavoriteIcon } from '@blocksuite/icons'; | ||
import type { DocMeta } from '@blocksuite/store'; | ||
import { useService } from '@toeverything/infra/di'; | ||
import { useLiveData } from '@toeverything/infra/livedata'; | ||
import { atom, useAtomValue } from 'jotai'; | ||
import { type ReactNode, useMemo } from 'react'; | ||
|
||
import * as styles from './group-definitions.css'; | ||
import type { ItemGroupDefinition, ListItem, PageGroupByType } from './types'; | ||
import { type DateKey } from './types'; | ||
import { betweenDaysAgo, withinDaysAgo } from './utils'; | ||
|
||
export const pageGroupByTypeAtom = atom<PageGroupByType>('updatedDate'); | ||
|
||
const GroupLabel = ({ | ||
label, | ||
count, | ||
icon, | ||
}: { | ||
label: string | ReactNode; | ||
count: number; | ||
icon?: ReactNode; | ||
}) => ( | ||
<div className={styles.groupLabelWrapper}> | ||
{icon} | ||
<div className={styles.groupLabel}>{label}</div> | ||
<div className={styles.pageCount}>{` · ${count}`}</div> | ||
</div> | ||
); | ||
|
||
// todo: optimize date matchers | ||
export const useDateGroupDefinitions = <T extends ListItem>( | ||
key: DateKey | ||
): ItemGroupDefinition<T>[] => { | ||
const t = useAFFiNEI18N(); | ||
return useMemo( | ||
() => [ | ||
{ | ||
id: 'today', | ||
label: count => ( | ||
<GroupLabel label={t['com.affine.today']()} count={count} /> | ||
), | ||
match: item => | ||
withinDaysAgo(new Date(item[key] ?? item.createDate ?? ''), 1), | ||
}, | ||
{ | ||
id: 'yesterday', | ||
label: count => ( | ||
<GroupLabel label={t['com.affine.yesterday']()} count={count} /> | ||
), | ||
match: item => | ||
betweenDaysAgo(new Date(item[key] ?? item.createDate ?? ''), 1, 2), | ||
}, | ||
{ | ||
id: 'last7Days', | ||
label: count => ( | ||
<GroupLabel label={t['com.affine.last7Days']()} count={count} /> | ||
), | ||
match: item => | ||
betweenDaysAgo(new Date(item[key] ?? item.createDate ?? ''), 2, 7), | ||
}, | ||
{ | ||
id: 'last30Days', | ||
label: count => ( | ||
<GroupLabel label={t['com.affine.last30Days']()} count={count} /> | ||
), | ||
match: item => | ||
betweenDaysAgo(new Date(item[key] ?? item.createDate ?? ''), 7, 30), | ||
}, | ||
{ | ||
id: 'moreThan30Days', | ||
label: count => ( | ||
<GroupLabel label={t['com.affine.moreThan30Days']()} count={count} /> | ||
), | ||
match: item => | ||
!withinDaysAgo(new Date(item[key] ?? item.createDate ?? ''), 30), | ||
}, | ||
], | ||
[key, t] | ||
); | ||
}; | ||
export const useTagGroupDefinitions = (): ItemGroupDefinition<ListItem>[] => { | ||
const tagService = useService(TagService); | ||
const tagMetas = useLiveData(tagService.tagMetas); | ||
return useMemo(() => { | ||
return tagMetas.map(tag => ({ | ||
id: tag.id, | ||
label: count => ( | ||
<GroupLabel | ||
label={tag.title} | ||
count={count} | ||
icon={ | ||
<div | ||
className={styles.tagIcon} | ||
style={{ | ||
backgroundColor: tag.color, | ||
}} | ||
></div> | ||
} | ||
/> | ||
), | ||
match: item => (item as DocMeta).tags?.includes(tag.id), | ||
})); | ||
}, [tagMetas]); | ||
}; | ||
|
||
export const useFavoriteGroupDefinitions = < | ||
T extends ListItem, | ||
>(): ItemGroupDefinition<T>[] => { | ||
const t = useAFFiNEI18N(); | ||
return useMemo( | ||
() => [ | ||
{ | ||
id: 'favourited', | ||
label: count => ( | ||
<GroupLabel | ||
label={t['com.affine.page.group-header.favourited']()} | ||
count={count} | ||
icon={<FavoritedIcon className={styles.favouritedIcon} />} | ||
/> | ||
), | ||
match: item => !!(item as DocMeta).favorite, | ||
}, | ||
{ | ||
id: 'notFavourited', | ||
label: count => ( | ||
<GroupLabel | ||
label={t['com.affine.page.group-header.not-favourited']()} | ||
count={count} | ||
icon={<FavoriteIcon className={styles.notFavouritedIcon} />} | ||
/> | ||
), | ||
match: item => !(item as DocMeta).favorite, | ||
}, | ||
], | ||
[t] | ||
); | ||
}; | ||
|
||
export const usePageItemGroupDefinitions = () => { | ||
const key = useAtomValue(pageGroupByTypeAtom); | ||
const tagGroupDefinitions = useTagGroupDefinitions(); | ||
const createDateGroupDefinitions = useDateGroupDefinitions('createDate'); | ||
const updatedDateGroupDefinitions = useDateGroupDefinitions('updatedDate'); | ||
const favouriteGroupDefinitions = useFavoriteGroupDefinitions(); | ||
|
||
return useMemo(() => { | ||
const itemGroupDefinitions = { | ||
createDate: createDateGroupDefinitions, | ||
updatedDate: updatedDateGroupDefinitions, | ||
tag: tagGroupDefinitions, | ||
favourites: favouriteGroupDefinitions, | ||
none: undefined, | ||
|
||
// add more here later | ||
// todo: some page group definitions maybe dynamic | ||
}; | ||
return itemGroupDefinitions[key]; | ||
}, [ | ||
createDateGroupDefinitions, | ||
favouriteGroupDefinitions, | ||
key, | ||
tagGroupDefinitions, | ||
updatedDateGroupDefinitions, | ||
]); | ||
}; |
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
Oops, something went wrong.