chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { deleteAllTimersMemory } from '../lib/memory'
|
||||
|
||||
export const run: ActionFunction = async function () {
|
||||
await deleteAllTimersMemory()
|
||||
|
||||
await leon.answer({ key: 'timer_canceled' })
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { TimerWidget } from '../widgets/timer-widget'
|
||||
import { getTimerMemoryByWidgetId, getNewestTimerMemory } from '../lib/memory'
|
||||
|
||||
export const run: ActionFunction = async function (_params, paramsHelper) {
|
||||
const widgetId = paramsHelper.getWidgetId()
|
||||
const timerMemory = widgetId
|
||||
? await getTimerMemoryByWidgetId(widgetId)
|
||||
: await getNewestTimerMemory()
|
||||
|
||||
if (!timerMemory) {
|
||||
await leon.answer({ key: 'no_timer_set' })
|
||||
return
|
||||
}
|
||||
|
||||
const { interval, finishedAt, duration } = timerMemory
|
||||
let remainingTime = finishedAt - Math.floor(Date.now() / 1_000)
|
||||
if (remainingTime <= 0) {
|
||||
remainingTime = 0
|
||||
}
|
||||
const initialProgress = 100 - (remainingTime / duration) * 100
|
||||
|
||||
const timerWidget = new TimerWidget({
|
||||
params: {
|
||||
seconds: remainingTime,
|
||||
initialProgress,
|
||||
initialDuration: duration,
|
||||
interval
|
||||
},
|
||||
onFetch: {
|
||||
widgetId: widgetId ?? timerMemory.widgetId,
|
||||
actionName: 'check_timer'
|
||||
}
|
||||
})
|
||||
|
||||
await leon.answer({
|
||||
widget: timerWidget,
|
||||
key: 'timer_status',
|
||||
data: {
|
||||
remaining_time: remainingTime
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import type { ActionFunction } from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { TimerWidget } from '../widgets/timer-widget'
|
||||
import { createTimerMemory } from '../lib/memory'
|
||||
|
||||
interface TimerDuration {
|
||||
value?: number
|
||||
unit?: string
|
||||
}
|
||||
|
||||
export const run: ActionFunction = async function (params) {
|
||||
const supportedUnits = ['hours', 'minutes', 'seconds']
|
||||
const duration = (params.action_arguments['duration'] as TimerDuration) || null
|
||||
|
||||
if (
|
||||
!duration ||
|
||||
typeof duration.value !== 'number' ||
|
||||
typeof duration.unit !== 'string'
|
||||
) {
|
||||
await leon.answer({ key: 'cannot_get_duration' })
|
||||
return
|
||||
}
|
||||
|
||||
const normalizedUnit = duration.unit.toLowerCase()
|
||||
if (!supportedUnits.includes(normalizedUnit)) {
|
||||
await leon.answer({ key: 'unit_not_supported' })
|
||||
return
|
||||
}
|
||||
|
||||
const { value: durationValue } = duration
|
||||
const seconds =
|
||||
normalizedUnit === 'hours'
|
||||
? Number(durationValue) * 3_600
|
||||
: normalizedUnit === 'minutes'
|
||||
? Number(durationValue) * 60
|
||||
: Number(durationValue)
|
||||
const interval = 1_000
|
||||
const timerWidget = new TimerWidget({
|
||||
params: {
|
||||
seconds,
|
||||
initialProgress: 0,
|
||||
interval
|
||||
},
|
||||
onFetch: {
|
||||
actionName: 'check_timer'
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
createTimerMemory(timerWidget.id, seconds, interval),
|
||||
leon.answer({
|
||||
widget: timerWidget,
|
||||
key: 'timer_set'
|
||||
})
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user