Files
J621/frontend/src/store/e621.ts
T
JakeBreath cad659b75f Add a blacklist editor that saves to e621
The Online sidebar's "Your blacklist" section is editable now: typing a
tag appends it and each entry gets an x to remove it. Changes are written
straight to the e621 account (PATCH /users/{id}.json with
user[blacklisted_tags]); the store keeps the fetched profile for the id,
updates the list optimistically and reloads it from e621. The Followed
page's blacklist cloud is rebuilt afterwards via the new
/api/follows/cloud/?refresh=1 force flag.

Verified against the live API with a reversible add/verify/restore test.
2026-09-17 20:35:23 -05:00

86 lines
2.2 KiB
TypeScript

import { create } from "zustand";
import { api } from "@/lib/api";
import {
effectiveCredentials,
updateBlacklist,
verifyE621Credentials,
type E621Credentials,
type E621User,
} from "@/lib/e621";
interface E621State {
credentials: E621Credentials | null;
profile: E621User | null;
blacklist: string[];
loading: boolean;
ready: boolean;
load: () => Promise<void>;
loadBlacklist: () => Promise<void>;
saveBlacklist: (lines: string[]) => Promise<void>;
save: (values: {
username: string;
api_key: string;
base_url: string;
}) => Promise<E621Credentials>;
clear: () => void;
}
export const useE621 = create<E621State>((set, get) => ({
credentials: null,
profile: null,
blacklist: [],
loading: false,
ready: false,
load: async () => {
set({ loading: true });
try {
const credentials = await api<E621Credentials>("/api/auth/e621/");
set({ credentials, ready: true, loading: false });
void get().loadBlacklist();
} catch {
set({ credentials: null, ready: true, loading: false });
}
},
loadBlacklist: async () => {
try {
const user = await verifyE621Credentials(
effectiveCredentials(get().credentials),
);
const tags = (user.blacklisted_tags ?? "")
.split("\n")
.map((tag) => tag.trim())
.filter(Boolean);
set({ blacklist: tags, profile: user });
} catch {
// Keep the last known blacklist when e621 is unreachable.
}
},
saveBlacklist: async (lines) => {
const credentials = get().credentials;
const profile = get().profile;
if (!credentials?.configured || !profile) {
throw new Error("Add e621 credentials in Account to edit your blacklist.");
}
await updateBlacklist(credentials, profile.id, lines.join("\n"));
set({ blacklist: lines });
await get().loadBlacklist();
},
save: async (values) => {
const credentials = await api<E621Credentials>("/api/auth/e621/", {
method: "PUT",
json: values,
});
set({ credentials });
void get().loadBlacklist();
return credentials;
},
clear: () =>
set({ credentials: null, profile: null, blacklist: [], ready: false }),
}));