Skip to content

feat: implement multi-org template gallery #13784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0a5c431
feat: initial changes for multi-org templates page
jaaydenh Jul 1, 2024
6f96fee
feat: add TemplateCard component
jaaydenh Jul 2, 2024
51ebb67
feat: add component stories
jaaydenh Jul 3, 2024
a0effc6
chore: update template query naming
jaaydenh Jul 3, 2024
be37085
fix: fix formatting
jaaydenh Jul 3, 2024
5649166
feat: template card interaction and navigation
jaaydenh Jul 8, 2024
3ea3aa2
fix: copy updates
jaaydenh Jul 8, 2024
f17a0c3
chore: update TemplateFilter type to include FilterQuery
jaaydenh Jul 9, 2024
0077db0
chore: update typesGenerated.ts
jaaydenh Jul 9, 2024
461202e
feat: update template filter api logic
jaaydenh Jul 9, 2024
66e02fb
fix: fix format
jaaydenh Jul 11, 2024
369c59f
fix: get activeOrg
jaaydenh Jul 11, 2024
c41cdc4
fix: add format annotation
jaaydenh Jul 11, 2024
7f5d35e
chore: use organization display name
jaaydenh Jul 11, 2024
6e2a6d8
feat: client side org filtering
jaaydenh Jul 15, 2024
978c047
fix: use org display name
jaaydenh Jul 15, 2024
aaed038
fix: add ExactName
jaaydenh Jul 15, 2024
8d84ad9
feat: show orgs filter only if more than 1 org
jaaydenh Jul 15, 2024
a1c6169
chore: updates for PR review
jaaydenh Jul 16, 2024
15542c0
fix: fix format
jaaydenh Jul 16, 2024
8f4c56f
chore: add story for multi org
jaaydenh Jul 16, 2024
a282bac
fix: aggregate templates by organization id
jaaydenh Jul 17, 2024
b092644
fix: fix format
jaaydenh Jul 17, 2024
32376e6
fix: check org count
jaaydenh Jul 17, 2024
801138a
fix: update ExactName type
jaaydenh Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: update template filter api logic
  • Loading branch information
