Skip to content

A local-first personal finance app

Hiding tags and tag table bulk actions (#7919)

Alec Bakholdina0e3cbdd8657
diff --git a/packages/desktop-client/src/components/tags/ManageTags.tsx b/packages/desktop-client/src/components/tags/ManageTags.tsx
index 1bb0fe909b..d8aeab6c96 100644
--- a/packages/desktop-client/src/components/tags/ManageTags.tsx
+++ b/packages/desktop-client/src/components/tags/ManageTags.tsx
@@ -1,31 +1,34 @@
-import React, { useCallback, useMemo, useState } from 'react';
+import React, { useEffect, useMemo, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { Button } from '@actual-app/components/button';
import { SvgAdd } from '@actual-app/components/icons/v1';
-import { SvgSearchAlternate } from '@actual-app/components/icons/v2';
import { SpaceBetween } from '@actual-app/components/space-between';
import { styles } from '@actual-app/components/styles';
import { Text } from '@actual-app/components/text';
import { theme } from '@actual-app/components/theme';
import { View } from '@actual-app/components/view';
+import { listen } from '@actual-app/core/platform/client/connection';
import { getNormalisedString } from '@actual-app/core/shared/normalisation';
import { Search } from '#components/common/Search';
import { SelectedProvider, useSelected } from '#hooks/useSelected';
import { useTags } from '#hooks/useTags';
-import { useDeleteTagsMutation, useDiscoverTagsMutation } from '#tags';
+import { SelectedTagsButton } from './SelectedTagsButton';
import { TagCreationRow } from './TagCreationRow';
import { TagsHeader } from './TagsHeader';
import { TagsList } from './TagsList';
+import { TagsMenuButton } from './TagsMenuButton';
export function ManageTags() {
const { t } = useTranslation();
const [filter, setFilter] = useState('');
const [hoveredTag, setHoveredTag] = useState<string>();
const [create, setCreate] = useState(false);
- const { data: tags = [] } = useTags();
+ const { data: tags = [], refetch } = useTags();
+
+ useEffect(() => listen('undo-event', () => refetch({ cancelRefetch: true })));
const filteredTags = useMemo(() => {
return filter === ''
@@ -37,20 +40,6 @@ export function ManageTags() {
const selectedInst = useSelected('manage-tags', filteredTags, []);
- const { mutate: discoverTags } = useDiscoverTagsMutation();
- const { mutate: deleteTags } = useDeleteTagsMutation();
-
- const onDeleteSelected = useCallback(async () => {
- deleteTags(
- { ids: [...selectedInst.items] },
- {
- onSuccess: () => {
- selectedInst.dispatch({ type: 'select-none' });
- },
- },
- );
- }, [deleteTags, selectedInst]);
-
return (
<SelectedProvider instance={selectedInst}>
<View>
@@ -78,15 +67,9 @@ export function ManageTags() {
<SvgAdd width={10} height={10} style={{ marginRight: 3 }} />
<Trans>Add New</Trans>
</Button>
- <Button variant="bare" onPress={() => discoverTags()}>
- <SvgSearchAlternate
- width={10}
- height={10}
- style={{ marginRight: 3 }}
- />
- <Trans>Find Existing Tags</Trans>
- </Button>
<View style={{ flex: 1 }} />
+ <SelectedTagsButton />
+ <TagsMenuButton />
<Search
placeholder={t('Filter tags...')}
value={filter}
@@ -118,27 +101,6 @@ export function ManageTags() {
</View>
)}
</View>
- <View
- style={{
- paddingBlock: 15,
- paddingInline: 0,
- borderTop: theme.pillBorder,
- flexShrink: 0,
- }}
- >
- <SpaceBetween
- gap={10}
- style={{ alignItems: 'center', justifyContent: 'flex-end' }}
- >
- {selectedInst.items.size > 0 && (
- <Button onPress={onDeleteSelected}>
- <Trans count={selectedInst.items.size}>
- Delete {selectedInst.items.size} tags
- </Trans>
- </Button>
- )}
- </SpaceBetween>
- </View>
</View>
</SelectedProvider>
);
diff --git a/packages/desktop-client/src/components/tags/SelectedTagsButton.tsx b/packages/desktop-client/src/components/tags/SelectedTagsButton.tsx
new file mode 100644
index 0000000000..273011068e
--- /dev/null
+++ b/packages/desktop-client/src/components/tags/SelectedTagsButton.tsx
@@ -0,0 +1,58 @@
+import { useHotkeys } from 'react-hotkeys-hook';
+import { useTranslation } from 'react-i18next';
+
+import { SelectedItemsButton } from '#components/table';
+import { useSelectedDispatch, useSelectedItems } from '#hooks/useSelected';
+import {
+ useDeleteTagsMutation,
+ useHideTagsMutation,
+ useUnhideTagsMutation,
+} from '#tags';
+
+type Actions = 'delete-tags' | 'hide-tags' | 'unhide-tags';
+
+export function SelectedTagsButton() {
+ const dispatch = useSelectedDispatch();
+ const { t } = useTranslation();
+ const selectedItems = useSelectedItems();
+ const { mutate: deleteTags } = useDeleteTagsMutation();
+ const { mutate: hideTags } = useHideTagsMutation();
+ const { mutate: unhideTags } = useUnhideTagsMutation();
+
+ async function handleDelete(tagIds: string[]) {
+ deleteTags(
+ { ids: tagIds },
+ { onSuccess: () => dispatch({ type: 'select-none' }) },
+ );
+ }
+
+ function handleSelect(name: Actions, tagIds: string[]) {
+ if (name === 'delete-tags') {
+ void handleDelete(tagIds);
+ } else if (name === 'hide-tags') {
+ hideTags({ ids: [...tagIds] });
+ } else if (name === 'unhide-tags') {
+ unhideTags({ ids: [...tagIds] });
+ } else {
+ console.error('Unhandled action', name);
+ }
+ }
+
+ const enabled = !!selectedItems.size;
+ useHotkeys('d', () => handleDelete([...selectedItems]), { enabled });
+ useHotkeys('h', () => hideTags({ ids: [...selectedItems] }), { enabled });
+ useHotkeys('u', () => unhideTags({ ids: [...selectedItems] }), { enabled });
+
+ return (
+ <SelectedItemsButton<Actions>
+ id="selected-tags"
+ name={c => `${c} Tags`}
+ items={[
+ { name: 'delete-tags', text: t('Delete'), key: 'D' },
+ { name: 'hide-tags', text: t('Hide'), key: 'H' },
+ { name: 'unhide-tags', text: t('Unhide'), key: 'U' },
+ ]}
+ onSelect={handleSelect}
+ />
+ );
+}
diff --git a/packages/desktop-client/src/components/tags/TagRow.tsx b/packages/desktop-client/src/components/tags/TagRow.tsx
index 6ddac777a0..53e5dfa1d9 100644
--- a/packages/desktop-client/src/components/tags/TagRow.tsx
+++ b/packages/desktop-client/src/components/tags/TagRow.tsx
@@ -141,11 +141,11 @@ export const TagRow = memo(
exposed={focusedField === 'description'}
onExpose={name => onEdit(tag.id, name)}
value={tag.description || t('No description')}
- valueStyle={
- tag.description
- ? {}
- : { fontStyle: 'italic', color: theme.tableTextLight }
- }
+ valueStyle={{
+ opacity: tag.hidden ? 0.5 : undefined,
+ fontStyle: !tag.description ? 'italic' : undefined,
+ color: !tag.description ? theme.tableTextLight : undefined,
+ }}
inputProps={{
value: tag.description || '',
onUpdate,
@@ -163,6 +163,7 @@ export const TagRow = memo(
color: theme.noticeTextDark,
fontSize: 12,
cursor: 'pointer',
+ opacity: tag.hidden ? 0.5 : undefined,
':hover': { backgroundColor: theme.noticeBackgroundLight },
}}
onSelect={onShowActivity}
diff --git a/packages/desktop-client/src/components/tags/TagsMenuButton.tsx b/packages/desktop-client/src/components/tags/TagsMenuButton.tsx
new file mode 100644
index 0000000000..78815f5ab1
--- /dev/null
+++ b/packages/desktop-client/src/components/tags/TagsMenuButton.tsx
@@ -0,0 +1,79 @@
+import { useRef, useState } from 'react';
+import { useTranslation } from 'react-i18next';
+
+import { Button } from '@actual-app/components/button';
+import { SvgDotsHorizontalTriple } from '@actual-app/components/icons/v1';
+import {
+ SvgSearchAlternate,
+ SvgViewHide,
+ SvgViewShow,
+} from '@actual-app/components/icons/v2';
+import { Menu } from '@actual-app/components/menu';
+import { Popover } from '@actual-app/components/popover';
+import { View } from '@actual-app/components/view';
+
+import { useSyncedPref } from '#hooks/useSyncedPref';
+import { useDiscoverTagsMutation } from '#tags';
+
+export function TagsMenuButton() {
+ const { t } = useTranslation();
+ const [open, setOpen] = useState(false);
+ const { mutate: discoverTags } = useDiscoverTagsMutation();
+ const [showHidden, setShowHidden] = useSyncedPref('show-hidden-tags');
+ const triggerRef = useRef<HTMLButtonElement | null>(null);
+
+ return (
+ <View>
+ <Button
+ ref={triggerRef}
+ variant="bare"
+ aria-label={t('Menu')}
+ onClick={() => setOpen(true)}
+ >
+ <SvgDotsHorizontalTriple width={17} height={17} />
+ </Button>
+ <Popover
+ triggerRef={triggerRef}
+ isOpen={open}
+ isNonModal
+ onOpenChange={setOpen}
+ >
+ <Menu
+ items={[
+ showHidden === 'true'
+ ? {
+ name: 'set-dont-show-hidden',
+ text: t("Don't show hidden tags"),
+ icon: SvgViewHide,
+ }
+ : {
+ name: 'set-show-hidden',
+ text: t('Show hidden tags'),
+ icon: SvgViewShow,
+ },
+ {
+ name: 'discover-tags',
+ text: t('Discover new tags'),
+ icon: SvgSearchAlternate,
+ },
+ ]}
+ onMenuSelect={name => {
+ switch (name) {
+ case 'set-dont-show-hidden':
+ setShowHidden('false');
+ break;
+ case 'set-show-hidden':
+ setShowHidden('true');
+ break;
+ case 'discover-tags':
+ discoverTags();
+ break;
+ default:
+ console.error('Unhandled menu option', name);
+ }
+ }}
+ />
+ </Popover>
+ </View>
+ );
+}
diff --git a/packages/desktop-client/src/hooks/useTagCSS.ts b/packages/desktop-client/src/hooks/useTagCSS.ts
index 919968e2ab..09e6d4806e 100644
--- a/packages/desktop-client/src/hooks/useTagCSS.ts
+++ b/packages/desktop-client/src/hooks/useTagCSS.ts
@@ -17,10 +17,11 @@ export function useTagCSS(opts?: { ellipsis?: boolean }) {
tag: string,
options: { color?: string | null; compact?: boolean } = {},
) => {
+ const tagObj = tags.find(t => t.tag === tag);
const [color, backgroundColor, backgroundColorHovered] = getTagCSSColors(
theme,
// fallback strategy: options color > tag color > default color > theme color (undefined)
- options.color ?? tags.find(t => t.tag === tag)?.color,
+ options.color ?? tagObj?.color,
);
return css({
@@ -32,6 +33,7 @@ export function useTagCSS(opts?: { ellipsis?: boolean }) {
display: 'inline-block',
}
: { display: 'inline-flex' }),
+ opacity: tagObj?.hidden ? 0.5 : undefined,
padding: options.compact ? '0px 7px' : '3px 7px',
borderRadius: 16,
userSelect: 'none',
diff --git a/packages/desktop-client/src/hooks/useTags.ts b/packages/desktop-client/src/hooks/useTags.ts
index 41bfdd016a..40007c2d32 100644
--- a/packages/desktop-client/src/hooks/useTags.ts
+++ b/packages/desktop-client/src/hooks/useTags.ts
@@ -6,8 +6,16 @@ import { byLengthAsc, byStartAsc, Fzf } from 'fzf';
import { tagQueries } from '#tags/queries';
+import { useSyncedPref } from './useSyncedPref';
+
export function useTags() {
- return useQuery(tagQueries.list());
+ const [showHiddenTags] = useSyncedPref('show-hidden-tags');
+ return useQuery({
+ ...tagQueries.list(),
+ select: data => {
+ return showHiddenTags === 'true' ? data : data.filter(tag => !tag.hidden);
+ },
+ });
}
export function filterTags<T extends TagEntity>(
diff --git a/packages/desktop-client/src/tags/mutations.ts b/packages/desktop-client/src/tags/mutations.ts
index fa402df934..e55863df30 100644
--- a/packages/desktop-client/src/tags/mutations.ts
+++ b/packages/desktop-client/src/tags/mutations.ts
@@ -135,6 +135,56 @@ export function useDeleteTagsMutation() {
});
}
+type HideTagsPayload = {
+ ids: Array<TagEntity['id']>;
+};
+
+export function useHideTagsMutation() {
+ const queryClient = useQueryClient();
+ const dispatch = useDispatch();
+ const { t } = useTranslation();
+
+ return useMutation({
+ mutationFn: async ({ ids }: HideTagsPayload) => {
+ return await send('tags-hide-all', ids);
+ },
+ onSuccess: () => invalidateQueries(queryClient),
+ onError: error => {
+ console.error('Error hiding tags:', error);
+ dispatchErrorNotification(
+ dispatch,
+ t('There was an error hiding the tags. Please try again.'),
+ error,
+ );
+ },
+ });
+}
+
+type UnhideTagsPayload = {
+ ids: Array<TagEntity['id']>;
+};
+
+export function useUnhideTagsMutation() {
+ const queryClient = useQueryClient();
+ const dispatch = useDispatch();
+ const { t } = useTranslation();
+
+ return useMutation({
+ mutationFn: async ({ ids }: UnhideTagsPayload) => {
+ return await send('tags-unhide-all', ids);
+ },
+ onSuccess: () => invalidateQueries(queryClient),
+ onError: error => {
+ console.error('Error hiding tags:', error);
+ dispatchErrorNotification(
+ dispatch,
+ t('There was an error hiding the tags. Please try again.'),
+ error,
+ );
+ },
+ });
+}
+
export function useDiscoverTagsMutation() {
const queryClient = useQueryClient();
const dispatch = useDispatch();
diff --git a/packages/desktop-client/src/tags/queries.ts b/packages/desktop-client/src/tags/queries.ts
index bd5c10c2e6..724e157d08 100644
--- a/packages/desktop-client/src/tags/queries.ts
+++ b/packages/desktop-client/src/tags/queries.ts
@@ -8,10 +8,7 @@ export const tagQueries = {
list: () =>
queryOptions<TagEntity[]>({
queryKey: [...tagQueries.lists()],
- queryFn: async () => {
- const tags: TagEntity[] = await send('tags-get');
- return tags;
- },
+ queryFn: () => send('tags-get'),
placeholderData: [],
// Manually invalidated when tags change
staleTime: Infinity,
diff --git a/packages/docs/docs/api/types.jsx b/packages/docs/docs/api/types.jsx
index 8c64ab291c..7de2e1439c 100644
--- a/packages/docs/docs/api/types.jsx
+++ b/packages/docs/docs/api/types.jsx
@@ -423,6 +423,7 @@ export const objects = {
{ name: 'tag', type: 'string', required: true },
{ name: 'color', type: 'string' },
{ name: 'description', type: 'string' },
+ { name: 'hidden', type: 'boolean' },
],
condition: [
diff --git a/packages/loot-core/migrations/1780327681000_add_tags_hidden.sql b/packages/loot-core/migrations/1780327681000_add_tags_hidden.sql
new file mode 100644
index 0000000000..8daf33e9e8
--- /dev/null
+++ b/packages/loot-core/migrations/1780327681000_add_tags_hidden.sql
@@ -0,0 +1,5 @@
+BEGIN TRANSACTION;
+
+ALTER TABLE tags ADD COLUMN hidden BOOLEAN DEFAULT 0;
+
+COMMIT;
diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts
index b1ff1eec2b..ae73a5083f 100644
--- a/packages/loot-core/src/server/db/index.ts
+++ b/packages/loot-core/src/server/db/index.ts
@@ -961,7 +961,7 @@ function toSqlQueryParameters(params: unknown[]) {
export function getTags() {
return all<DbTag>(`
- SELECT id, tag, color, description
+ SELECT id, tag, color, description, hidden
FROM tags
WHERE tombstone = 0
`);
@@ -969,20 +969,22 @@ export function getTags() {
export function getAllTags() {
return all<DbTag>(`
- SELECT id, tag, color, description
+ SELECT id, tag, color, description, hidden
FROM tags
`);
}
-export function insertTag(tag): Promise<DbTag['id']> {
+export function insertTag(
+ tag: Omit<DbTag, 'id' | 'tombstone'>,
+): Promise<DbTag['id']> {
return insertWithUUID('tags', tag);
}
-export async function deleteTag(tag) {
+export async function deleteTag(tag: Pick<DbTag, 'id'>) {
return delete_('tags', tag.id);
}
-export function updateTag(tag) {
+export function updateTag(tag: Partial<DbTag> & Pick<DbTag, 'id'>) {
return update('tags', tag);
}
diff --git a/packages/loot-core/src/server/db/types/index.ts b/packages/loot-core/src/server/db/types/index.ts
index 14e657988e..236c297526 100644
--- a/packages/loot-core/src/server/db/types/index.ts
+++ b/packages/loot-core/src/server/db/types/index.ts
@@ -349,4 +349,5 @@ export type DbTag = {
color?: string | null;
description?: string | null;
tombstone: 1 | 0;
+ hidden?: 1 | 0;
};
diff --git a/packages/loot-core/src/server/tags/app.ts b/packages/loot-core/src/server/tags/app.ts
index 5dfa33ce36..390057d84b 100644
--- a/packages/loot-core/src/server/tags/app.ts
+++ b/packages/loot-core/src/server/tags/app.ts
@@ -10,6 +10,8 @@ export type TagsHandlers = {
'tags-create': typeof createTag;
'tags-delete': typeof deleteTag;
'tags-delete-all': typeof deleteAllTags;
+ 'tags-hide-all': typeof hideAllTags;
+ 'tags-unhide-all': typeof unhideAllTags;
'tags-update': typeof updateTag;
'tags-discover': typeof discoverTags;
};
@@ -18,7 +20,9 @@ export const app = createApp<TagsHandlers>();
app.method('tags-get', getTags);
app.method('tags-create', mutator(undoable(createTag)));
app.method('tags-delete', mutator(undoable(deleteTag)));
-app.method('tags-delete-all', mutator(deleteAllTags));
+app.method('tags-delete-all', mutator(undoable(deleteAllTags)));
+app.method('tags-hide-all', mutator(undoable(hideAllTags)));
+app.method('tags-unhide-all', mutator(undoable(unhideAllTags)));
app.method('tags-update', mutator(undoable(updateTag)));
app.method('tags-discover', mutator(discoverTags));
@@ -29,7 +33,7 @@ const collator = new Intl.Collator(undefined, {
async function getTags(): Promise<TagEntity[]> {
const tags = await db.getTags();
tags.sort((a, b) => collator.compare(a.tag, b.tag));
- return tags;
+ return tags.map(tag => ({ ...tag, hidden: !!tag.hidden }));
}
async function createTag({
@@ -76,10 +80,32 @@ async function deleteAllTags(
return ids;
}
+async function hideAllTags(ids: Array<TagEntity['id']>) {
+ await batchMessages(async () => {
+ for (const id of ids) {
+ await db.updateTag({ id, hidden: 1 });
+ }
+ });
+ return ids;
+}
+
+async function unhideAllTags(ids: Array<TagEntity['id']>) {
+ await batchMessages(async () => {
+ for (const id of ids) {
+ await db.updateTag({ id, hidden: 0 });
+ }
+ });
+ return ids;
+}
+
async function updateTag(
tag: Partial<TagEntity> & Pick<TagEntity, 'id'>,
): Promise<Partial<TagEntity>> {
- await db.updateTag(tag);
+ const { hidden, ...rest } = tag;
+ await db.updateTag({
+ ...rest,
+ ...(hidden !== undefined ? { hidden: hidden ? 1 : 0 } : {}),
+ });
return tag;
}
diff --git a/packages/loot-core/src/server/transactions/transaction-rules.ts b/packages/loot-core/src/server/transactions/transaction-rules.ts
index ac7e64b125..bedea9b7bf 100644
--- a/packages/loot-core/src/server/transactions/transaction-rules.ts
+++ b/packages/loot-core/src/server/transactions/transaction-rules.ts
@@ -638,7 +638,9 @@ export function conditionsToAQL(
case 'hasAnyTag': {
const tagValues = extractTagsForFilter(value);
-
+ if (tagValues.length === 0) {
+ return { id: null };
+ }
return {
$or: tagValues.map(v => {
const escapedTag = v
diff --git a/packages/loot-core/src/types/models/tags.ts b/packages/loot-core/src/types/models/tags.ts
index 34c27e9575..af4a0f6bf4 100644
--- a/packages/loot-core/src/types/models/tags.ts
+++ b/packages/loot-core/src/types/models/tags.ts
@@ -3,4 +3,5 @@ export type TagEntity = {
tag: string;
color?: string | null;
description?: string | null;
+ hidden?: boolean;
};
diff --git a/packages/loot-core/src/types/prefs.ts b/packages/loot-core/src/types/prefs.ts
index 13dd059b01..bc7a52da42 100644
--- a/packages/loot-core/src/types/prefs.ts
+++ b/packages/loot-core/src/types/prefs.ts
@@ -55,7 +55,8 @@ export type SyncedPrefs = Partial<
| `camt-swap-payee-memo-${string}`
| `flip-amount-${string}-${'csv' | 'qif'}`
| `flags.${FeatureFlag}`
- | `learn-categories`,
+ | `learn-categories`
+ | `show-hidden-tags`,
string
>
>;
diff --git a/upcoming-release-notes/7919.md b/upcoming-release-notes/7919.md
new file mode 100644
index 0000000000..70497e50f0
--- /dev/null
+++ b/upcoming-release-notes/7919.md
@@ -0,0 +1,6 @@
+---
+category: Features
+authors: [alecbakholdin]
+---
+
+Add ability to show or hide tags and added better handling of bulk tag actions