- stores/realtimeDebug.ts: neuer DEV-only Zustand-Store mit connection-state, reconnect-counter, token-expiry-countdown, channel-liste, rolling log-buffer (last 100 events). Hookt Phoenix-Socket open/close/reconnect + Channel-subscribe. - _layout.tsx: initRealtimeDebug() im __DEV__-Block beim App-Start. - debug.tsx: zwei neue Cards (RealtimeStatusCard + RealtimeLogCard) mit 1s-Tick-Refresh, Copy + Clear Buttons. Settings-Entry 'Realtime connection (DEV)'. - protectedDevices.ts: Array.isArray-Guard für apiFetch-Response — verhindert TypeError 'devices.filter is not a function' wenn Backend Non-Array zurückgibt. Diagnostik-Tool für Realtime-Disconnect-Bug bei lange eingeloggten Usern. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import { create } from 'zustand';
|
|
import { apiFetch } from '../lib/api';
|
|
|
|
export type ProtectedDeviceStatus = 'pending' | 'active' | 'revoked' | 'degraded';
|
|
|
|
export interface ProtectedDevice {
|
|
id: string;
|
|
platform: 'mac' | 'windows' | string;
|
|
label: string;
|
|
status: ProtectedDeviceStatus;
|
|
installedAt: string | null;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface EnrollResult {
|
|
deviceId: string;
|
|
downloadUrl: string;
|
|
}
|
|
|
|
type ProtectedDevicesState = {
|
|
devices: ProtectedDevice[];
|
|
loading: boolean;
|
|
enrolling: boolean;
|
|
|
|
load: () => Promise<void>;
|
|
enroll: (label: string, platform: 'mac' | 'windows') => Promise<EnrollResult>;
|
|
confirmInstalled: (id: string) => Promise<void>;
|
|
remove: (id: string) => Promise<{ manualRemovalRequired: boolean }>;
|
|
};
|
|
|
|
export const useProtectedDevicesStore = create<ProtectedDevicesState>((set, get) => ({
|
|
devices: [],
|
|
loading: false,
|
|
enrolling: false,
|
|
|
|
load: async () => {
|
|
set({ loading: true });
|
|
try {
|
|
const res = await apiFetch<ProtectedDevice[]>('/api/devices/protected');
|
|
set({ devices: Array.isArray(res) ? res : [] });
|
|
} catch {
|
|
// endpoint might not be ready yet — keep empty state, screen handles it
|
|
} finally {
|
|
set({ loading: false });
|
|
}
|
|
},
|
|
|
|
enroll: async (label: string, platform: 'mac' | 'windows') => {
|
|
set({ enrolling: true });
|
|
try {
|
|
const result = await apiFetch<EnrollResult>('/api/devices/enroll', {
|
|
method: 'POST',
|
|
body: { platform, label },
|
|
});
|
|
await get().load();
|
|
return result;
|
|
} finally {
|
|
set({ enrolling: false });
|
|
}
|
|
},
|
|
|
|
confirmInstalled: async (id: string) => {
|
|
await apiFetch(`/api/devices/${id}/confirm-installed`, { method: 'POST' });
|
|
set((s) => ({
|
|
devices: s.devices.map((d) =>
|
|
d.id === id ? { ...d, status: 'active' as const, installedAt: new Date().toISOString() } : d
|
|
),
|
|
}));
|
|
},
|
|
|
|
remove: async (id: string) => {
|
|
const res = await apiFetch<{ manualRemovalRequired: boolean }>(`/api/devices/${id}/revoke`, {
|
|
method: 'DELETE',
|
|
});
|
|
set((s) => ({ devices: s.devices.filter((d) => d.id !== id) }));
|
|
return res;
|
|
},
|
|
}));
|