Skip to content

Commit

Permalink
Merge pull request #127 from ujiro99/fix-bugs
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
ujiro99 authored Aug 7, 2023
2 parents 045a3a7 + f6b3653 commit 21998c6
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 420 deletions.
62 changes: 57 additions & 5 deletions src/components/Debug.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import React from 'react'
import { useTrackingState } from '@/hooks/useTrackingState'
import { TrackingState } from '@/@types/global'
import { useTrackingState } from '@/hooks/useTrackingState'
import { useStorage } from '@/hooks/useStorage'
import { EventLine } from '@/hooks/useEventAlarm'
import { useTaskManager } from '@/hooks/useTaskManager'
import { formatTime } from '@/services/util'
import { STORAGE_KEY } from '@/services/storage'
import { isDebug } from '@/const'
import { Time } from '@/models/time'
import { Node, NODE_TYPE } from '@/models/node'
import { Task } from '@/models/task'
import { flattenTree } from '@/components/Tree/utilities'

const trackingsToText = (trackings: TrackingState[]) => {
let res = '<trackings>'
let res = '<Trackings>'
trackings.forEach((tracking) => {
res += '\n line: ' + tracking.line
res += '\n nodeId: ' + tracking.nodeId
Expand All @@ -16,16 +24,60 @@ const trackingsToText = (trackings: TrackingState[]) => {
return res
}

const calendarToText = (calendars: EventLine[]) => {
let res = '<Calendars>'
calendars.forEach((calendar) => {
res += '\n line: ' + calendar.line
res += '\n id: ' + calendar.event.id
res += '\n title: ' + calendar.event.title
res += '\n md: ' + calendar.event.md
res += '\n start: ' + calendar.event.start
res += '\n end: ' + calendar.event.end
res += '\n Length of time: ' + Time.fromTimeObject(calendar.event.time)
})
return res
}

const nodeToText = (root: any) => {
const flatten = flattenTree([root])
let res = '<Node>'
flatten.forEach((item) => {
const n = item as any as Node
if (n.type === NODE_TYPE.ROOT) {
return
}
res += `\n id: ${n.id} line: ${n.line} type: ${n.type} - ${
(n.data as Task).title
}`
})
return res
}

export function Debug(): JSX.Element {
const { trackings } = useTrackingState()
const [sCalendar] = useStorage<EventLine[]>(STORAGE_KEY.CALENDAR_EVENT)
const [sAlarms] = useStorage(STORAGE_KEY.ALARMS)
const root = useTaskManager().getRoot()

if (!isDebug) {
return <></>
}

return (
<pre className="debug">
<code>{trackingsToText(trackings)}</code>
</pre>
<>
<pre className="debug">
<code>{nodeToText(root)}</code>
</pre>
<pre className="debug">
<code>{trackingsToText(trackings)}</code>
</pre>
<pre className="debug">
<code>{calendarToText(sCalendar)}</code>
</pre>
<pre className="debug">
<code>alarms</code> <br />
<code>{JSON.stringify(sAlarms, null, 2)}</code>
</pre>
</>
)
}
1 change: 1 addition & 0 deletions src/components/Popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ body {
line-height: 1;
background: #f0f0f0;
padding: 1em;
margin-bottom: 4px;
border-radius: 4px;
}
12 changes: 7 additions & 5 deletions src/components/Tree/SortableTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function SortableTree({
const items = root.children

const { moveTracking } = useTrackingMove()
const { moveEventLine } = useEventAlarm ()
const { moveEventLine } = useEventAlarm()

const flattenedItems = useMemo(() => {
const flattenedTree = flattenTree(items)
Expand Down Expand Up @@ -272,22 +272,24 @@ export function SortableTree({
const overIndex = clonedItems.findIndex(({ id }) => id === over.id)
const activeIndex = clonedItems.findIndex(({ id }) => id === active.id)
const activeTreeItem = clonedItems[activeIndex]
const activeNode = root.find((n) => n.id === activeTreeItem.id)

clonedItems[activeIndex] = { ...activeTreeItem, depth, parentId }

let sortedItems = arrayMove(clonedItems, activeIndex, overIndex)
sortedItems = updateLines(sortedItems)
const newItems = buildTree(sortedItems)
moveTracking(activeIndex + 1, overIndex + 1)
moveEventLine(activeIndex + 1, overIndex + 1)

moveTracking(activeIndex + 1, overIndex + 1, activeNode.size())
moveEventLine(activeIndex + 1, overIndex + 1, activeNode.size())
setTreeItems(newItems)
}
}

function setTreeItems(newItems: TreeItems) {
// update persistent data
manager.setRoot(treeItemsToNode(newItems))
let newRoot = treeItemsToNode(newItems)
newRoot = updateLines(newRoot)
manager.setRoot(newRoot)
}
function handleDragCancel() {
resetState()
Expand Down
61 changes: 19 additions & 42 deletions src/eventPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@ async function updateIconTime() {
}
}

type NotificationEventId = {
notification: string
event: string
}

chrome.alarms.onAlarm.addListener((param) => {
const alarm = Alarm.fromString(param.name)
Log.d(alarm.type + ' ' + alarm.name)
Expand All @@ -204,6 +199,7 @@ chrome.alarms.onAlarm.addListener((param) => {
})
} else if (alarm.type === ALARM_TYPE.EVENT) {
chrome.notifications.create(
alarm.calendarEventId,
{
type: 'basic',
title: alarm.message,
Expand All @@ -213,23 +209,12 @@ chrome.alarms.onAlarm.addListener((param) => {
{ title: t('alarm_button_start') },
{ title: t('alarm_button_nothing') },
],
},
async (notificationId) => {
const idArr =
((await Storage.get(
STORAGE_KEY.NOTIFICATION_EVENT,
)) as NotificationEventId[]) ?? []
const ids = {
notification: notificationId,
event: alarm.calendarEventId,
}
Storage.set(STORAGE_KEY.NOTIFICATION_EVENT, [...idArr, ids])
},
}
)
}
})

const startTrackingForCalendarEvent = async (notificationId: string) => {
const startTrackingForCalendarEvent = async (eventId: string) => {
const trackings = (await Storage.get(
STORAGE_KEY.TRACKING_STATE,
)) as TrackingState[]
Expand All @@ -239,64 +224,56 @@ const startTrackingForCalendarEvent = async (notificationId: string) => {
const key = TaskRecordKey.fromDate(new Date())

// Find the line number to start tracking
const idArr = (await Storage.get(
STORAGE_KEY.NOTIFICATION_EVENT,
)) as NotificationEventId[]
const eventId = idArr.find((n) => n.notification === notificationId)?.event
const line = eventLines.find((e) => e.event.id === eventId)?.line

if (line == null) {
// It is not a notification to CalendarEvent, so does nothing.
Log.e('line not found')
return
}

// Stop other tracking
// TODO: Check if already tracking

// Stop other tracking & alarms
const records = await loadRecords()
const root = selectRecord(key, records)
const [newRoot, events] = await stopTrackings(root, trackings)
saveStates(key, newRoot, trackings, events)
const [newRoot, events] = stopTrackings(root, trackings)
onMessageFuncs.stopAlarmsForTask(0, () => { })

// Update tracking state
onMessageFuncs.startTracking(0, () => {})
onMessageFuncs.startTracking(0, () => { })
const tracking = {
isTracking: true,
nodeId: null,
trackingStartTime: Date.now(),
key: key.toKey(),
elapsedTime: new Time(),
line,
}
await Storage.set(STORAGE_KEY.TRACKING_STATE, [tracking])
saveStates(key, newRoot, [tracking], events)

// Set alarms for task
onMessageFuncs.stopAlarmsForTask(0, () => {})
const alarmRules = (await Storage.get(STORAGE_KEY.ALARMS)) as AlarmRule[]
const node = root.find((n) => n.line === line)
if (node != null && node.type === NODE_TYPE.TASK) {
const alarms = AlarmService.taskToAlarms(node.data as Task, alarmRules)
alarms.forEach((alarm) => {
onMessageFuncs.setAlarm(alarm, () => {})
onMessageFuncs.setAlarm(alarm, () => { })
})
}

// Delete notified IDs
await Storage.set(
STORAGE_KEY.NOTIFICATION_EVENT,
idArr.filter((n) => n.notification !== notificationId),
)
}

chrome.notifications.onClicked.addListener(async (notificationId) => {
Log.d(`${notificationId}`)
await startTrackingForCalendarEvent(notificationId)
chrome.notifications.onClicked.addListener(async (evnetId) => {
Log.d(`${evnetId}`)
await startTrackingForCalendarEvent(evnetId)
})

chrome.notifications.onButtonClicked.addListener(
async (notificationId, buttonIndex) => {
Log.d(`${notificationId}, ${buttonIndex}`)
async (evnetId, buttonIndex) => {
Log.d(`${evnetId}, ${buttonIndex}`)
if (buttonIndex !== 0) {
// Do nothing button clicked.
return
}
await startTrackingForCalendarEvent(notificationId)
await startTrackingForCalendarEvent(evnetId)
},
)
Loading

0 comments on commit 21998c6

Please sign in to comment.