Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Create raycast extension
Browse files Browse the repository at this point in the history
  • Loading branch information
colebemis committed Jul 26, 2022
1 parent 6b1058a commit dce6b59
Show file tree
Hide file tree
Showing 11 changed files with 1,085 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/docs/app/routes/components.$name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function ComponentPage() {
<h1>{data.component.name}</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>

{/* <h2>Uses</h2>
<h2>Uses</h2>

<h3>Primitives</h3>
<ul>
Expand All @@ -77,7 +77,7 @@ export default function ComponentPage() {
</Link>
</li>
))}
</ul> */}
</ul>
</div>
);
}
5 changes: 3 additions & 2 deletions examples/docs/app/routes/primitives.$name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export default function ComponentPage() {
<div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.4" }}>
<h1>{data.designToken.name}</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
{/* <h2>Used by</h2>

<h2>Used by</h2>
<ul>
{data.designToken.usedByComponents.map(({ name }) => (
<li key={name}>
Expand All @@ -66,7 +67,7 @@ export default function ComponentPage() {
</Link>
</li>
))}
</ul> */}
</ul>
</div>
);
}
10 changes: 10 additions & 0 deletions examples/raycast/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"root": true,
"env": {
"es2020": true,
"node": true
},
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"]
}
1 change: 1 addition & 0 deletions examples/raycast/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Primer Raycast extension
Binary file added examples/raycast/assets/command-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/raycast/assets/list-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions examples/raycast/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "https://www.raycast.com/schemas/extension.json",
"name": "raycast",
"title": "Primer",
"description": "",
"icon": "command-icon.png",
"author": "cole",
"categories": [
"Design Tools"
],
"license": "MIT",
"commands": [
{
"name": "index",
"title": "Search components",
"description": "",
"mode": "view"
}
],
"dependencies": {
"@raycast/api": "^1.38.2",
"change-case": "^4.1.2",
"graphql": "^16.5.0",
"graphql-request": "^4.3.0",
"node-fetch": "^3.0.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.5.1",
"typescript": "^4.4.3"
},
"scripts": {
"build": "ray build -e dist",
"dev": "ray develop",
"fix-lint": "ray lint --fix",
"lint": "ray lint",
"publish": "ray publish"
}
}
145 changes: 145 additions & 0 deletions examples/raycast/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { Action, ActionPanel, List, showToast, Toast } from "@raycast/api";
import { paramCase, pascalCase } from "change-case";
import { gql, GraphQLClient } from "graphql-request";
import { AbortError } from "node-fetch";
import { useCallback, useEffect, useRef, useState } from "react";

export default function Command() {
const { state, search } = useSearch();

return (
<List
isLoading={state.isLoading}
onSearchTextChange={search}
searchBarPlaceholder="Search components..."
throttle
>
<List.Section title="Results" subtitle={state.results.length + ""}>
{state.results.map((searchResult) => (
<SearchListItem key={searchResult.name} searchResult={searchResult} />
))}
</List.Section>
</List>
);
}

function SearchListItem({ searchResult }: { searchResult: SearchResult }) {
return (
<List.Item
title={searchResult.name}
// subtitle={searchResult.description}
// accessoryTitle={searchResult.username}
actions={
<ActionPanel>
<Action.OpenInBrowser
title="Open documentation"
url={`http://localhost:3000/components/${paramCase(
searchResult.name
)}`}
/>

<Action.CopyToClipboard
title="Copy name"
content={pascalCase(searchResult.name)}
shortcut={{ modifiers: ["cmd"], key: "." }}
/>
</ActionPanel>
}
/>
);
}

function useSearch() {
const [state, setState] = useState<SearchState>({
results: [],
isLoading: true,
});
const cancelRef = useRef<AbortController | null>(null);

const search = useCallback(
async function search(searchText: string) {
cancelRef.current?.abort();
cancelRef.current = new AbortController();
setState((oldState) => ({
...oldState,
isLoading: true,
}));
try {
const results = await performSearch(
searchText,
cancelRef.current.signal
);
setState((oldState) => ({
...oldState,
results: results,
isLoading: false,
}));
} catch (error) {
setState((oldState) => ({
...oldState,
isLoading: false,
}));

if (error instanceof AbortError) {
return;
}

console.error("search error", error);
showToast({
style: Toast.Style.Failure,
title: "Could not perform search",
message: String(error),
});
}
},
[cancelRef, setState]
);

useEffect(() => {
search("");
return () => {
cancelRef.current?.abort();
};
}, []);

return {
state: state,
search: search,
};
}

async function performSearch(
searchText: string,
signal: AbortSignal
): Promise<SearchResult[]> {
const client = new GraphQLClient("http://localhost:4000");
const query = gql`
query ($searchText: String!) {
components(
where: { name: { contains: $searchText, mode: insensitive } }
) {
name
}
}
`;

const results = await client.request({
document: query,
variables: { searchText },
// signal,
});

return results.components;
}

interface SearchState {
results: SearchResult[];
isLoading: boolean;
}

interface SearchResult {
name: string;
// description?: string;
// username?: string;
// url: string;
}
17 changes: 17 additions & 0 deletions examples/raycast/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 16",
"include": ["src/**/*"],
"compilerOptions": {
"lib": ["es2021"],
"module": "commonjs",
"target": "es2021",
"strict": true,
"isolatedModules": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react-jsx",
"resolveJsonModule": true
}
}
Loading

0 comments on commit dce6b59

Please sign in to comment.