Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(dashboard): add status types, api methods, and the contact-statuses hook
Yudhi Armyndharis4554323106e6
diff --git a/dashboard/src/hooks/useContactStatuses.ts b/dashboard/src/hooks/useContactStatuses.tsnew file mode 100644index 000000000..b5a64e067--- /dev/null+++ b/dashboard/src/hooks/useContactStatuses.ts@@ -0,0 +1,10 @@+import { useQuery, type UseQueryResult } from '@tanstack/react-query';+import { sessionApi, type StatusUpdate } from '../services/api';++export function useContactStatuses(sessionId: string | null): UseQueryResult<StatusUpdate[], Error> {+ return useQuery<StatusUpdate[], Error>({+ queryKey: ['contact-statuses', sessionId],+ queryFn: async () => (await sessionApi.getContactStatuses(sessionId!)).statuses,+ enabled: Boolean(sessionId),+ });+}diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.tsindex a32182209..d8403fce6 100644--- a/dashboard/src/services/api.ts+++ b/dashboard/src/services/api.ts@@ -225,6 +225,34 @@ export interface ChannelMessage {mediaUrl?: string;}+// Mirrors the backend engine Status / IWhatsAppEngine status methods (GET /sessions/:id/status).+export interface StatusUpdate {+ id: string;+ contact: { id: string; name?: string; pushName?: string };+ type: 'text' | 'image' | 'video';+ caption?: string;+ mediaUrl?: string;+ backgroundColor?: string;+ font?: number;+ timestamp: string;+ expiresAt: string;+}++export interface ContactStatusGroup {+ contact: { id: string; name?: string; pushName?: string };+ items: StatusUpdate[];+ latest: string;+}++// Minimal contact type for the recipient picker; the backend GET /sessions/:id/contacts+// returns a Contact array; fields beyond id are optional.+export interface Contact {+ id: string;+ name?: string;+ pushName?: string;+ number?: string;+}+export interface SendMediaPayload {base64?: string;url?: string;@@ -556,6 +584,39 @@ async function requestText(endpoint: string): Promise<string> {return response.text();}+/** Like {@link request} but returns a Blob — e.g. for status media downloads. */+async function requestBlob(endpoint: string): Promise<Blob> {+ const url = `${API_BASE_URL}${endpoint}`;++ // Get API key from sessionStorage for authentication+ const apiKey = sessionStorage.getItem('openwa_api_key');++ const headers: HeadersInit = {+ ...(apiKey ? { 'X-API-Key': apiKey } : {}),+ };++ const response = await fetch(url, { headers });++ if (response.status === 401) {+ // The stored API key is invalid/expired/revoked — clear it and return to login+ sessionStorage.removeItem('openwa_api_key');+ if (typeof window !== 'undefined') {+ window.location.assign('/');+ // Halt this request's promise chain so callers neither throw nor receive an undefined payload.+ return new Promise<Blob>(() => {});+ }+ }++ if (!response.ok) {+ const error = await response.json().catch(() => ({}));+ const err = new Error(error.message || `HTTP ${response.status}`) as Error & { status?: number };+ err.status = response.status;+ throw err;+ }++ return response.blob();+}+// =============================================================================// Session API// =============================================================================@@ -609,6 +670,24 @@ export const sessionApi = {getSubscribedChannels: (id: string) => request<Channel[]>(`/sessions/${id}/channels`),getChannelMessages: (id: string, channelId: string, limit = 50) =>request<ChannelMessage[]>(`/sessions/${id}/channels/${encodeURIComponent(channelId)}/messages?limit=${limit}`),+ getContactStatuses: (id: string) => request<{ statuses: StatusUpdate[] }>(`/sessions/${id}/status`),+ getStatusMediaBlob: (id: string, statusId: string) =>+ requestBlob(`/sessions/${id}/status/${encodeURIComponent(statusId)}/media`),+ postTextStatus: (id: string, text: string, recipients: string[], extra?: { backgroundColor?: string; font?: number }) =>+ request(`/sessions/${id}/status/send-text`, {+ method: 'POST',+ body: JSON.stringify({ text, recipients, ...extra }),+ }),+ postImageStatus: (+ id: string,+ image: { url?: string; base64?: string; mimetype?: string },+ recipients: string[],+ caption?: string,+ ) =>+ request(`/sessions/${id}/status/send-image`, {+ method: 'POST',+ body: JSON.stringify({ image, recipients, caption }),+ }),};// =============================================================================@@ -675,6 +754,7 @@ export interface ProfilePictureResponse {}export const contactApi = {+ list: (sessionId: string) => request<Contact[]>(`/sessions/${sessionId}/contacts`),checkNumber: (sessionId: string, number: string) =>request<CheckNumberResponse>(`/sessions/${sessionId}/contacts/check/${encodeURIComponent(number)}`),// Returns the contact/group profile picture URL. Both engines return null when the user hid their