Skip to content

Commit

Permalink
Clean Up Types (#255)
Browse files Browse the repository at this point in the history
* Remove Logs

* Improve track() Types

* Simplify Server Action Types
  • Loading branch information
timoclsn authored Oct 30, 2023
1 parent ee7d299 commit 5c20c32
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const DownloadButtonClient = ({ csv }: Props) => {
size="large"
onClick={() => {
downloadCsv(csv, 'lifecentereddesign-net-resources.csv');
track('Download Resources', {});
track('Download Resources');
}}
>
<Download />
Expand Down
2 changes: 1 addition & 1 deletion apps/resources/app/resources/Suggestion/SuggestionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const SuggestionForm = () => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
track('Resource Suggestion', {});
track('Resource Suggestion');
},
onError: () => {
if (!linkInputRef.current) return;
Expand Down
2 changes: 1 addition & 1 deletion apps/resources/components/Header/CO2Badge/CO2Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const CO2Badge = ({ co2, cleanerThan }: Props) => {

const onOpenChange = (open: boolean) => {
if (open) {
track('Open CO2 Badge', {});
track('Open CO2 Badge');
}
setOpen(open);
};
Expand Down
2 changes: 1 addition & 1 deletion apps/resources/components/Newsletter/NewsletterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const NewsletterForm = () => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
track('Newsletter Signup', {});
track('Newsletter Signup');
},
onError: () => {
if (!emailInputRef.current) return;
Expand Down
4 changes: 2 additions & 2 deletions apps/resources/hooks/useFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export const useFilter = () => {
}

if (param === 'likes') {
track('Toggle filter resources by likes', {});
track('Toggle filter resources by likes');
}

if (param === 'comments') {
track('Toggle filter resources by comments', {});
track('Toggle filter resources by comments');
}

if (param === 'limit') {
Expand Down
7 changes: 3 additions & 4 deletions apps/resources/lib/serverActions/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ const createReducer =

export const useAction = <
TInputSchema extends z.ZodTypeAny,
TInput extends TInputSchema | undefined,
TResponse extends any,
>(
inputAction: ServerAction<TInputSchema, TInput, TResponse>,
inputAction: ServerAction<TInputSchema, TResponse>,
options: {
onRunAction?: (input: InferInputType<TInputSchema, TInput>) => void;
onRunAction?: (input: InferInputType<TInputSchema>) => void;
onSuccess?: (data: TResponse | null) => void;
onError?: (
error: string | null,
Expand All @@ -92,7 +91,7 @@ export const useAction = <
const [isRunning, startTransition] = useTransition();

const runAction = useCallback(
async (input: InferInputType<TInputSchema, TInput>) => {
async (input: InferInputType<TInputSchema>) => {
startTransition(async () => {
dispatch({
type: 'RUN_ACTION',
Expand Down
17 changes: 6 additions & 11 deletions apps/resources/lib/serverActions/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,30 @@ import {

type MaybePromise<T> = Promise<T> | T;

export type InferInputType<
TInputSchema extends z.ZodTypeAny,
TInput extends TInputSchema | undefined,
> = z.ZodTypeAny extends TInput ? void : z.input<TInputSchema>;
export type InferInputType<TInputSchema extends z.ZodTypeAny> =
z.ZodTypeAny extends TInputSchema ? void : z.infer<TInputSchema>;

export type InferValidationErrors<TInputSchema extends z.ZodTypeAny> =
z.inferFlattenedErrors<TInputSchema>['fieldErrors'];

interface Result<TInputSchema extends z.ZodTypeAny, TResponse> {
interface Result<TInputSchema extends z.ZodTypeAny, TResponse extends any> {
data: TResponse | null;
error: string | null;
validationErrors: InferValidationErrors<TInputSchema> | null;
}

export type ServerAction<
TInputSchema extends z.ZodTypeAny,
TInput extends TInputSchema | undefined,
TResponse,
TResponse extends any,
> = (
input: InferInputType<TInputSchema, TInput>,
input: InferInputType<TInputSchema>,
) => Promise<Result<TInputSchema, TResponse>> | void;

export const createActionClient = <Context>(createClientOpts?: {
middleware?: () => MaybePromise<Context>;
}) => {
const actionBuilder = <
TInputSchema extends z.ZodTypeAny,
TInput extends TInputSchema | undefined,
TResponse extends any,
>(actionBuilderOpts: {
input?: TInputSchema;
Expand All @@ -43,7 +39,7 @@ export const createActionClient = <Context>(createClientOpts?: {
ctx: Context;
}) => MaybePromise<void> | MaybePromise<TResponse>;
}) => {
const createAction: ServerAction<TInputSchema, TInput, TResponse> = async (
const createAction: ServerAction<TInputSchema, TResponse> = async (
input,
) => {
try {
Expand Down Expand Up @@ -82,7 +78,6 @@ export const createActionClient = <Context>(createClientOpts?: {
isNextRedirectError(errorMessage) ||
isNextNotFoundError(errorMessage)
) {
console.log('is next errror');
throw error;
}

Expand Down
16 changes: 9 additions & 7 deletions apps/resources/lib/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ContentType } from './resources';

interface TrackingEvents {
'Download Resources': {};
'Resource Suggestion': {};
'Download Resources': null;
'Resource Suggestion': null;
'Copy Resource Link': {
link: string;
};
Expand All @@ -28,7 +28,7 @@ interface TrackingEvents {
'Uncaught error': {
message: string;
};
'Newsletter Signup': {};
'Newsletter Signup': null;
'Filter resources by type': {
type: string;
};
Expand All @@ -41,17 +41,19 @@ interface TrackingEvents {
'Sort resources': {
by: string;
};
'Toggle filter resources by likes': {};
'Toggle filter resources by comments': {};
'Toggle filter resources by likes': null;
'Toggle filter resources by comments': null;
'Show more resources': {
count: number;
};
'Open CO2 Badge': {};
'Open CO2 Badge': null;
}

export const track = <TEventKey extends keyof TrackingEvents>(
event: TEventKey,
data: TrackingEvents[TEventKey],
...data: TrackingEvents[TEventKey] extends null
? []
: [TrackingEvents[TEventKey]]
) => {
splitbee.track(event, data);
};
1 change: 0 additions & 1 deletion apps/resources/lib/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ describe('getBaseUrl', () => {
process.env.NEXT_PUBLIC_VERCEL_ENV = '';
process.env.NEXT_PUBLIC_VERCEL_URL = '';
process.env.PORT = '3000';
console.log(process.env.NEXT_PUBLIC_VERCEL_URL);
const baseUrl = getBaseUrl();
expect(baseUrl).toBe('http://localhost:3000');
});
Expand Down

1 comment on commit 5c20c32

@vercel
Copy link

@vercel vercel bot commented on 5c20c32 Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.