Skip to content

Commit

Permalink
[Claude] Fix: models not displaying correctly on the Ask page (raycas…
Browse files Browse the repository at this point in the history
…t#15757)

* fix: models not displaying correctly on the Ask page

* format

* Update CHANGELOG.md and optimise images

---------

Co-authored-by: Per Nielsen Tikær <[email protected]>
Co-authored-by: raycastbot <[email protected]>
  • Loading branch information
3 people authored and gogocharli committed Jan 6, 2025
1 parent 1328154 commit 71162ec
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 49 deletions.
4 changes: 4 additions & 0 deletions extensions/claude/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Claude Changelog

## [Update] - 2024-12-20

- Fix: Models dropdown not displaying all models correctly

## [Update] - 2024-12-12

- Fix: Remove duplicated "Copy Question" action, use existing with tweaked conditions
Expand Down
1 change: 1 addition & 0 deletions extensions/claude/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"alextlopez11",
"FelixII",
"midnite",
"gilberto_jr",
"ridemountainpig",
"ridjis"
],
Expand Down
2 changes: 1 addition & 1 deletion extensions/claude/src/ask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function Ask(props: { conversation?: Conversation }) {
}

setLoading(false);
}, [question.data]);
}, [question.data, models.data]);

useEffect(() => {
if (props.conversation?.id !== conversation.id || conversations.data.length === 0) {
Expand Down
89 changes: 49 additions & 40 deletions extensions/claude/src/hooks/useModel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LocalStorage, showToast, Toast } from "@raycast/api";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useState, useTransition } from "react";
import { Model, ModelHook } from "../type";

export const DEFAULT_MODEL: Model = {
Expand All @@ -14,67 +14,70 @@ export const DEFAULT_MODEL: Model = {
pinned: false,
};

const option: Model["option"][] = [
"claude-3-5-haiku-latest",
"claude-3-5-haiku-20241022",
"claude-3-5-sonnet-latest",
"claude-3-5-sonnet-20241022",
"claude-3-5-sonnet-20240620",
"claude-3-opus-20240229",
"claude-3-sonnet-20240229",
"claude-3-haiku-20240307",
"claude-2.1",
"claude-2.0",
];

async function getStoredModels(): Promise<Model[]> {
const storedModels = await LocalStorage.getItem<string>("models");
if (!storedModels) {
return [DEFAULT_MODEL];
}
return [...JSON.parse(storedModels), DEFAULT_MODEL] satisfies Model[];
}

export function useModel(): ModelHook {
const [data, setData] = useState<Model[]>([]);
const [isLoading, setLoading] = useState<boolean>(false);

const option: Model["option"][] = [
"claude-3-5-haiku-latest",
"claude-3-5-haiku-20241022",
"claude-3-5-sonnet-latest",
"claude-3-5-sonnet-20241022",
"claude-3-5-sonnet-20240620",
"claude-3-opus-20240229",
"claude-3-sonnet-20240229",
"claude-3-haiku-20240307",
"claude-2.1",
"claude-2.0",
];
const [isLoading, setLoading] = useState(false);

useEffect(() => {
(async () => {
setLoading(true);
const storedModels = await LocalStorage.getItem<string>("models");

if (!storedModels) {
setData([DEFAULT_MODEL]);
} else {
setData((previous) => [...previous, ...JSON.parse(storedModels)]);
}
setLoading(false);
})();
setLoading(true);
getStoredModels()
.then(setData)
.finally(() => setLoading(false));
}, []);

useEffect(() => {
LocalStorage.setItem("models", JSON.stringify(data));
}, [data]);

const add = useCallback(
async (model: Model) => {
const toast = await showToast({
title: "Saving your model...",
style: Toast.Style.Animated,
});
const newModel: Model = { ...model, created_at: new Date().toISOString() };
setData([...data, newModel]);
setData((prevData) => {
const newData = [...prevData, newModel];
LocalStorage.setItem("models", JSON.stringify(newData));
return newData;
});
toast.title = "Model saved!";
toast.style = Toast.Style.Success;
},
[setData, data]
[setData]
);

const update = useCallback(
async (model: Model) => {
setData((prev) => {
return prev.map((x) => {
setData((prevData) => {
const newModels = prevData.map((x) => {
if (x.id === model.id) {
return model;
}
return x;
});
LocalStorage.setItem("models", JSON.stringify(newModels));
return newModels;
});
},
[setData, data]
[setData]
);

const remove = useCallback(
Expand All @@ -83,21 +86,27 @@ export function useModel(): ModelHook {
title: "Remove your model...",
style: Toast.Style.Animated,
});
const newModels: Model[] = data.filter((oldModel) => oldModel.id !== model.id);
setData(newModels);
setData((prevData) => {
const newModels = prevData.filter((oldModel) => oldModel.id !== model.id);
LocalStorage.setItem("models", JSON.stringify(newModels));
return newModels;
});
toast.title = "Model removed!";
toast.style = Toast.Style.Success;
},
[setData, data]
[setData]
);

const clear = useCallback(async () => {
const toast = await showToast({
title: "Clearing your models ...",
style: Toast.Style.Animated,
});
const newModels: Model[] = data.filter((oldModel) => oldModel.id === DEFAULT_MODEL.id);
setData(newModels);
setData((prevData) => {
const newModels: Model[] = prevData.filter((oldModel) => oldModel.id === DEFAULT_MODEL.id);
LocalStorage.setItem("models", JSON.stringify(newModels));
return newModels;
});
toast.title = "Models cleared!";
toast.style = Toast.Style.Success;
}, [setData]);
Expand Down
9 changes: 1 addition & 8 deletions extensions/claude/src/views/model/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ export const ModelDropdown = (props: ChangeModelProp) => {
const separateDefaultModel = models.filter((x) => x.id !== "default");
const defaultModel = models.find((x) => x.id === "default");
return (
<List.Dropdown
tooltip="Select Model"
storeValue={true}
defaultValue={selectedModel}
onChange={(id) => {
onModelChange(id);
}}
>
<List.Dropdown tooltip="Select Model" storeValue={true} defaultValue={selectedModel} onChange={onModelChange}>
{defaultModel && <List.Dropdown.Item key={defaultModel.id} title={defaultModel.name} value={defaultModel.id} />}
<List.Dropdown.Section title="Custom Models">
{separateDefaultModel.map((model) => (
Expand Down

0 comments on commit 71162ec

Please sign in to comment.