jaaydenh committed Jul 17, 2024
commit 461202e2cff2db560bf197d837a855d7cb4fedda
25 changes: 6 additions & 19 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ export const watchBuildLogsByTemplateVersionId = (

const proto = location.protocol === "https:" ? "wss:" : "ws:";
const socket = new WebSocket(
`${proto}//${
location.host
`${proto}//${location.host
}/api/v2/templateversions/${versionId}/logs?${searchParams.toString()}`,
);

Expand Down Expand Up @@ -270,8 +269,7 @@ export const watchBuildLogsByBuildId = (
}
const proto = location.protocol === "https:" ? "wss:" : "ws:";
const socket = new WebSocket(
`${proto}//${
location.host
`${proto}//${location.host
}/api/v2/workspacebuilds/${buildId}/logs?${searchParams.toString()}`,
);
socket.binaryType = "blob";
Expand Down Expand Up @@ -382,7 +380,7 @@ export class MissingBuildParameters extends Error {
* lexical scope.
*/
class ApiMethods {
constructor(protected readonly axios: AxiosInstance) {}
constructor(protected readonly axios: AxiosInstance) { }

login = async (
email: string,
Expand Down Expand Up @@ -599,21 +597,10 @@ class ApiMethods {
};

getTemplates = async (
options?: TemplateOptions,
options?: TypesGen.TemplateFilter,
): Promise<TypesGen.Template[]> => {
const params: Record<string, string> = {};
if (options?.deprecated !== undefined) {
// Just want to check if it isn't undefined. If it has
// a boolean value, convert it to a string and include
// it as a param.
params["deprecated"] = String(options.deprecated);
}

const response = await this.axios.get<TypesGen.Template[]>(
`/api/v2/templates`,
{ params },
);

const url = getURLWithSearchParams("/api/v2/templates", options);
const response = await this.axios.get<TypesGen.Template[]>(url);
return response.data;
};

Expand Down
4 changes: 2 additions & 2 deletions site/src/api/queries/audits.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { API } from "api/api";
import type { AuditLogResponse } from "api/typesGenerated";
import { useFilterParamsKey } from "components/Filter/filter";
import type { UsePaginatedQueryOptions } from "hooks/usePaginatedQuery";
import { filterParamsKey } from "utils/filters";

export function paginatedAudits(
searchParams: URLSearchParams,
): UsePaginatedQueryOptions<AuditLogResponse, string> {
return {
searchParams,
queryPayload: () => searchParams.get(useFilterParamsKey) ?? "",
queryPayload: () => searchParams.get(filterParamsKey) ?? "",
queryKey: ({ payload, pageNumber }) => {
return ["auditLogs", payload, pageNumber] as const;
},
Expand Down
12 changes: 4 additions & 8 deletions site/src/api/queries/templates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { MutationOptions, QueryClient, QueryOptions } from "react-query";
import { API } from "api/api";
import type {
TemplateFilter,
CreateTemplateRequest,
CreateTemplateVersionRequest,
ProvisionerJob,
Expand Down Expand Up @@ -46,15 +47,10 @@ export const templatesByOrganizationId = (
};
};

const getTemplatesQueryKey = (deprecated?: boolean) => [
"templates",
deprecated,
];

export const templates = (deprecated?: boolean) => {
export const templates = (config: TemplateFilter = {}) => {
return {
queryKey: getTemplatesQueryKey(deprecated),
queryFn: () => API.getTemplates({ deprecated }),
queryKey: ["templates", config],
queryFn: () => API.getTemplates(config),
};
};

Expand Down
7 changes: 3 additions & 4 deletions site/src/components/Filter/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { InputGroup } from "components/InputGroup/InputGroup";
import { SearchField } from "components/SearchField/SearchField";
import { useDebouncedFunction } from "hooks/debounce";
import { filterParamsKey } from "utils/filters";

export type PresetFilter = {
name: string;
Expand All @@ -35,21 +36,19 @@ type UseFilterConfig = {
onUpdate?: (newValue: string) => void;
};

export const useFilterParamsKey = "filter";

export const useFilter = ({
fallbackFilter = "",
searchParamsResult,
onUpdate,
}: UseFilterConfig) => {
const [searchParams, setSearchParams] = searchParamsResult;
const query = searchParams.get(useFilterParamsKey) ?? fallbackFilter;
const query = searchParams.get(filterParamsKey) ?? fallbackFilter;

const update = (newValues: string | FilterValues) => {
const serialized =
typeof newValues === "string" ? newValues : stringifyFilter(newValues);

searchParams.set(useFilterParamsKey, serialized);
searchParams.set(filterParamsKey, serialized);
setSearchParams(searchParams);

if (onUpdate !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface TemplatesPageViewProps {
templates: Template[] | undefined;
examples: TemplateExample[] | undefined;
canCreateTemplates: boolean;
query: string | undefined;
error?: unknown;
}

Expand Down Expand Up @@ -79,14 +80,12 @@ export const TemplatesPageView: FC<TemplatesPageViewProps> = ({
templates,
examples,
canCreateTemplates,
query,
error,
}) => {
const [urlParams] = useSearchParams();
const isEmpty = templates && templates.length === 0;
const navigate = useNavigate();

const activeOrg = urlParams.get("org") ?? "all";

const isEmpty = templates && templates.length === 0;
const activeOrg = query?.split(":")[1] ?? "all";
const templatesByOrg = getTemplatesByOrg(templates ?? []);

return (
Expand All @@ -109,15 +108,17 @@ export const TemplatesPageView: FC<TemplatesPageViewProps> = ({
)}
</PageHeader>

{Boolean(error) && <ErrorAlert error={error} />}
{Boolean(error) && (
<ErrorAlert error={error} css={{ marginBottom: 32 }} />
)}

<Stack direction="row" spacing={4} alignItems="flex-start">
<Stack css={{ width: 208, flexShrink: 0, position: "sticky", top: 48 }}>
<span css={styles.filterCaption}>ORGANIZATION</span>
{Object.keys(templatesByOrg).map((org) => (
<Link
key={org}
to={`?org=${org}`}
to={org !== "all" ? `?filter=organization:${org}` : "?"}
css={[styles.tagLink, org === activeOrg && styles.tagLinkActive]}
>
{org === "all" ? "All Organizations" : org} (
Expand Down
7 changes: 6 additions & 1 deletion site/src/pages/TemplatesPage/TemplatesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import type { FC } from "react";
import { Helmet } from "react-helmet-async";
import { useQuery } from "react-query";
import { useSearchParams } from "react-router-dom";
import {
templateExamples,
templatesByOrganizationId,
templates,
} from "api/queries/templates";
import { useAuthenticated } from "contexts/auth/RequireAuth";
import { useDashboard } from "modules/dashboard/useDashboard";
import { filterParamsKey } from "utils/filters";
import { pageTitle } from "utils/page";
import { TemplatesPageView as MultiOrgTemplatesPageView } from "./MultiOrgTemplatePage/TemplatesPageView";
import { TemplatesPageView } from "./TemplatePage/TemplatesPageView";

export const TemplatesPage: FC = () => {
const { permissions } = useAuthenticated();
const { organizationId, experiments } = useDashboard();
const [searchParams] = useSearchParams();
const query = searchParams.get(filterParamsKey) || undefined;

const templatesByOrganizationIdQuery = useQuery(
templatesByOrganizationId(organizationId),
);
const templatesQuery = useQuery(templates());
const templatesQuery = useQuery(templates({ q: query }));
const examplesQuery = useQuery({
...templateExamples(organizationId),
enabled: permissions.createTemplates,
Expand All @@ -41,6 +45,7 @@ export const TemplatesPage: FC = () => {
canCreateTemplates={permissions.createTemplates}
examples={examplesQuery.data}
templates={templatesQuery.data}
query={query}
/>
) : (
<TemplatesPageView
Expand Down
1 change: 1 addition & 0 deletions site/src/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export function prepareQuery(query: string | undefined): string | undefined;
export function prepareQuery(query?: string): string | undefined {
return query?.trim().replace(/ +/g, " ");
}
export const filterParamsKey = "filter";