Skip to content

Commit

Permalink
fix node behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
radityaharya authored Apr 25, 2024
1 parent d7b2744 commit f1746f8
Show file tree
Hide file tree
Showing 25 changed files with 702 additions and 669 deletions.
23 changes: 7 additions & 16 deletions src/app/api/user/[uid]/playlist/[playlistId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,15 @@ export async function GET(

spClient.setAccessToken(accessToken as string);

const data = await spClient.getPlaylistTracks(params.playlistId);
const tracks = data.body.items;
const total = data.body.total;
const limit = data.body.limit;
let offset = limit;
while (offset < total) {
const data = await spClient.getPlaylistTracks(params.playlistId, {
offset,
});
tracks.push(...data.body.items);
offset += limit;
}
const response = await spClient.getPlaylist(params.playlistId);

const playlist = {
id: params.playlistId,
tracks,
total,
actual: tracks.length,
id: response.body.id,
name: response.body.name,
description: response.body.description,
image: response.body.images?.[0]?.url,
total: response.body.tracks.total,
owner: response.body.owner.display_name,
};

return NextResponse.json(playlist);
Expand Down
45 changes: 34 additions & 11 deletions src/app/api/user/[uid]/playlists/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { type NextRequest, NextResponse } from "next/server";
import SpotifyWebApi from "spotify-web-api-node";
import { env } from "~/env";
import { getAccessTokenFromProviderAccountId } from "~/server/db/helper";
import Redis from "ioredis";
import { Logger } from "~/lib/log";


const logger = new Logger("/api/user/[uid]/playlists");

const redis = new Redis(env.REDIS_URL, {
maxRetriesPerRequest: null,
});
export async function GET(
request: NextRequest,
{
Expand All @@ -15,11 +24,6 @@ export async function GET(
return NextResponse.json("No access token found", { status: 500 });
}

// const connection = new Redis(env.REDIS_URL, {
// maxRetriesPerRequest: null,
// });

// get user playlists and format them like above
const spClient = new SpotifyWebApi({
clientId: env.SPOTIFY_CLIENT_ID,
clientSecret: env.SPOTIFY_CLIENT_SECRET,
Expand All @@ -29,31 +33,50 @@ export async function GET(

const q = request.nextUrl.searchParams.get("q");

const cacheKey = q ? `search:${q}` : `user:${params.uid}`;
const cachedData = await redis.get(cacheKey);

if (cachedData) {
const ttl = await redis.ttl(cacheKey);
logger.info(`Cache hit for ${cacheKey}`);
return NextResponse.json(JSON.parse(cachedData), {
headers: {
"X-Cache": "HIT",
"X-Cache-TTL": ttl.toString(),
},
});
}

let data;
let playlists;

if (q) {
const data = await spClient.searchPlaylists(q, {
data = await spClient.searchPlaylists(q, {
limit: 50,
});
const playlists = data.body.playlists?.items.map((playlist) => ({
playlists = data.body.playlists?.items.map((playlist) => ({
playlistId: playlist.id,
name: playlist.name,
description: playlist.description,
image: playlist.images?.[0]?.url,
total: playlist.tracks.total,
owner: playlist.owner.display_name,
}));
return NextResponse.json(playlists);
} else {
const data = await spClient.getUserPlaylists(params.uid, {
data = await spClient.getUserPlaylists(params.uid, {
limit: 50,
});
const playlists = data.body.items.map((playlist) => ({
playlists = data.body.items.map((playlist) => ({
playlistId: playlist.id,
name: playlist.name,
description: playlist.description,
image: playlist.images?.[0]?.url,
total: playlist.tracks.total,
owner: playlist.owner.display_name,
}));
return NextResponse.json(playlists);
}

await redis.set(cacheKey, JSON.stringify(playlists), "EX", 10);

return NextResponse.json(playlists);
}
8 changes: 8 additions & 0 deletions src/app/states/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type RFState = {
onConnect: OnConnect;
setNodes: (nodes: Node[]) => void;
setEdges: (edges: Edge[]) => void;

setNode: (id: string, node: Node) => void;

addNode: (data: any) => Node;
addEdge: (data: any) => void;
updateNodeData: (id: string, data: any) => void;
Expand Down Expand Up @@ -99,6 +102,11 @@ const useStore = create<RFState>((set, get) => ({
edges: edges,
});
},
setNode(id: string, node: Node) {
set((state) => ({
nodes: state.nodes.map((n) => (n.id === id ? node : n)),
}));
},
onNodesChange(changes) {
set({
nodes: applyNodeChanges(changes, get().nodes),
Expand Down
5 changes: 3 additions & 2 deletions src/app/utils/reactFlowToWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ function addNodesToWorkflow(nodes, workflow) {
},
});
} else {
const typeWithoutPostfix = node.type!.split("-")[0];
workflow.operations.push({
id: node.id,
type: node.type!,
type: typeWithoutPostfix,
params: node.data,
position: node.position,
sources: [],
Expand Down Expand Up @@ -117,7 +118,7 @@ export default async function reactFlowToWorkflow({

let [valid, errors] = [true, {}];
if (nodes.length > 0 && edges.length > 0) {
nodes = filterNodes(nodes);
// nodes = filterNodes(nodes);
addNodesToWorkflow(nodes, workflowObject);
addEdgesToWorkflow(edges, workflowObject);
setNodesAsSources(workflowObject);
Expand Down
3 changes: 2 additions & 1 deletion src/app/workflow/Builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { toast } from "sonner";

import { useRouter } from "next/navigation";
import { useWorkflowData } from "~/hooks/useWorkflowData";
import React from "react";

function Builder({
params,
Expand Down Expand Up @@ -203,4 +204,4 @@ const LoadingSVG = () => (
</svg>
);

export default Builder;
export default React.memo(Builder);
Loading

0 comments on commit f1746f8

Please sign in to comment.