Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Add design system client abstraction
Abi Raja08bdf85edba0
diff --git a/frontend/src/hooks/useDesignSystems.ts b/frontend/src/hooks/useDesignSystems.tsindex eefc4053..b27dfa62 100644--- a/frontend/src/hooks/useDesignSystems.ts+++ b/frontend/src/hooks/useDesignSystems.ts@@ -1,43 +1,46 @@import { useCallback, useEffect, useState } from "react";import toast from "react-hot-toast";import {- createDesignSystem,- deleteDesignSystem,+ defaultDesignSystemsClient,+ DesignSystemsClient,DesignSystemPayload,- fetchDesignSystems,- updateDesignSystem,} from "../lib/design-systems";import { DesignSystem } from "../types";-export function useDesignSystems() {+export function useDesignSystems(+ client: DesignSystemsClient = defaultDesignSystemsClient+) {const [designSystems, setDesignSystems] = useState<DesignSystem[]>([]);const [isLoading, setIsLoading] = useState(true);const refreshDesignSystems = useCallback(async () => {try {setIsLoading(true);- setDesignSystems(await fetchDesignSystems());+ setDesignSystems(await client.fetchDesignSystems());} catch (error) {console.error("Failed to load design systems", error);toast.error("Could not load design systems from the backend.");} finally {setIsLoading(false);}- }, []);+ }, [client]);useEffect(() => {refreshDesignSystems();}, [refreshDesignSystems]);- const create = useCallback(async (payload: DesignSystemPayload) => {- const created = await createDesignSystem(payload);- setDesignSystems((current) => [...current, created]);- return created;- }, []);+ const create = useCallback(+ async (payload: DesignSystemPayload) => {+ const created = await client.createDesignSystem(payload);+ setDesignSystems((current) => [...current, created]);+ return created;+ },+ [client]+ );const update = useCallback(async (id: string, payload: Partial<DesignSystemPayload>) => {- const updated = await updateDesignSystem(id, payload);+ const updated = await client.updateDesignSystem(id, payload);setDesignSystems((current) =>current.map((designSystem) =>designSystem.id === updated.id ? updated : designSystem@@ -45,15 +48,18 @@ export function useDesignSystems() {);return updated;},- []+ [client]);- const remove = useCallback(async (id: string) => {- await deleteDesignSystem(id);- setDesignSystems((current) =>- current.filter((designSystem) => designSystem.id !== id)- );- }, []);+ const remove = useCallback(+ async (id: string) => {+ await client.deleteDesignSystem(id);+ setDesignSystems((current) =>+ current.filter((designSystem) => designSystem.id !== id)+ );+ },+ [client]+ );return {designSystems,diff --git a/frontend/src/lib/design-systems.ts b/frontend/src/lib/design-systems.tsindex 1cd9dc71..d2ede443 100644--- a/frontend/src/lib/design-systems.ts+++ b/frontend/src/lib/design-systems.ts@@ -1,11 +1,33 @@import { HTTP_BACKEND_URL } from "../config";import { DesignSystem } from "../types";+type DesignSystemsRequestMethod = "GET" | "POST" | "PATCH" | "DELETE";+export interface DesignSystemPayload {name: string;content: string;}+export interface DesignSystemsRequestOptions {+ method?: DesignSystemsRequestMethod;+ body?: unknown;+}++export type DesignSystemsRequest = <T>(+ path: string,+ options?: DesignSystemsRequestOptions+) => Promise<T>;++export interface DesignSystemsClient {+ fetchDesignSystems: () => Promise<DesignSystem[]>;+ createDesignSystem: (payload: DesignSystemPayload) => Promise<DesignSystem>;+ updateDesignSystem: (+ id: string,+ payload: Partial<DesignSystemPayload>+ ) => Promise<DesignSystem>;+ deleteDesignSystem: (id: string) => Promise<void>;+}+export const NEW_DESIGN_SYSTEM_CONTENT = "";async function parseJsonResponse<T>(response: Response): Promise<T> {@@ -17,41 +39,77 @@ async function parseJsonResponse<T>(response: Response): Promise<T> {return response.json() as Promise<T>;}+function getDesignSystemUrl(baseUrl: string, path: string) {+ return `${baseUrl.replace(/\/$/, "")}${path}`;+}++export function createHttpDesignSystemsRequest(+ baseUrl = HTTP_BACKEND_URL,+ fetcher: typeof fetch = fetch+): DesignSystemsRequest {+ return async <T>(path: string, options: DesignSystemsRequestOptions = {}) => {+ const { method = "GET", body } = options;+ const response = await fetcher(getDesignSystemUrl(baseUrl, path), {+ method,+ headers:+ body === undefined ? undefined : { "Content-Type": "application/json" },+ body: body === undefined ? undefined : JSON.stringify(body),+ });++ if (response.status === 204) {+ return undefined as T;+ }++ return parseJsonResponse<T>(response);+ };+}++export function createDesignSystemsClient(+ request: DesignSystemsRequest+): DesignSystemsClient {+ return {+ fetchDesignSystems: () => request<DesignSystem[]>("/api/design-systems"),+ createDesignSystem: (payload: DesignSystemPayload) =>+ request<DesignSystem>("/api/design-systems", {+ method: "POST",+ body: payload,+ }),+ updateDesignSystem: (+ id: string,+ payload: Partial<DesignSystemPayload>+ ) =>+ request<DesignSystem>(`/api/design-systems/${id}`, {+ method: "PATCH",+ body: payload,+ }),+ deleteDesignSystem: (id: string) =>+ request<void>(`/api/design-systems/${id}`, {+ method: "DELETE",+ }),+ };+}++export const defaultDesignSystemsClient = createDesignSystemsClient(+ createHttpDesignSystemsRequest()+);+export async function fetchDesignSystems(): Promise<DesignSystem[]> {- const response = await fetch(`${HTTP_BACKEND_URL}/api/design-systems`);- return parseJsonResponse<DesignSystem[]>(response);+ return defaultDesignSystemsClient.fetchDesignSystems();}export async function createDesignSystem(payload: DesignSystemPayload): Promise<DesignSystem> {- const response = await fetch(`${HTTP_BACKEND_URL}/api/design-systems`, {- method: "POST",- headers: { "Content-Type": "application/json" },- body: JSON.stringify(payload),- });- return parseJsonResponse<DesignSystem>(response);+ return defaultDesignSystemsClient.createDesignSystem(payload);}export async function updateDesignSystem(id: string,payload: Partial<DesignSystemPayload>): Promise<DesignSystem> {- const response = await fetch(`${HTTP_BACKEND_URL}/api/design-systems/${id}`, {- method: "PATCH",- headers: { "Content-Type": "application/json" },- body: JSON.stringify(payload),- });- return parseJsonResponse<DesignSystem>(response);+ return defaultDesignSystemsClient.updateDesignSystem(id, payload);}export async function deleteDesignSystem(id: string): Promise<void> {- const response = await fetch(`${HTTP_BACKEND_URL}/api/design-systems/${id}`, {- method: "DELETE",- });-- if (!response.ok) {- const message = await response.text();- throw new Error(message || "Failed to delete design system");- }+ return defaultDesignSystemsClient.deleteDesignSystem(id);}