import { MessagePackType } from "@openchat/types" import { DateUtils } from…
- User
import { MessagePackType } from "@openchat/types" import { DateUtils } from "@openchat/framework" import { useEffect, useMemo, useState } from "react" import { useDeepDep } from "../../hook/index.js" import { UIGlobals } from "../../app/ui-globals.js"
export const useMessagePack = (messageId: string | undefined | null) => { const [messagePack, setMessagePack] = useState<MessagePackType | null>(null)
useEffect(() => { if (!messageId) return
UIGlobals.AppController.message .getById({ id: messageId }) .then((messagePack) => setMessagePack(messagePack))}, [messageId])
return messagePack }
export function useMessagePackBoardFilter( allMessagePacks: MessagePackType[], filter: { sectionId?: string sortByOrder?: boolean keyword?: string userIds?: string[] tagIds?: string[] date?: Date | null }, ) { return useMemo(() => { let filteredMessagePacks: MessagePackType[] = [...allMessagePacks]
if (filter.sectionId) { filteredMessagePacks = filteredMessagePacks.filter( (messagePack) => messagePack.message.board && messagePack.message.board.sectionId === filter.sectionId, ) } if (filter.sortByOrder) { filteredMessagePacks.sort((a, b) => { if (a.message.board && b.message.board) { return a.message.board.order - b.message.board.order } return 0 }) } if ( filter.keyword || (filter.userIds && filter.userIds.length) || (filter.tagIds && filter.tagIds.length) || filter.date ) { filteredMessagePacks = filteredMessagePacks.filter((messagePack) => { const { board } = messagePack.message if (!board) return false const matchesKeyword = !filter.keyword || (board.title && board.title.toLowerCase().includes(filter.keyword.toLowerCase())) const matchesUserIds = !filter.userIds || !filter.userIds.length || board.attachedUserIds.some( (userId) => filter.userIds && filter.userIds.includes(userId), ) const matchesTagIds = !filter.tagIds || !filter.tagIds.length || board.tagIds.some( (tagId) => filter.tagIds && filter.tagIds.includes(tagId), ) const matchesDate = !filter.date || (board.startDate && DateUtils.createDate(board.startDate).getDate() <= filter.date.getDate()) || (board.endDate && DateUtils.createDate(board.endDate).getDate() >= filter.date.getDate()) return matchesKeyword && matchesUserIds && matchesTagIds && matchesDate }) } return filteredMessagePacks}, [allMessagePacks, useDeepDep(filter)]) } how to filter by checking if board.startDate contains or matches the filter.date ?
- Assistant
To filter message packs based on whether the
board.startDatematches thefilter.dateexactly, you need to adjust thematchesDatetest in youruseMemoblock. The logic you've written already checks if theboard.startDateis before or the same as thefilter.dateand ifboard.endDateis after or the same as thefilter.date. However, if you want an exact match forboard.startDatewithfilter.date, you should update the condition.Here's an example modification of that logic:
const matchesDate = !filter.date || (board.startDate && DateUtils.createDate(board.startDate).toDateString() === filter.date.toDateString())This condition now checks whether
board.startDateexists and whether its date (ignoring time of day) matchesfilter.date. ThetoDateString()method is used here to compare only the date parts, excluding any time differences.So, updating your
useMemoblock formatchesDatelike this should filter the message packs to include only those whereboard.startDatematches thefilter.date. - User
not worked
- Assistant
If the
toDateString()method didn't work as intended, let's ensure that the date comparison is accurate regardless of time differences. We need to compare the dates by normalizing them, ensuring both dates have the same format without considering the time.Here's an updated approach to filter based on an exact match for the dates, focusing on constructing a consistent comparison:
const matchesDate = !filter.date || (board.startDate && DateUtils.createDate(board.startDate).setHours(0, 0, 0, 0) === filter.date.setHours(0, 0, 0, 0))Explanation:
-
Normalization: Both
board.startDateandfilter.dateare normalized by usingsetHours(0, 0, 0, 0). This sets the time portion of the date to midnight, effectively ignoring the time part and focusing purely on the date. -
Comparison: The
setHours(0, 0, 0, 0)method returns the number of milliseconds since the Unix epoch for that date at midnight, allowing for a straightforward and reliable comparison of the two dates.
If this still doesn't work:
-
Debugging: Add some debugging log statements to verify that the dates are being parsed and compared as expected. For instance:
console.log('Board Start Date:', DateUtils.createDate(board.startDate)); console.log('Filter Date:', filter.date); -
DateUtils Functionality: Double-check that
DateUtils.createDate(board.startDate)correctly parses thestartDatefrom your data format to a JavaScriptDateobject. You may need to adjust this parsing step if the source format is not compatible.
By ensuring the dates are being handled consistently, these adjustments should help resolve the issue. If the problem persists, double-check the data types and ensure that the dates are stored and manipulated consistently throughout your code.
-
- User
Board Start Date: 2024-08-16T00:00:00.000Z Filter Date: 2024-08-16T14:17:44.142Z
- Assistant
The issue appears to be that the
startDateand thefilter.datehave different times, which could affect direct comparison. From your log:- Board Start Date:
2024-08-16T00:00:00.000Z(Midnight in UTC) - Filter Date:
2024-08-16T14:17:44.142Z(Different time on the same date)
Despite the same calendar date, these two timestamps won't match directly due to the time component. To resolve this, we need to ensure that both dates are compared using only their date parts.
Here's a refined approach to handle the normalization and comparison:
const matchesDate = !filter.date || (board.startDate && DateUtils.createDate(board.startDate).toISOString().slice(0, 10) === filter.date.toISOString().slice(0, 10));Breakdown:
-
toISOString(): Converts both dates to a standard string format that is easy to manipulate. It represents the date in theYYYY-MM-DDTHH:mm:ss.sssZformat. -
.slice(0, 10): Extracts theYYYY-MM-DDpart of the date string, effectively removing the time component and leaving just the date for the comparison.
Additional Tips:
-
Time Zones: Ensure both dates are in the same time zone context when performing the comparison. The conversion to ISO format is helpful here because it gives a consistent baseline.
-
Date Parsing: Double-check that
DateUtils.createDatecorrectly interprets the date string as UTC when needed, especially if your input dates are stored or provided in a different format.
Using the above method, the comparison should now correctly verify if the calendar date matches, ignoring differences in time.
- Board Start Date: