chahinebrini edf047eacf feat(native): device-lock i18n keys + devices store type extensions
- Add auth.device_locked_* keys (DE/EN/FR): headline, body, countdown,
  email_hint, use_original CTA, back link
- Add devices.bound_badge + devices.release_* keys (DE/EN/FR) for the
  bound-device / release-flow in the Devices page
- Extend UserDevice interface with boundToPlan and releaseRequestedAt
- Add requestRelease + cancelRelease store actions calling the new
  POST /api/devices/:id/request-release|cancel-release endpoints

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 00:37:12 +02:00

94 lines
2.5 KiB
TypeScript

import { create } from 'zustand';
import { apiFetch } from '../lib/api';
import { getDeviceInfo } from '../lib/deviceId';
export interface UserDevice {
id: string;
deviceId: string;
platform: string;
model: string | null;
name: string | null;
lastSeenAt: string;
createdAt: string;
isCurrent?: boolean;
boundToPlan: 'free' | 'pro' | 'legend' | 'standard' | 'premium' | null;
releaseRequestedAt: string | null;
}
type DevicesState = {
devices: UserDevice[];
maxDevices: number;
plan: string;
loading: boolean;
ensureRegistered: () => Promise<void>;
loadDevices: () => Promise<void>;
removeDevice: (id: string) => Promise<void>;
requestRelease: (id: string) => Promise<void>;
cancelRelease: (id: string) => Promise<void>;
};
export const useDevicesStore = create<DevicesState>((set) => ({
devices: [],
maxDevices: 1,
plan: 'free',
loading: false,
ensureRegistered: async () => {
const info = await getDeviceInfo().catch(() => null);
if (!info) return;
await apiFetch('/api/devices/register', {
method: 'POST',
skipDeviceHeader: true,
body: {
deviceId: info.deviceId,
platform: info.platform,
name: info.name,
model: info.model,
osVersion: info.osVersion,
appVersion: info.appVersion,
},
}).then((res: any) => {
set({ maxDevices: res.max ?? 1 });
}).catch(() => {});
},
loadDevices: async () => {
set({ loading: true });
try {
await useDevicesStore.getState().ensureRegistered();
const res = await apiFetch<{ devices: UserDevice[]; max: number; plan: string }>(
'/api/devices'
);
set({ devices: res.devices, maxDevices: res.max, plan: res.plan });
} finally {
set({ loading: false });
}
},
removeDevice: async (id: string) => {
await apiFetch(`/api/devices/${id}`, { method: 'DELETE' });
set((s) => ({ devices: s.devices.filter((d) => d.id !== id) }));
},
requestRelease: async (id: string) => {
await apiFetch(`/api/devices/${id}/request-release`, { method: 'POST' });
const now = new Date().toISOString();
set((s) => ({
devices: s.devices.map((d) =>
d.id === id ? { ...d, releaseRequestedAt: now } : d
),
}));
},
cancelRelease: async (id: string) => {
await apiFetch(`/api/devices/${id}/cancel-release`, { method: 'POST' });
set((s) => ({
devices: s.devices.map((d) =>
d.id === id ? { ...d, releaseRequestedAt: null } : d
),
}));
},
}));