Skip to content

Commit

Permalink
fix theme starring
Browse files Browse the repository at this point in the history
  • Loading branch information
beebls committed Feb 1, 2025
1 parent bb2bb9c commit aabf5c5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/backend-impl/decky-backend-repository-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class DeckyBackendRepository implements IBackendRepository {
);
}
}
async fetch<Return>(url: string, request: RequestInit, mode: "json" | "text" = "json") {
async fetch<Return>(url: string, request: RequestInit, mode: "json" | "text" | "void" = "json") {
try {
console.debug("CSSLOADER FETCH", url, request);
const res = await fetchNoCors(url, request);
if (!res.ok) {
throw new Error(`Res Not Okay - Code ${res.status}`);
}
// TODO: Potentially handle void mode better
if (mode === "void") return undefined as Return;
if (mode === "text") {
return res.text() as Return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/backend/repositories/backend-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export interface IBackendRepository {
call: <Args extends any[] = [], Return = void>(methodName: string, args: Args) => Promise<Return>;
toast: (title: string, body?: string) => void;
fetch: <Return>(url: string, request: RequestInit, mode?: "json" | "text") => Promise<Return>;
fetch: <Return>(
url: string,
request: RequestInit,
mode?: "json" | "text" | "void"
) => Promise<Return>;
}
6 changes: 5 additions & 1 deletion src/backend/services/backend-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export class Backend {
async deleteTheme(themeName: string) {
return await Backend.repository.call<[string], void>("delete_theme", [themeName]);
}
async fetch<Return>(url: string, request: RequestInit = {}, mode: "json" | "text" = "json") {
async fetch<Return>(
url: string,
request: RequestInit = {},
mode: "json" | "text" | "void" = "json"
) {
return Backend.repository.fetch<Return>(url, request, mode);
}

Expand Down
52 changes: 33 additions & 19 deletions src/backend/state/theme-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ export interface CSSLoaderStateActions {
reloadPlugin: () => Promise<void>;
reloadThemes: () => Promise<void>;
refreshToken: () => Promise<string | undefined>;
apiFetch: <Return>(
url: string,
request?: RequestInit,
requiresAuth?: boolean | string
) => Promise<Return>;
apiFetch: <Return>(url: string, request?: RequestInit, options?: FetchOptions) => Promise<Return>;
logInWithShortToken: (newToken?: string) => Promise<void>;
logOut: () => void;
getThemes: () => Promise<void>;
Expand Down Expand Up @@ -87,30 +83,39 @@ export interface CSSLoaderStateActions {
setWatchState: (state: boolean) => Promise<void>;
}

export interface FetchOptions {
requiresAuth?: boolean;
customAuthToken?: string;
responseMode?: "json" | "text" | "void";
}

export interface ICSSLoaderState extends CSSLoaderStateValues, CSSLoaderStateActions {}

export const createCSSLoaderStore = (backend: Backend) =>
createStore<ICSSLoaderState>((set, get) => {
async function apiFetch<Return>(
fetchPath: string,
request?: RequestInit,
// Can be a boolean (to automatically fetch token), or a string (to use a custom token)
requiresAuth?: boolean | string
options?: FetchOptions
) {
try {
const { refreshToken } = get();
let authToken = undefined;
if (requiresAuth) {
authToken = typeof requiresAuth === "string" ? requiresAuth : await refreshToken();
if (options?.requiresAuth) {
authToken = options?.customAuthToken ?? (await refreshToken());
}
return await backend.fetch<Return>(`${apiUrl}${fetchPath}`, {
method: "GET",
...request,
headers: {
...(request?.headers || {}),
Authorization: `Bearer ${authToken}`,
return await backend.fetch<Return>(
`${apiUrl}${fetchPath}`,
{
method: "GET",
...request,
headers: {
...(request?.headers || {}),
Authorization: `Bearer ${authToken}`,
},
},
});
options?.responseMode ?? "json"
);
} catch (error) {
if (error instanceof FetchError) {
throw error;
Expand All @@ -121,7 +126,13 @@ export const createCSSLoaderStore = (backend: Backend) =>

async function getPatrons() {
try {
const data = await backend.fetch<string>(`${apiUrl}/patrons`, {}, "text");
const data = await apiFetch<string>(
`${apiUrl}/patrons`,
{},
{
responseMode: "text",
}
);
if (data) {
return data.split("\n");
}
Expand Down Expand Up @@ -157,7 +168,8 @@ export const createCSSLoaderStore = (backend: Backend) =>
hiddenMotdId: "",
serverState: false,
watchState: false,
translationsBranch: "-1",
// Not entirely sure why but unless I manually type this it errors
translationsBranch: "-1" as "-1",
patrons: [],

initializeStore: async () => {
Expand Down Expand Up @@ -282,7 +294,9 @@ export const createCSSLoaderStore = (backend: Backend) =>
apiFullToken: json.token,
apiTokenExpireDate: new Date().valueOf() + 1000 * 10 * 60,
});
const meJson = await apiFetch<FullAccountData>("/auth/me", undefined, true);
const meJson = await apiFetch<FullAccountData>("/auth/me", undefined, {
requiresAuth: true,
});
if (meJson) {
set({ apiMeData: meJson });
}
Expand Down
7 changes: 5 additions & 2 deletions src/modules/expanded-view/context/ExpandedViewStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const expandedViewStore = createStore<IExpandedViewStore>((set, get) => {
const starResponse = await apiFetch<{ starred: boolean }>(
`/users/me/stars/${themeId}`,
{},
true
{ requiresAuth: true }
);
if (!starResponse) {
// Silently error
Expand All @@ -110,7 +110,10 @@ const expandedViewStore = createStore<IExpandedViewStore>((set, get) => {
{
method: isStarred ? "DELETE" : "POST",
},
true
{
requiresAuth: true,
responseMode: "void",
}
);
const newIsStarred = !isStarred;
set({
Expand Down
8 changes: 6 additions & 2 deletions src/modules/theme-store/context/ThemeBrowserStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ export function ThemeBrowserStoreProvider({
const response = await apiFetch<FilterQueryResponse>(
`${filterPath}?type=${typeMapping[themeType]}`,
{},
requiresAuth
{
requiresAuth,
}
);
if (response.filters) {
set({ filterOptions: response });
Expand Down Expand Up @@ -147,7 +149,9 @@ export function ThemeBrowserStoreProvider({
const response = await apiFetch<ThemeQueryResponse>(
`${themePath}?${generateParamStr(searchOpts, themeType)}`,
{},
requiresAuth
{
requiresAuth,
}
);
if (response.items) {
set({ themeTotal: response.total });
Expand Down

0 comments on commit aabf5c5

Please sign in to comment.