export async function loader()

in app/routes/api.jobs.tsx [19:88]


export async function loader({ request }: LoaderFunctionArgs) {
  console.log("GET /api/jobs");
  const url = new URL(request.url);
  const page = parseInt(url.searchParams.get("page") || "1");
  const limit = Math.min(parseInt(url.searchParams.get("limit") || "20"), 100);
  const status = url.searchParams.get("status");
  const search = url.searchParams.get("search");

  // Check authentication
  if (!isPublicPath(url.pathname)) {
    const cookieHeader = request.headers.get("Cookie");
    const credentials = extractCredentialsFromCookie(cookieHeader);

    if (!hasValidCredentials(credentials)) {
      return json(
        {
          error: {
            code: "UNAUTHORIZED",
            message:
              "Authentication required - please provide API credentials through the UI",
          },
        },
        { status: 401 }
      );
    }

    try {
      const jobStore = getJobStore();

      // Get the authenticated user's username
      // Use effective username which can be from HF or GitHub
      const username = getEffectiveUsername(credentials);

      // Add the author filter to only show the user's own jobs
      const result = await jobStore.listJobs({
        page,
        limit,
        status: status === "all" ? undefined : status || undefined,
        search: search || undefined,
        author: username, // Filter by the authenticated user
      });

      return json(result);
    } catch (error) {
      console.error("Error listing jobs:", error);
      return json(
        {
          error: {
            code: "INTERNAL_ERROR",
            message: "Failed to list jobs",
          },
        },
        { status: 500 }
      );
    }
  } else {
    // For public paths, only return minimal information
    return json({
      jobs: [],
      pagination: {
        page: 1,
        limit,
        total: 0,
        totalPages: 0,
        hasNext: false,
        hasPrev: false,
      },
    });
  }
}