Skip to content

Commit

Permalink
feat(dropdown): Remember selected filter
Browse files Browse the repository at this point in the history
The filter drop down currently always uses All by default.
This default can be annoying if you have a deleted masked emails
that sort near the top.

This changes FilterEmailsDropdown to remember the selected filter
between invocations, so the default changes to what you selected last
time.

Note that it remembers the value only when changed with the drop down
and not when you create a new masked email.
  • Loading branch information
abhinav committed Aug 12, 2023
1 parent 2d34be7 commit bf339af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
34 changes: 31 additions & 3 deletions src/pages/popup/components/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ import FilterEmailsDropdown from '@pages/popup/components/home/filter/FilterEmai
import TopComponent from '@pages/popup/components/home/top/Top';
import EmailCount from '@pages/popup/components/home/emails/EmailCount';
import browser from 'webextension-polyfill';
import { getFastmailSession } from '../../../../../utils/storageUtil';
import {
getFastmailSession,
getDefaultFilter,
setDefaultFilter
} from '../../../../../utils/storageUtil';
import CreateEmailModal from '@pages/popup/components/home/detail/modals/CreateEmailModal';
import LogoutConfirmationModal from '@pages/popup/components/home/detail/modals/LogoutConfirmationModal';
import { FILTER_OPTIONS } from '@pages/popup/components/home/filter/FilterOption';
import {
FILTER_OPTIONS,
FilterOption
} from '@pages/popup/components/home/filter/FilterOption';

interface HomeComponentProps {
onLogout: () => void;
Expand Down Expand Up @@ -43,6 +50,21 @@ export default function HomeComponent({ onLogout }: HomeComponentProps) {
setShowLogoutConfirmationModal(true);
};

const setFilterOptionAndRemember = (
value: ((prevState: FilterOption) => FilterOption) | FilterOption
) => {
if (typeof value == 'function') {
setFilterOption((prevState) => {
const newValue = value(prevState);
setDefaultFilter(newValue);
return newValue;
});
} else {
setFilterOption(value);
setDefaultFilter(value);
}
};

useEffect(() => {
// Declare the async function
const fetchActiveTabUrl = async () => {
Expand All @@ -58,8 +80,14 @@ export default function HomeComponent({ onLogout }: HomeComponentProps) {
}
};

const fetchDefaultFilter = async () => {
const filter = await getDefaultFilter();
setFilterOption(filter);
};

// Call the async function
fetchActiveTabUrl();
fetchDefaultFilter();
}, []);

const refreshMaskedEmails = async () => {
Expand Down Expand Up @@ -128,7 +156,7 @@ export default function HomeComponent({ onLogout }: HomeComponentProps) {
<div className="columns-[250px] border-r border-r-big-stone">
<div className="h-[35px] border-b border-b-big-stone flex items-center justify-center">
<FilterEmailsDropdown
setFilterOption={setFilterOption}
setFilterOption={setFilterOptionAndRemember}
filterOption={filterOption}
/>
<EmailCount count={filteredEmailsCount} />
Expand Down
1 change: 1 addition & 0 deletions utils/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const outputFolderName = 'dist';
export const FASTMAIL_SESSION_KEY = 'fastmail_session';
export const FAVORITE_EMAILS_KEY = 'favorite_emails';
export const DEFAULT_FILTER_KEY = 'default_filter';
14 changes: 13 additions & 1 deletion utils/storageUtil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
FASTMAIL_SESSION_KEY,
FAVORITE_EMAILS_KEY
FAVORITE_EMAILS_KEY,
DEFAULT_FILTER_KEY
} from './constants/constants';
import browser from 'webextension-polyfill';
import { Session } from 'fastmail-masked-email';
import { FILTER_OPTIONS, FilterOption } from '@src/pages/popup/components/home/filter/FilterOption';

export const getFavoriteEmailIds = async (): Promise<string[]> => {
const data = await browser.storage.sync.get(FAVORITE_EMAILS_KEY);
Expand All @@ -25,6 +27,16 @@ export const setFastmailSession = async (session: Session): Promise<void> => {
await browser.storage.sync.set({ [FASTMAIL_SESSION_KEY]: session });
};

export const getDefaultFilter = async (): Promise<FilterOption> => {
const data = await browser.storage.sync.get(DEFAULT_FILTER_KEY);
const value = data[DEFAULT_FILTER_KEY] || 'All';
return FILTER_OPTIONS[value] || FILTER_OPTIONS.All;
}

export const setDefaultFilter = async (filter: FilterOption): Promise<void> => {
await browser.storage.sync.set({ [DEFAULT_FILTER_KEY]: filter.value });
}

export const clearStorage = async (): Promise<void> => {
await browser.storage.sync.clear();
};

0 comments on commit bf339af

Please sign in to comment.