Skip to content

Commit

Permalink
Merge branch 'raycast:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
pernielsentikaer authored Aug 30, 2024
2 parents 948a5fe + 6cb5af3 commit 5ead41a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 52 deletions.
4 changes: 4 additions & 0 deletions extensions/workouts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Workouts Changelog

## [Added a new view on Create Activity Command success] - 2024-08-30

Added a new view after successfully creating an activity

## [Add Routes Command] - 2024-08-29

List all your routes with the `routes` command and download them as GPX/TCX files
Expand Down
70 changes: 27 additions & 43 deletions extensions/workouts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion extensions/workouts/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ export const createActivity = async (activityValues: StravaManualActivity) => {
if ((json as Error).message) {
throw new Error((json as Error).message);
}
return true;
const activity = json as StravaActivitySummary;
return activity;
} catch (err) {
const error = err instanceof Error ? err.message : "An error occurred";
throw new Error(error);
Expand Down
34 changes: 30 additions & 4 deletions extensions/workouts/src/createActivity.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ActionPanel, Form, Icon, Toast, showToast, getPreferenceValues, Action } from "@raycast/api";
import { SportType, StravaManualActivity } from "./api/types";
import { ActionPanel, Detail, Form, Icon, Toast, showToast, getPreferenceValues, Action } from "@raycast/api";
import { SportType, StravaActivitySummary, StravaManualActivity } from "./api/types";
import { createActivity, provider } from "./api/client";
import { formatSportTypesText, isDurationValid, isNumber } from "./utils";
import { withAccessToken, useForm, FormValidation } from "@raycast/utils";
import { useRef } from "react";
import { useState, useRef } from "react";

function CreateActivity() {
const sportTypes = Object.values(SportType);
const distanceUnitRef = useRef(getPreferenceValues<Preferences>().distance_unit);
const distanceFieldTitle = "Distance (" + distanceUnitRef.current + ")";
const [isSubmitted, setIsSubmitted] = useState(false);
const [activityDetails, setActivityDetails] = useState<StravaActivitySummary>();

const { handleSubmit, itemProps } = useForm<StravaManualActivity>({
onSubmit(values) {
Expand Down Expand Up @@ -41,16 +43,40 @@ function CreateActivity() {
title: "Saving your activity",
});
try {
await createActivity({ ...values, distanceUnit: distanceUnitRef.current });
const activity = await createActivity({ ...values, distanceUnit: distanceUnitRef.current });
toast.style = Toast.Style.Success;
toast.title = "Activity saved";
setIsSubmitted(true);
setActivityDetails(activity);
} catch (error) {
toast.style = Toast.Style.Failure;
toast.title = "Failed creating activity";
toast.message = String(error);
}
}

if (isSubmitted && activityDetails) {
const stravaLink = `https://www.strava.com/activities/${activityDetails.id}/`;
const markdownContent = `## 🎉 Kudos!
Kudos to you for logging another workout!
You can now view it on Strava [here](${stravaLink}).
Keep up the great work and stay active!`;

return (
<Detail
markdown={markdownContent}
actions={
<ActionPanel>
<Action.OpenInBrowser title="View on Strava" url={stravaLink} />
</ActionPanel>
}
/>
);
}

return (
<Form
actions={
Expand Down
5 changes: 1 addition & 4 deletions extensions/workouts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,9 @@ export function isDurationValid(
}

export function convertDistanceToMeters(distance: string, unit: string) {
if (!distance) return "";
const cleanedString = distance.trim();
const value = parseFloat(cleanedString);

if (isNaN(value)) {
throw new Error("Invalid distance format");
}
switch (unit) {
case "km":
return value * 1000;
Expand Down

0 comments on commit 5ead41a

Please sign in to comment.