Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 3 additions & 20 deletions apps/mobile/hooks/use-habit-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,9 @@ export function useHabitForm(options: HabitFormOptions = {}): HabitFormHelpers {
},
})

const watchedFrequencyUnit = useWatch({ control: form.control, name: 'frequencyUnit' })
const watchedFrequencyQuantity = useWatch({
control: form.control,
name: 'frequencyQuantity',
})
const watchedDays = useWatch({ control: form.control, name: 'days' })
const watchedIsGeneral = useWatch({ control: form.control, name: 'isGeneral' })
const watchedIsFlexible = useWatch({ control: form.control, name: 'isFlexible' })
const watchedDueTime = useWatch({ control: form.control, name: 'dueTime' })
const watchedEndDate = useWatch({ control: form.control, name: 'endDate' })

const watchedValues = normalizeHabitFormData({
frequencyUnit: watchedFrequencyUnit,
frequencyQuantity: watchedFrequencyQuantity,
days: watchedDays,
isGeneral: watchedIsGeneral,
isFlexible: watchedIsFlexible,
dueTime: watchedDueTime,
endDate: watchedEndDate,
})
const watchedValues = normalizeHabitFormData(
useWatch({ control: form.control }) as HabitFormInput,
)
const { isOneTime, isGeneral, isFlexible, isRecurring, showDayPicker, showEndDate } =
getHabitFormFlags(watchedValues)

Expand Down
42 changes: 2 additions & 40 deletions apps/mobile/hooks/use-habits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import {
import { API } from '@orbit/shared/api'
import {
applyLinkedGoalUpdates,
buildOptimisticSkipPatch,
findHabitInList,
formatAPIDate,
normalizeHabits,
} from '@orbit/shared/utils'
import type {
HabitScheduleItem,
HabitScheduleChild,
HabitDetail,
HabitFullDetail,
LogHabitResponse,
Expand Down Expand Up @@ -75,46 +76,7 @@ type CreateSubHabitMutationInput = {
__offlineTempId?: string
}
type HabitListSnapshots = readonly (readonly [readonly unknown[], HabitScheduleItem[] | undefined])[]
type HabitTreeNode = HabitScheduleItem | HabitScheduleChild

function getTomorrowDateString(): string {
const tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1)
return formatAPIDate(tomorrow)
}

function findHabitInTree(node: HabitTreeNode, habitId: string): HabitTreeNode | null {
if (node.id === habitId) return node

for (const child of node.children) {
const match = findHabitInTree(child, habitId)
if (match) return match
}

return null
}

function findHabitInList(items: HabitScheduleItem[], habitId: string): HabitTreeNode | null {
for (const item of items) {
const match = findHabitInTree(item, habitId)
if (match) return match
}

return null
}

function buildOptimisticSkipPatch(habit: HabitTreeNode): Partial<HabitScheduleItem> {
if (habit.frequencyUnit !== null) return { isCompleted: true }

const dueDate = getTomorrowDateString()
return {
isCompleted: false,
dueDate,
scheduledDates: [dueDate],
isOverdue: false,
instances: [{ date: dueDate, status: 'Pending', logId: null }],
}
}
export {
EMPTY_CHILDREN_BY_PARENT,
EMPTY_HABITS_BY_ID,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/(app)/calendar-sync/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function AutoSyncSettingsCard() {
() =>
formatCalendarAutoSyncLastSynced(
state?.lastSyncedAt ?? null,
(key, values) => t(key as never, values as never),
(key, values) => t(key, values),
),
[state?.lastSyncedAt, t],
)
Expand Down
44 changes: 2 additions & 42 deletions apps/web/hooks/use-habits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { useTranslations } from 'next-intl'
import { habitKeys, goalKeys, gamificationKeys, profileKeys } from '@orbit/shared/query'
import {
applyLinkedGoalUpdates,
formatAPIDate,
buildOptimisticSkipPatch,
findHabitInList,
normalizeHabits,
} from '@orbit/shared/utils'
import {
Expand Down Expand Up @@ -72,47 +73,6 @@ type HabitListSnapshots = ReadonlyArray<
readonly [readonly unknown[], HabitScheduleItem[] | undefined]
>

type HabitTreeNode = HabitScheduleItem | HabitScheduleChild

function getTomorrowDateString(): string {
const tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1)
return formatAPIDate(tomorrow)
}

function findHabitInTree(node: HabitTreeNode, habitId: string): HabitTreeNode | null {
if (node.id === habitId) return node

for (const child of node.children) {
const match = findHabitInTree(child, habitId)
if (match) return match
}

return null
}

function findHabitInList(items: HabitScheduleItem[], habitId: string): HabitTreeNode | null {
for (const item of items) {
const match = findHabitInTree(item, habitId)
if (match) return match
}

return null
}

function buildOptimisticSkipPatch(habit: HabitTreeNode): Partial<HabitScheduleItem> {
if (habit.frequencyUnit !== null) return { isCompleted: true }

const dueDate = getTomorrowDateString()
return {
isCompleted: false,
dueDate,
scheduledDates: [dueDate],
isOverdue: false,
instances: [{ date: dueDate, status: 'Pending', logId: null }],
}
}

function snapshotHabitLists(queryClient: ReturnType<typeof useQueryClient>): HabitListSnapshots {
return queryClient.getQueriesData<HabitScheduleItem[]>({
queryKey: habitKeys.lists(),
Expand Down
Loading