Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 194x 1x 60x 1x 125x 125x 125x 1x 1x 1x 1x 1x 1x 1x 104x 104x 104x 104x 104x 104x 104x 17x 17x 4x 4x 4x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 6x 6x 6x 6x 104x 104x 104x 104x 104x 104x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 2x 2x 2x 2x 104x 104x 104x 104x 104x 31x 31x 31x 104x 15x 15x 15x 104x 37x 37x 37x 104x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* useAction — primary hook for all action types.
* Abstracts page actions, query actions, and mutation actions behind one API.
*/
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query';
import {useCallback, useMemo} from 'react';
import {useBlong} from '../context/BlongContext.js';
import {ulid} from '../lib/ulid.js';
import {useAppStore} from '../state/appStore.js';
import type {
IBlongError,
IMutationAction,
IPageAction,
IQueryAction,
ITypedAction,
IUseActionResult,
} from '../types/action.js';
import type {ITab} from '../types/portal.js';
function isComponentAction(a?: ITypedAction): a is IPageAction {
return !a || 'component' in a;
}
function isMutationAction(a: ITypedAction): a is IMutationAction {
return a && 'mutates' in a && a.mutates === true;
}
function isQueryAction(a: ITypedAction): a is IQueryAction {
return a && 'method' in a && !('mutates' in a);
}
/**
* useAction — invoke or subscribe to an action by name.
*
* @param actionName - Registered action name (e.g. 'model.coral.fetch')
* @param params - Static params merged with action-level params
*/
export function useAction<TResult = unknown>(
actionName: string,
type?: 'page' | 'query' | 'mutation',
params?: Record<string, unknown>,
): IUseActionResult<TResult> {
const {handler} = useBlong();
const queryClient = useQueryClient();
const actions = useAppStore(s => s.actions);
const openTab = useAppStore(s => s.openTab);
const showError = useAppStore(s => s.showError);
const action = useMemo(
() =>
actions[actionName] ??
(actionName &&
!actionName.startsWith('component') &&
({
method: actionName,
type: type || 'query',
...(type === 'mutation' && {mutates: true}),
} as ITypedAction)),
[actionName, actions, type],
);
// Resolve method name for query/mutation actions
const method = !isComponentAction(action)
? (action.method ?? (action as IQueryAction).handler ?? actionName)
: actionName;
// Merge params: action-level static params then call-time params
const mergedParams = useCallback(
(callParams?: Record<string, unknown>) => {
const actionParams =
!isComponentAction(action) && action.params
? typeof action.params === 'function'
? action.params(callParams ?? {})
: action.params
: {};
return {
...(params ?? {}),
...(actionParams as Record<string, unknown>),
...(callParams ?? {}),
};
},
[action, params],
);
// ── Page / open action ─────────────────────────────────────────────────
// Always declared unconditionally — used when action is absent or page-type.
const open = useCallback(
async (callParams?: Record<string, unknown>) => {
const resolved = mergedParams(callParams);
const componentAction: ITypedAction | undefined =
action || (await handler[actionName](resolved, {}));
if (componentAction && isComponentAction(componentAction)) {
try {
const component = await componentAction.component(resolved);
const tab: ITab = {
id: ulid(),
actionName,
params: resolved,
title: componentAction.title,
component,
};
openTab(tab);
} catch (err) {
showError({type: 'error.component.load', message: String(err)});
}
} else {
showError({
type: 'error.action.notfound',
message: `Action "${actionName}" not found`,
});
}
},
[action, actionName, mergedParams, openTab, showError, handler],
);
// ── Query action ─────────────────────────────────────────────────────
// Always declared unconditionally; enabled only when action is a query action.
const {
data,
isLoading,
error: queryError,
refetch,
} = useQuery<TResult, IBlongError>({
queryKey: [method, params ?? {}],
queryFn: () => handler[method](mergedParams(), {}) as Promise<TResult>,
enabled: !!action && isQueryAction(action),
staleTime: 0,
});
const queryCall = useCallback(
(callParams?: Record<string, unknown>) =>
handler[method](mergedParams(callParams), {}) as Promise<TResult>,
[method, mergedParams, handler],
);
// ── Mutation action ────────────────────────────────────────────────────
// Always declared unconditionally; only triggered when mutateAsync is called.
const {
mutateAsync,
isPending,
error: mutationError,
} = useMutation<TResult, IBlongError, Record<string, unknown>>({
mutationFn: callParams => handler[method](mergedParams(callParams), {}) as Promise<TResult>,
onSuccess: () => {
if (isMutationAction(action)) {
const invalidates = (action as IMutationAction).invalidates ?? [];
for (const name of invalidates) {
const invalidAction = actions[name];
const invalidMethod =
invalidAction && !isComponentAction(invalidAction)
? (invalidAction.method ??
(invalidAction as IQueryAction).handler ??
name)
: name;
void queryClient.invalidateQueries({queryKey: [invalidMethod]});
}
}
},
});
const mutationCall = useCallback(
(callParams?: Record<string, unknown>) => mutateAsync(callParams ?? {}),
[mutateAsync],
);
// ── Return based on action type ────────────────────────────────────────
if (actionName && isComponentAction(action)) {
return {call: open, open, loading: false};
}
if (isQueryAction(action)) {
return {
call: queryCall,
open: queryCall as IUseActionResult<TResult>['open'],
data,
loading: isLoading,
error: queryError ?? undefined,
refetch: () => refetch().then(r => r.data!),
};
}
if (isMutationAction(action)) {
return {
call: mutationCall,
open: mutationCall as IUseActionResult<TResult>['open'],
loading: isPending,
error: mutationError ?? undefined,
};
}
// Fallback
return {call: async () => undefined, open: () => undefined, loading: false};
}
|