Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions apps/mobile/__tests__/components/habits/habit-row.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, it, expect, vi } from 'vitest'
import { createMockHabit } from '@orbit/shared/__tests__/factories'
import { HabitRow } from '@/components/habits/habit-row'

const TestRenderer = require('react-test-renderer')

vi.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key, i18n: { language: 'en' } }),
}))

vi.mock('@/hooks/use-time-format', () => ({
useTimeFormat: () => ({ displayTime: (value: string) => value }),
}))

vi.mock('@/lib/use-app-theme', () => ({
useAppTheme: () => ({ currentScheme: 'purple', currentTheme: 'dark' }),
}))

vi.mock('@/lib/motion', () => ({
usePrefersReducedMotion: () => true,
}))

function collectStrings(node: unknown): string[] {
if (node == null) return []
if (typeof node === 'string') return [node]
if (Array.isArray(node)) return node.flatMap(collectStrings)
return collectStrings((node as { children?: unknown }).children)
}

function renderRowText(habit: ReturnType<typeof createMockHabit>): string[] {
let tree: { toJSON: () => unknown }
TestRenderer.act(() => {
tree = TestRenderer.create(<HabitRow habit={habit} />)
})
return collectStrings(tree!.toJSON())
}

describe('HabitRow tags (mobile)', () => {
it('renders the habit tag names on the row', () => {
const texts = renderRowText(
createMockHabit({
title: 'Read',
tags: [
{ id: '1', name: 'Learning', color: '#7c3aed' },
{ id: '2', name: 'Evening', color: '#10b981' },
],
}),
)

expect(texts).toContain('Learning')
expect(texts).toContain('Evening')
})

it('caps visible tags at three and shows a +N overflow counter', () => {
const texts = renderRowText(
createMockHabit({
title: 'Read',
tags: Array.from({ length: 10 }, (_, i) => ({
id: String(i),
name: `Tag${i}`,
color: '#7c3aed',
})),
}),
)

expect(texts).toContain('Tag0')
expect(texts).toContain('Tag2')
expect(texts).not.toContain('Tag3')
expect(texts.join('')).toContain('+7')
})
})
35 changes: 34 additions & 1 deletion apps/mobile/components/habits/habit-detail-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function HabitDetailContent({
showsVerticalScrollIndicator={false}
keyboardShouldPersistTaps="always"
>
{habit.emoji || summaryStrip ? (
{habit.emoji || summaryStrip || habit.tags.length > 0 ? (
<View style={styles.titleBlock}>
{habit.emoji ? (
<View
Expand All @@ -166,6 +166,18 @@ function HabitDetailContent({
{summaryStrip}
</Text>
) : null}
{habit.tags.length > 0 ? (
<View style={styles.drawerTagRow}>
{habit.tags.map((tag) => (
<View key={tag.id} style={styles.drawerTagChip}>
<View style={[styles.drawerTagDot, { backgroundColor: tag.color }]} />
<Text style={[styles.drawerTagName, { color: tokens.fg3 }]}>
{tag.name}
</Text>
</View>
))}
</View>
) : null}
</View>
) : null}

Expand Down Expand Up @@ -468,6 +480,27 @@ function createStyles(tokens: ReturnType<typeof createTokensV2>) {
paddingTop: 8,
paddingBottom: 16,
},
drawerTagRow: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
alignItems: 'center',
gap: 8,
},
drawerTagChip: {
flexDirection: 'row',
alignItems: 'center',
gap: 5,
},
drawerTagDot: {
width: 6,
height: 6,
borderRadius: 3,
},
drawerTagName: {
fontFamily: 'Rubik_400Regular',
fontSize: 13,
},
emojiWell: {
width: 76,
height: 76,
Expand Down
48 changes: 48 additions & 0 deletions apps/mobile/components/habits/habit-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import { ParentRing } from '@/components/ui/parent-ring'
import { SelectCheck } from '@/components/ui/select-check'
import type { StatusDotState } from '@/components/ui/status-dot'

const MAX_VISIBLE_TAGS = 3

/**
* Action callbacks consumed by HabitRow.
*/
Expand Down Expand Up @@ -439,6 +441,24 @@ function HabitRowContent({
) : null}
</Text>
) : null}

{habit.tags.length > 0 ? (
<View style={styles.tagRow}>
{habit.tags.slice(0, MAX_VISIBLE_TAGS).map((tag) => (
<View key={tag.id} style={styles.tagChip}>
<View style={[styles.tagDot, { backgroundColor: tag.color }]} />
<Text numberOfLines={1} style={[styles.tagName, { color: tokens.fg3 }]}>
{tag.name}
</Text>
</View>
))}
{habit.tags.length > MAX_VISIBLE_TAGS ? (
<Text style={[styles.tagOverflow, { color: tokens.fg3 }]}>
+{habit.tags.length - MAX_VISIBLE_TAGS}
</Text>
) : null}
</View>
) : null}
</View>
)
}
Expand Down Expand Up @@ -827,6 +847,34 @@ const styles = StyleSheet.create({
fontSize: 13,
lineHeight: 17,
},
tagRow: {
flexDirection: 'row',
alignItems: 'center',
gap: 8,
marginTop: 1,
overflow: 'hidden',
},
tagChip: {
flexDirection: 'row',
alignItems: 'center',
gap: 5,
flexShrink: 0,
},
tagDot: {
width: 6,
height: 6,
borderRadius: 3,
},
tagName: {
fontFamily: 'Rubik_400Regular',
fontSize: 12,
maxWidth: 96,
},
tagOverflow: {
fontFamily: 'Roboto_400Regular',
fontSize: 12,
flexShrink: 0,
},
trailing: {
flexDirection: 'row',
alignItems: 'center',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ vi.mock('@/components/ui/app-overlay', () => ({
open,
children,
title,
titleContent,
footer,
}: {
open: boolean
children: React.ReactNode
title?: string
titleContent?: React.ReactNode
description?: string
footer?: React.ReactNode
expandable?: boolean
onExpandDescription?: () => void
}) =>
open ? (
<div data-testid="app-overlay">
{title && <h2>{title}</h2>}
{(titleContent || title) && <h2>{titleContent || title}</h2>}
{children}
{footer}
</div>
Expand Down Expand Up @@ -197,6 +199,24 @@ describe('HabitDetailDrawer', () => {
expect(screen.getByText('Exercise')).toBeDefined()
})

it('renders the habit tags in the detail header', () => {
render(
<HabitDetailDrawer
open={true}
onOpenChange={vi.fn()}
habit={createMockHabit({
title: 'Read',
tags: [
{ id: '1', name: 'Learning', color: '#7c3aed' },
{ id: '2', name: 'Evening', color: '#10b981' },
],
})}
/>,
)
expect(screen.getByText('Learning')).toBeDefined()
expect(screen.getByText('Evening')).toBeDefined()
})

it('shows due time when habit has dueTime', () => {
render(
<HabitDetailDrawer
Expand Down
40 changes: 40 additions & 0 deletions apps/web/__tests__/components/habits/habit-row.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,43 @@ describe('HabitRow description preview', () => {
expect(screen.getByText('Child preview')).toBeDefined()
})
})

describe('HabitRow tags', () => {
it('renders the habit tag names on the row', () => {
render(
<HabitRow
habit={createMockHabit({
title: 'Read',
tags: [
{ id: '1', name: 'Learning', color: '#7c3aed' },
{ id: '2', name: 'Evening', color: '#10b981' },
],
})}
/>,
)

expect(screen.getByText('Learning')).toBeDefined()
expect(screen.getByText('Evening')).toBeDefined()
})

it('caps visible tags at three and shows a +N overflow counter for the rest', () => {
render(
<HabitRow
habit={createMockHabit({
title: 'Read',
tags: Array.from({ length: 10 }, (_, i) => ({
id: String(i),
name: `Tag${i}`,
color: '#7c3aed',
})),
})}
/>,
)

expect(screen.getByText('Tag0')).toBeDefined()
expect(screen.getByText('Tag1')).toBeDefined()
expect(screen.getByText('Tag2')).toBeDefined()
expect(screen.queryByText('Tag3')).toBeNull()
expect(screen.getByText('+7')).toBeDefined()
})
})
23 changes: 23 additions & 0 deletions apps/web/components/habits/habit-detail-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ export function HabitDetailDrawer({
{summaryStrip}
</span>
) : null}
{habit.tags.length > 0 ? (
<span className="flex flex-wrap items-center justify-center" style={{ gap: 8 }}>
{habit.tags.map((tag) => (
<span key={tag.id} className="inline-flex items-center" style={{ gap: 5 }}>
<span
aria-hidden="true"
className="rounded-full shrink-0"
style={{ width: 6, height: 6, background: tag.color }}
/>
<span
style={{
fontFamily: 'var(--font-sans)',
fontSize: 13,
color: 'var(--fg-3)',
lineHeight: 1.2,
}}
>
{tag.name}
</span>
</span>
))}
</span>
) : null}
</span>
) : undefined
}
Expand Down
43 changes: 43 additions & 0 deletions apps/web/components/habits/habit-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { Popover } from '@/components/ui/popover'
import { SelectCheck } from '@/components/ui/select-check'
import type { StatusDotState } from '@/components/ui/status-dot'

const MAX_VISIBLE_TAGS = 3

/** Action callbacks consumed by HabitRow. Mirrors the mobile shape so that
* cross-platform call sites can pass the same handler bag. */
export interface HabitRowActions {
Expand Down Expand Up @@ -277,6 +279,47 @@ export function HabitRow({
)}
</span>
)}
{habit.tags.length > 0 && (
<span className="flex items-center overflow-hidden" style={{ gap: 8, marginTop: 1 }}>
{habit.tags.slice(0, MAX_VISIBLE_TAGS).map((tag) => (
<span
key={tag.id}
className="inline-flex items-center shrink-0 min-w-0"
style={{ gap: 5, maxWidth: 132 }}
>
<span
aria-hidden="true"
className="rounded-full shrink-0"
style={{ width: 6, height: 6, background: tag.color }}
/>
<span
className="truncate"
style={{
fontFamily: 'var(--font-sans)',
fontSize: 12,
color: 'var(--fg-3)',
lineHeight: 1.2,
}}
>
{tag.name}
</span>
</span>
))}
{habit.tags.length > MAX_VISIBLE_TAGS && (
<span
className="shrink-0"
style={{
fontFamily: 'var(--font-sans)',
fontSize: 12,
color: 'var(--fg-3)',
fontVariantNumeric: 'tabular-nums',
}}
>
+{habit.tags.length - MAX_VISIBLE_TAGS}
</span>
)}
</span>
)}
</div>

<div className="flex items-center shrink-0" style={{ gap: 10 }}>
Expand Down