Begin migration to expo-router

This commit is contained in:
Niklas Korz 2024-08-22 14:29:57 +02:00
parent e373deb347
commit 0347ee6fb7
117 changed files with 8776 additions and 11935 deletions

97
app/api/fetcher.ts Normal file
View file

@ -0,0 +1,97 @@
import { pb } from "./pb";
import { ListsResponse, TasksResponse } from "./pocketbase-types";
export interface ListData {
id: string;
name: string;
parentId: string | null;
lists: Array<{
id: string;
name: string;
listCount: number;
taskCount: number;
}>;
tasks: Array<TasksResponse>;
}
export async function listFetcher(id: string): Promise<ListData> {
interface Expand {
lists_via_parent: unknown[];
tasks_via_list: unknown[];
}
type ListsResponseExpand = ListsResponse<Expand>;
if (id === "all") {
const lists = await pb
.collection<ListsResponseExpand>("lists")
.getList(0, 50, {
filter: pb.filter("parent = null"),
expand: "lists_via_parent,tasks_via_list",
});
const tasks = await pb.collection("tasks").getList(0, 50, {
filter: pb.filter("list = null"),
});
return {
id,
name: "Lists",
parentId: null,
lists: lists.items.map((l) => ({
id: l.id,
name: l.name,
listCount: l.expand?.["lists_via_parent"]?.length || 0,
taskCount: l.expand?.["tasks_via_list"]?.length || 0,
})),
tasks: tasks.items,
};
}
const list = await pb.collection("lists").getOne(id);
const lists = await pb
.collection<ListsResponseExpand>("lists")
.getList(0, 50, {
filter: pb.filter("parent = {:id}", { id }),
expand: "lists_via_parent,tasks_via_list",
});
const tasks = await pb.collection("tasks").getList(0, 50, {
filter: pb.filter("list = {:id}", { id }),
});
return {
id,
name: list.name,
parentId: list.parent,
lists: lists.items.map((l) => ({
id: l.id,
name: l.name,
listCount: l.expand?.["lists_via_parent"]?.length || 0,
taskCount: l.expand?.["tasks_via_list"]?.length || 0,
})),
tasks: tasks.items,
};
}
export interface TaskData extends TasksResponse {
listId: string;
imageSource: string;
}
export async function taskFetcher(id: string): Promise<TaskData> {
const task = await pb.collection("tasks").getOne(id);
const icon = await pb.collection("icons").getOne(task.icon);
const imageSource = pb.getFileUrl(icon, icon.image, {
thumb: "300x300",
});
return {
...task,
listId: task.list,
imageSource,
};
}
export interface RandomTaskData {
id: string;
name: string;
}
export async function randomTask(parent: string | null) {
return await pb.send<RandomTaskData>(`/api/extras/random/${parent}`, {});
}

5
app/api/pb.ts Normal file
View file

@ -0,0 +1,5 @@
import PocketBase from "pocketbase";
import type { TypedPocketBase } from "./pocketbase-types";
export const pb = new PocketBase("https://musclecat.pi.korz.tech") as TypedPocketBase;
//export const pb = new PocketBase("http://localhost:8090") as TypedPocketBase;

103
app/api/pocketbase-types.ts Normal file
View file

@ -0,0 +1,103 @@
/**
* This file was @generated using pocketbase-typegen
*/
import type PocketBase from 'pocketbase'
import type { RecordService } from 'pocketbase'
export enum Collections {
Completions = "completions",
Icons = "icons",
Lists = "lists",
Tasks = "tasks",
Users = "users",
}
// Alias types for improved usability
export type IsoDateString = string
export type RecordIdString = string
export type HTMLString = string
// System fields
export type BaseSystemFields<T = never> = {
id: RecordIdString
created: IsoDateString
updated: IsoDateString
collectionId: string
collectionName: Collections
expand?: T
}
export type AuthSystemFields<T = never> = {
email: string
emailVisibility: boolean
username: string
verified: boolean
} & BaseSystemFields<T>
// Record types for each collection
export type CompletionsRecord = {
task?: RecordIdString
user?: RecordIdString
}
export type IconsRecord = {
image?: string
name?: string
}
export type ListsRecord = {
name?: string
parent?: RecordIdString
}
export type TasksRecord = {
cooldown?: number
description?: HTMLString
icon?: RecordIdString
list: RecordIdString
name?: string
schedule?: IsoDateString
}
export type UsersRecord = {
avatar?: string
name?: string
}
// Response types include system fields and match responses from the PocketBase API
export type CompletionsResponse<Texpand = unknown> = Required<CompletionsRecord> & BaseSystemFields<Texpand>
export type IconsResponse<Texpand = unknown> = Required<IconsRecord> & BaseSystemFields<Texpand>
export type ListsResponse<Texpand = unknown> = Required<ListsRecord> & BaseSystemFields<Texpand>
export type TasksResponse<Texpand = unknown> = Required<TasksRecord> & BaseSystemFields<Texpand>
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand>
// Types containing all Records and Responses, useful for creating typing helper functions
export type CollectionRecords = {
completions: CompletionsRecord
icons: IconsRecord
lists: ListsRecord
tasks: TasksRecord
users: UsersRecord
}
export type CollectionResponses = {
completions: CompletionsResponse
icons: IconsResponse
lists: ListsResponse
tasks: TasksResponse
users: UsersResponse
}
// Type for usage with type asserted PocketBase instance
// https://github.com/pocketbase/js-sdk#specify-typescript-definitions
export type TypedPocketBase = PocketBase & {
collection(idOrName: 'completions'): RecordService<CompletionsResponse>
collection(idOrName: 'icons'): RecordService<IconsResponse>
collection(idOrName: 'lists'): RecordService<ListsResponse>
collection(idOrName: 'tasks'): RecordService<TasksResponse>
collection(idOrName: 'users'): RecordService<UsersResponse>
}