Skip to content

Commit eef607e

Browse files
committed
Fix list memo date time
1 parent 737f84f commit eef607e

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

src/memos.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,45 @@ export function getMemoTags(): Array<{ tag: string; count: number }> {
152152
export function formatMemoList(memos: Memo[]): string {
153153
if (memos.length === 0) return 'Nenhum memo encontrado.'
154154

155-
const lines = memos.map((m) => {
156-
const date = new Date(m.updatedAt).toLocaleDateString('pt-BR', {
157-
day: '2-digit', month: '2-digit',
158-
})
155+
const now = new Date()
156+
const todayStr = toDateKey(now)
157+
const yesterdayDate = new Date(now)
158+
yesterdayDate.setDate(yesterdayDate.getDate() - 1)
159+
const yesterdayStr = toDateKey(yesterdayDate)
160+
161+
const groups: Record<string, string[]> = {}
162+
const groupOrder: string[] = []
163+
164+
for (const m of memos) {
165+
const mDate = new Date(m.updatedAt)
166+
const mKey = toDateKey(mDate)
167+
168+
let label: string
169+
if (mKey === todayStr) {
170+
label = `Hoje (${formatDatePtBr(mDate)})`
171+
} else if (mKey === yesterdayStr) {
172+
label = `Ontem (${formatDatePtBr(mDate)})`
173+
} else {
174+
label = formatDatePtBr(mDate)
175+
}
176+
177+
if (!groups[label]) {
178+
groups[label] = []
179+
groupOrder.push(label)
180+
}
181+
159182
const tags = m.tags.length > 0 ? ` [${m.tags.map((t) => `#${t}`).join(' ')}]` : ''
160183
const preview = m.content.length > 80
161184
? m.content.slice(0, 80).replace(/\n/g, ' ') + '...'
162185
: m.content.replace(/\n/g, ' ')
163-
return ` [${date}] ${preview}${tags} {${m.id}}`
164-
})
186+
groups[label].push(` ${preview}${tags} {${m.id}}`)
187+
}
165188

166-
return `Memos (${memos.length}):\n${lines.join('\n')}`
189+
const sections = groupOrder.map((label) =>
190+
` ${label}:\n${groups[label].map((l) => ` ${l}`).join('\n')}`,
191+
)
192+
193+
return `Memos (${memos.length}):\n${sections.join('\n\n')}`
167194
}
168195

169196
export function formatMemoDetail(memo: Memo): string {
@@ -187,3 +214,16 @@ export function formatMemoTags(): string {
187214
function genId(): string {
188215
return randomUUID().slice(0, 8)
189216
}
217+
218+
/** YYYY-MM-DD key for date comparison (local timezone). */
219+
function toDateKey(d: Date): string {
220+
const y = d.getFullYear()
221+
const m = String(d.getMonth() + 1).padStart(2, '0')
222+
const day = String(d.getDate()).padStart(2, '0')
223+
return `${y}-${m}-${day}`
224+
}
225+
226+
/** dd/MM format for display. */
227+
function formatDatePtBr(d: Date): string {
228+
return d.toLocaleDateString('pt-BR', { day: '2-digit', month: '2-digit' })
229+
}

0 commit comments

Comments
 (0)