import { apiErrorMessage } from '~/utils/errors' import type { RoomListResponse, RoomSummary } from '~/types' export function useRooms() { const api = useApi() const rooms = useState('rooms', () => []) const loading = useState('rooms-loading', () => false) const error = useState('rooms-error', () => null) const creating = useState('room-dialog-open', () => false) async function refresh() { loading.value = true error.value = null try { const response = await api.get('/rooms') rooms.value = response.rooms } catch (err) { error.value = apiErrorMessage(err) } finally { loading.value = false } } function openCreate() { creating.value = true } function closeCreate() { creating.value = false } function touch(room: RoomSummary) { rooms.value = [room, ...rooms.value.filter((item) => item.id !== room.id)] } return { rooms, loading, error, creating, refresh, openCreate, closeCreate, touch } }