Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-kellam committed Jan 24, 2025
1 parent 1ce3325 commit ff79e33
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
15 changes: 13 additions & 2 deletions packages/web/src/app/api/(client)/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use client';

import { NEXT_PUBLIC_DOMAIN_SUB_PATH } from "@/lib/environment.client";
import { fileSourceResponseSchema, listRepositoriesResponseSchema, searchResponseSchema } from "@/lib/schemas";
import { FileSourceRequest, FileSourceResponse, ListRepositoriesResponse, SearchRequest, SearchResponse } from "@/lib/types";
import { fileSourceResponseSchema, getVersionResponseSchema, listRepositoriesResponseSchema, searchResponseSchema } from "@/lib/schemas";
import { FileSourceRequest, FileSourceResponse, GetVersionResponse, ListRepositoriesResponse, SearchRequest, SearchResponse } from "@/lib/types";
import assert from "assert";

export const search = async (body: SearchRequest): Promise<SearchResponse> => {
Expand Down Expand Up @@ -43,6 +43,17 @@ export const getRepos = async (): Promise<ListRepositoriesResponse> => {
return listRepositoriesResponseSchema.parse(result);
}

export const getVersion = async (): Promise<GetVersionResponse> => {
const path = resolveServerPath("/api/version");
const result = await fetch(path, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
}).then(response => response.json());
return getVersionResponseSchema.parse(result);
}

/**
* Given a subpath to a api route on the server (e.g., /api/search),
* returns the full path to that route on the server, taking into account
Expand Down
15 changes: 15 additions & 0 deletions packages/web/src/app/api/(server)/version/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SOURCEBOT_VERSION } from "@/lib/environment";
import { GetVersionResponse } from "@/lib/types";

// Note: In Next.JS 14, GET methods with no params are cached by default at build time.
// This creates issues since environment variables (like SOURCEBOT_VERSION) are
// not available until runtime. To work around this, we fore the route to be
// dynamic and evaluate on each request.
// @see: https://nextjs.org/docs/14/app/building-your-application/routing/route-handlers#caching
export const dynamic = "force-dynamic";

export const GET = async () => {
return Response.json({
version: SOURCEBOT_VERSION,
} satisfies GetVersionResponse);
}
17 changes: 14 additions & 3 deletions packages/web/src/app/components/upgradeToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import { useToast } from "@/components/hooks/use-toast";
import { ToastAction } from "@/components/ui/toast";
import { NEXT_PUBLIC_SOURCEBOT_VERSION } from "@/lib/environment.client";
import { useEffect } from "react";
import { useLocalStorage } from "usehooks-ts";
import { getVersion } from "../api/(client)/client";
import { useQuery } from "@tanstack/react-query";

const GITHUB_TAGS_URL = "https://api.github.com/repos/sourcebot-dev/sourcebot/tags";
const SEMVER_REGEX = /^v(\d+)\.(\d+)\.(\d+)$/;
Expand All @@ -23,8 +24,18 @@ export const UpgradeToast = () => {
new Date(0).toUTCString()
);

const { data: versionString } = useQuery({
queryKey: ["version"],
queryFn: () => getVersion(),
select: (data) => data.version,
})

useEffect(() => {
const currentVersion = getVersionFromString(NEXT_PUBLIC_SOURCEBOT_VERSION);
if (!versionString) {
return;
}

const currentVersion = getVersionFromString(versionString);
if (!currentVersion) {
return;
}
Expand Down Expand Up @@ -71,7 +82,7 @@ export const UpgradeToast = () => {

setUpgradeToastLastShownDate(new Date().toUTCString());
});
}, [setUpgradeToastLastShownDate, toast, upgradeToastLastShownDate]);
}, [setUpgradeToastLastShownDate, toast, upgradeToastLastShownDate, versionString]);

return null;
}
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/lib/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import { getEnv, getEnvNumber } from "./utils";
export const ZOEKT_WEBSERVER_URL = getEnv(process.env.ZOEKT_WEBSERVER_URL, "http://localhost:6070")!;
export const SHARD_MAX_MATCH_COUNT = getEnvNumber(process.env.SHARD_MAX_MATCH_COUNT, 10000);
export const TOTAL_MAX_MATCH_COUNT = getEnvNumber(process.env.TOTAL_MAX_MATCH_COUNT, 100000);
export const SOURCEBOT_VERSION = getEnv(process.env.SOURCEBOT_VERSION, 'unknown')!;
export const NODE_ENV = process.env.NODE_ENV;
4 changes: 4 additions & 0 deletions packages/web/src/lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,7 @@ export const listRepositoriesResponseSchema = z.object({
Stats: repoStatsSchema,
})
});

export const getVersionResponseSchema = z.object({
version: z.string(),
});
4 changes: 3 additions & 1 deletion packages/web/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { fileSourceRequestSchema, fileSourceResponseSchema, listRepositoriesResponseSchema, locationSchema, rangeSchema, repositorySchema, searchRequestSchema, searchResponseSchema, symbolSchema } from "./schemas";
import { fileSourceRequestSchema, fileSourceResponseSchema, getVersionResponseSchema, listRepositoriesResponseSchema, locationSchema, rangeSchema, repositorySchema, searchRequestSchema, searchResponseSchema, symbolSchema } from "./schemas";

export type KeymapType = "default" | "vim";

Expand All @@ -20,6 +20,8 @@ export type Repository = z.infer<typeof repositorySchema>;

export type Symbol = z.infer<typeof symbolSchema>;

export type GetVersionResponse = z.infer<typeof getVersionResponseSchema>;

export enum SearchQueryParams {
query = "query",
maxMatchDisplayCount = "maxMatchDisplayCount",
Expand Down

0 comments on commit ff79e33

Please sign in to comment.