mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 22:24:02 +00:00
miniapp: refresh after mutations, activity expand, squash chart palette
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
import { createContext, createMemo, createSignal, useContext, type ParentProps } from 'solid-js'
|
||||
import {
|
||||
createContext,
|
||||
createMemo,
|
||||
createSignal,
|
||||
onCleanup,
|
||||
useContext,
|
||||
type ParentProps
|
||||
} from 'solid-js'
|
||||
|
||||
import { majorStringToMinor, minorToMajorString } from '../lib/money'
|
||||
import {
|
||||
@@ -51,7 +58,14 @@ export type CycleFormState = {
|
||||
utilityAmountMajor: string
|
||||
}
|
||||
|
||||
const chartPalette = ['#3ecf8e', '#6fd3c0', '#94a8ff', '#f06a8d', '#f3d36f', '#7dc96d'] as const
|
||||
const chartPalette = [
|
||||
'var(--chart-1)',
|
||||
'var(--chart-2)',
|
||||
'var(--chart-3)',
|
||||
'var(--chart-4)',
|
||||
'var(--chart-5)',
|
||||
'var(--chart-6)'
|
||||
] as const
|
||||
|
||||
type DashboardContextValue = {
|
||||
dashboard: () => MiniAppDashboard | null
|
||||
@@ -224,7 +238,7 @@ function computePurchaseInvestmentChart(
|
||||
/* ── Provider ───────────────────────────────────────── */
|
||||
|
||||
export function DashboardProvider(props: ParentProps) {
|
||||
const { readySession } = useSession()
|
||||
const { readySession, registerRefreshListener } = useSession()
|
||||
const { copy } = useI18n()
|
||||
|
||||
const [dashboard, setDashboard] = createSignal<MiniAppDashboard | null>(null)
|
||||
@@ -275,6 +289,9 @@ export function DashboardProvider(props: ParentProps) {
|
||||
computePurchaseInvestmentChart(dashboard(), purchaseLedger(), copy().ledgerActorFallback)
|
||||
)
|
||||
|
||||
const unregisterDashboardRefreshListener = registerRefreshListener(loadDashboardData)
|
||||
onCleanup(unregisterDashboardRefreshListener)
|
||||
|
||||
async function loadDashboardData(initData: string, isAdmin: boolean) {
|
||||
// In demo mode, use demo data
|
||||
if (!initData) {
|
||||
|
||||
@@ -62,6 +62,9 @@ type SessionContextValue = {
|
||||
handleMemberLocaleChange: (nextLocale: Locale) => Promise<void>
|
||||
handleHouseholdLocaleChange: (nextLocale: Locale) => Promise<void>
|
||||
refreshHouseholdData: (includeAdmin?: boolean, forceRefresh?: boolean) => Promise<void>
|
||||
registerRefreshListener: (
|
||||
listener: (initData: string, isAdmin: boolean) => Promise<void>
|
||||
) => () => void
|
||||
}
|
||||
|
||||
const SessionContext = createContext<SessionContextValue>()
|
||||
@@ -114,6 +117,17 @@ export function SessionProvider(
|
||||
const [displayNameDraft, setDisplayNameDraft] = createSignal('')
|
||||
const [savingOwnDisplayName, setSavingOwnDisplayName] = createSignal(false)
|
||||
|
||||
const refreshListeners = new Set<(initData: string, isAdmin: boolean) => Promise<void>>()
|
||||
|
||||
function registerRefreshListener(
|
||||
listener: (initData: string, isAdmin: boolean) => Promise<void>
|
||||
) {
|
||||
refreshListeners.add(listener)
|
||||
return () => {
|
||||
refreshListeners.delete(listener)
|
||||
}
|
||||
}
|
||||
|
||||
const readySession = () => {
|
||||
const current = session()
|
||||
return current.status === 'ready' ? current : null
|
||||
@@ -310,7 +324,11 @@ export function SessionProvider(
|
||||
// Delegate actual data loading to dashboard context via onReady
|
||||
const current = readySession()
|
||||
if (current) {
|
||||
await props.onReady?.(data, includeAdmin || current.member.isAdmin)
|
||||
const isAdmin = includeAdmin || current.member.isAdmin
|
||||
await Promise.all([
|
||||
props.onReady?.(data, isAdmin),
|
||||
...Array.from(refreshListeners).map((l) => l(data, isAdmin))
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,7 +354,8 @@ export function SessionProvider(
|
||||
handleSaveOwnDisplayName,
|
||||
handleMemberLocaleChange,
|
||||
handleHouseholdLocaleChange,
|
||||
refreshHouseholdData
|
||||
refreshHouseholdData,
|
||||
registerRefreshListener
|
||||
}}
|
||||
>
|
||||
{props.children}
|
||||
|
||||
Reference in New Issue
Block a user