feat: multi-workspace support + channels grouped by workspace and network
Release APK / build (push) Has been cancelled
Release APK / build (push) Has been cancelled
- PostizContext: new PostizWorkspace type, multi-workspace storage (postiz_workspaces_v2), auto-migration from legacy single config, addWorkspace / updateWorkspace / removeWorkspace, clients map - Settings: full rewrite with workspace card list (add / edit / delete) - Compose: channels displayed in two levels — workspace section then network type (X/Twitter, Instagram, LinkedIn...) within each workspace; submit routes posts and image uploads per workspace - MediaLibraryModal: workspace tabs when multiple workspaces configured, returned items carry their workspaceId Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,14 +5,22 @@ import React, {
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
|
||||
const API_KEY_STORAGE = "postiz_api_key";
|
||||
const BASE_URL_STORAGE = "postiz_base_url";
|
||||
const WORKSPACES_KEY = "postiz_workspaces_v2";
|
||||
const LEGACY_API_KEY = "postiz_api_key";
|
||||
const LEGACY_BASE_URL = "postiz_base_url";
|
||||
|
||||
export const DEFAULT_BASE_URL = "https://postiz.gyozamancave.fr/api/public/v1";
|
||||
|
||||
export interface PostizWorkspace {
|
||||
id: string;
|
||||
name: string;
|
||||
apiKey: string;
|
||||
baseUrl: string;
|
||||
}
|
||||
|
||||
export interface PostizIntegration {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -45,129 +53,129 @@ export interface PostizUploadResult {
|
||||
}
|
||||
|
||||
interface PostizContextValue {
|
||||
apiKey: string;
|
||||
baseUrl: string;
|
||||
workspaces: PostizWorkspace[];
|
||||
isConfigured: boolean;
|
||||
isLoading: boolean;
|
||||
unauthorized: boolean;
|
||||
clearUnauthorized: () => void;
|
||||
clients: Record<string, AxiosInstance>;
|
||||
addWorkspace: (ws: Omit<PostizWorkspace, "id">) => Promise<void>;
|
||||
updateWorkspace: (ws: PostizWorkspace) => Promise<void>;
|
||||
removeWorkspace: (id: string) => Promise<void>;
|
||||
// backward compat for posts.tsx (first workspace)
|
||||
client: AxiosInstance | null;
|
||||
saveSettings: (apiKey: string, baseUrl: string) => Promise<void>;
|
||||
clearSettings: () => Promise<void>;
|
||||
apiKey: string;
|
||||
baseUrl: string;
|
||||
}
|
||||
|
||||
const PostizContext = createContext<PostizContextValue>({
|
||||
apiKey: "",
|
||||
baseUrl: DEFAULT_BASE_URL,
|
||||
workspaces: [],
|
||||
isConfigured: false,
|
||||
isLoading: true,
|
||||
unauthorized: false,
|
||||
clearUnauthorized: () => {},
|
||||
clients: {},
|
||||
addWorkspace: async () => {},
|
||||
updateWorkspace: async () => {},
|
||||
removeWorkspace: async () => {},
|
||||
client: null,
|
||||
saveSettings: async () => {},
|
||||
clearSettings: async () => {},
|
||||
apiKey: "",
|
||||
baseUrl: DEFAULT_BASE_URL,
|
||||
});
|
||||
|
||||
function createClient(
|
||||
apiKey: string,
|
||||
baseUrl: string,
|
||||
onUnauthorized?: () => void
|
||||
): AxiosInstance {
|
||||
const normalizedUrl = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
|
||||
function makeClient(ws: PostizWorkspace): AxiosInstance {
|
||||
const baseURL = ws.baseUrl.endsWith("/") ? ws.baseUrl : ws.baseUrl + "/";
|
||||
const instance = axios.create({
|
||||
baseURL: normalizedUrl,
|
||||
headers: {
|
||||
Authorization: apiKey,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
baseURL,
|
||||
headers: { Authorization: ws.apiKey, "Content-Type": "application/json" },
|
||||
timeout: 15000,
|
||||
});
|
||||
instance.interceptors.request.use((config) => {
|
||||
console.log(">>> REQUEST:", config.method?.toUpperCase(), (config.baseURL || "") + (config.url || ""));
|
||||
console.log(">>> REQUEST:", config.method?.toUpperCase(), (config.baseURL ?? "") + (config.url ?? ""));
|
||||
return config;
|
||||
});
|
||||
instance.interceptors.response.use(
|
||||
(res) => res,
|
||||
(err) => {
|
||||
if (axios.isAxiosError(err) && err.response?.status === 401) {
|
||||
onUnauthorized?.();
|
||||
}
|
||||
return Promise.reject(err);
|
||||
}
|
||||
);
|
||||
return instance;
|
||||
}
|
||||
|
||||
function buildClients(list: PostizWorkspace[]): Record<string, AxiosInstance> {
|
||||
return Object.fromEntries(list.map((ws) => [ws.id, makeClient(ws)]));
|
||||
}
|
||||
|
||||
export function PostizProvider({ children }: { children: React.ReactNode }) {
|
||||
const [apiKey, setApiKey] = useState("");
|
||||
const [baseUrl, setBaseUrl] = useState(DEFAULT_BASE_URL);
|
||||
const [workspaces, setWorkspaces] = useState<PostizWorkspace[]>([]);
|
||||
const [clients, setClients] = useState<Record<string, AxiosInstance>>({});
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [client, setClient] = useState<AxiosInstance | null>(null);
|
||||
const [unauthorized, setUnauthorized] = useState(false);
|
||||
const unauthorizedFiredRef = useRef(false);
|
||||
|
||||
const handleUnauthorized = useCallback(() => {
|
||||
if (unauthorizedFiredRef.current) return;
|
||||
unauthorizedFiredRef.current = true;
|
||||
setUnauthorized(true);
|
||||
}, []);
|
||||
|
||||
const clearUnauthorized = useCallback(() => {
|
||||
unauthorizedFiredRef.current = false;
|
||||
setUnauthorized(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
try {
|
||||
const storedKey = await SecureStore.getItemAsync(API_KEY_STORAGE);
|
||||
const storedUrl = await SecureStore.getItemAsync(BASE_URL_STORAGE);
|
||||
if (storedKey) {
|
||||
const url = (storedUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
|
||||
setApiKey(storedKey);
|
||||
setBaseUrl(url);
|
||||
setClient(() => createClient(storedKey, url, handleUnauthorized));
|
||||
const stored = await SecureStore.getItemAsync(WORKSPACES_KEY);
|
||||
if (stored) {
|
||||
const list: PostizWorkspace[] = JSON.parse(stored);
|
||||
setWorkspaces(list);
|
||||
setClients(buildClients(list));
|
||||
} else {
|
||||
// Migrate legacy single-workspace config
|
||||
const legacyKey = await SecureStore.getItemAsync(LEGACY_API_KEY);
|
||||
const legacyUrl = await SecureStore.getItemAsync(LEGACY_BASE_URL);
|
||||
if (legacyKey) {
|
||||
const migrated: PostizWorkspace = {
|
||||
id: Date.now().toString(),
|
||||
name: "Default",
|
||||
apiKey: legacyKey,
|
||||
baseUrl: (legacyUrl || DEFAULT_BASE_URL).replace(/\/$/, ""),
|
||||
};
|
||||
const list = [migrated];
|
||||
await SecureStore.setItemAsync(WORKSPACES_KEY, JSON.stringify(list));
|
||||
setWorkspaces(list);
|
||||
setClients(buildClients(list));
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
})();
|
||||
}, [handleUnauthorized]);
|
||||
}, []);
|
||||
|
||||
const saveSettings = useCallback(
|
||||
async (newApiKey: string, newBaseUrl: string) => {
|
||||
await SecureStore.setItemAsync(API_KEY_STORAGE, newApiKey);
|
||||
await SecureStore.setItemAsync(BASE_URL_STORAGE, newBaseUrl);
|
||||
setApiKey(newApiKey);
|
||||
setBaseUrl(newBaseUrl);
|
||||
clearUnauthorized();
|
||||
setClient(() => createClient(newApiKey, newBaseUrl, handleUnauthorized));
|
||||
const persist = useCallback(async (list: PostizWorkspace[]) => {
|
||||
await SecureStore.setItemAsync(WORKSPACES_KEY, JSON.stringify(list));
|
||||
setWorkspaces(list);
|
||||
setClients(buildClients(list));
|
||||
}, []);
|
||||
|
||||
const addWorkspace = useCallback(
|
||||
async (ws: Omit<PostizWorkspace, "id">) => {
|
||||
await persist([...workspaces, { ...ws, id: Date.now().toString() }]);
|
||||
},
|
||||
[handleUnauthorized, clearUnauthorized]
|
||||
[workspaces, persist]
|
||||
);
|
||||
|
||||
const clearSettings = useCallback(async () => {
|
||||
await SecureStore.deleteItemAsync(API_KEY_STORAGE);
|
||||
await SecureStore.deleteItemAsync(BASE_URL_STORAGE);
|
||||
setApiKey("");
|
||||
setBaseUrl(DEFAULT_BASE_URL);
|
||||
setClient(null);
|
||||
clearUnauthorized();
|
||||
}, [clearUnauthorized]);
|
||||
const updateWorkspace = useCallback(
|
||||
async (ws: PostizWorkspace) => {
|
||||
await persist(workspaces.map((w) => (w.id === ws.id ? ws : w)));
|
||||
},
|
||||
[workspaces, persist]
|
||||
);
|
||||
|
||||
const removeWorkspace = useCallback(
|
||||
async (id: string) => {
|
||||
await persist(workspaces.filter((w) => w.id !== id));
|
||||
},
|
||||
[workspaces, persist]
|
||||
);
|
||||
|
||||
const primaryWorkspace = workspaces[0] ?? null;
|
||||
|
||||
return (
|
||||
<PostizContext.Provider
|
||||
value={{
|
||||
apiKey,
|
||||
baseUrl,
|
||||
isConfigured: !!apiKey,
|
||||
workspaces,
|
||||
isConfigured: workspaces.length > 0,
|
||||
isLoading,
|
||||
unauthorized,
|
||||
clearUnauthorized,
|
||||
client,
|
||||
saveSettings,
|
||||
clearSettings,
|
||||
clients,
|
||||
addWorkspace,
|
||||
updateWorkspace,
|
||||
removeWorkspace,
|
||||
client: primaryWorkspace ? (clients[primaryWorkspace.id] ?? null) : null,
|
||||
apiKey: primaryWorkspace?.apiKey ?? "",
|
||||
baseUrl: primaryWorkspace?.baseUrl ?? DEFAULT_BASE_URL,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user