Skip to content

Commit

Permalink
fix: filter to most recent 3 weeks by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcknett committed Jan 4, 2024
1 parent 16f7bbb commit ce24e2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/components/LogTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export function LogTable() {
const fBaseContext = useContext(FirebaseContext)!;

const [selectedList, setSelectedList] = useState<string | undefined>(undefined);
const [filterOldLogs, setFilterOldLogs] = useState<boolean>(true);

const { account: { recentList } } = useAccount(fBaseContext);
const { logs, lists } = useLogs(fBaseContext, selectedList);
const { logs, lists } = useLogs(fBaseContext, selectedList, filterOldLogs);
logs.sort((a: ILog, b: ILog) => !a.endTime || !b.endTime ? 0 : b.endTime?.seconds - a.endTime?.seconds);

const groups = [];
Expand Down Expand Up @@ -48,6 +48,8 @@ export function LogTable() {
}
</select>
<button onClick={() => setSelectedList(undefined)}>Clear</button>
<span className='vl' />
<button onClick={() => setFilterOldLogs(!filterOldLogs)}>{filterOldLogs ? 'Show All' : 'Show Only Recent'}</button>
</div>
<table>
<thead>
Expand Down
25 changes: 21 additions & 4 deletions src/hooks/use-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,35 @@ import {
deleteDoc,
DocumentReference,
query,
Timestamp,
where
} from "firebase/firestore";
import { useCollectionData } from 'react-firebase-hooks/firestore';

import { IFirebaseContext } from "../data/FirebaseContext";
import { DEFAULT_LIST, ILog, logConverter } from '../data/data-types';
import { ILog, logConverter } from '../data/data-types';
import { checkedLogPath } from "../data/paths";
import { saveMruListAndDeleteDraft } from "./use-account";

export function useLogs(fBaseContext: IFirebaseContext, listName?: string | undefined) {
const logsCollection = collection(fBaseContext.db, checkedLogPath(fBaseContext)).withConverter(logConverter);
const logsQuery = listName ? query(logsCollection, where("list", "==", listName)) : logsCollection;
export function useLogs(fBaseContext: IFirebaseContext, listName?: string | undefined, filterOldLogs: boolean = true) {
const LOOKBACK_DAYS = 21;
const startDate = new Date();
startDate.setHours(0, 0, 0, 0);
startDate.setDate(startDate.getDate() - LOOKBACK_DAYS);
const startTimestamp = Timestamp.fromDate(startDate);

const filters = [];
if (filterOldLogs) {
filters.push(where("startTime", ">=", startTimestamp));
}
if (listName) {
filters.push(where("list", "==", listName));
}

const logPath = checkedLogPath(fBaseContext);
const logsCollection = collection(fBaseContext.db, logPath).withConverter(logConverter);
const logsQuery = query(logsCollection, ...filters);

const [logsSnapshot, loading, error] = useCollectionData(logsQuery);

const logs: ILog[] = logsSnapshot || [];
Expand Down

0 comments on commit ce24e2f

Please sign in to comment.