|
| 1 | +"use client"; |
| 2 | + |
1 | 3 | import { useActiveAsset } from "@/hooks/useActiveAsset"; |
2 | 4 | import { useActiveOrg } from "@/hooks/useActiveOrg"; |
3 | 5 | import { useActiveProject } from "@/hooks/useActiveProject"; |
4 | | -import { useActiveAssetVersion } from "@/hooks/useActiveAssetVersion"; |
| 6 | +import useDecodedParams from "@/hooks/useDecodedParams"; |
5 | 7 | import Link from "next/link"; |
6 | | -import React, { useState, useEffect } from "react"; |
| 8 | +import { useEffect, useState } from "react"; |
7 | 9 | import { Badge } from "../ui/badge"; |
8 | 10 | import ProjectTitle from "./ProjectTitle"; |
9 | | -import useDecodedParams from "@/hooks/useDecodedParams"; |
| 11 | +import { eventBus } from "@/events"; |
10 | 12 |
|
11 | 13 | const AssetTitle = () => { |
12 | 14 | const activeOrg = useActiveOrg(); |
13 | 15 | const project = useActiveProject()!; |
14 | 16 | const asset = useActiveAsset(); |
15 | | - const assetVersion = useActiveAssetVersion(); |
16 | 17 |
|
17 | 18 | const params = useDecodedParams() as { assetVersionSlug?: string }; |
18 | 19 |
|
19 | | - const [assetVersionSlug, setAssetVersionSlug] = useState<string | undefined>( |
20 | | - undefined, |
21 | | - ); |
| 20 | + const [assetVersionSlug, setAssetVersionSlug] = useState<string>(); |
22 | 21 |
|
23 | 22 | useEffect(() => { |
24 | | - const currentSlug = assetVersion?.slug ?? params?.assetVersionSlug; |
| 23 | + const currentSlug = params?.assetVersionSlug; |
25 | 24 |
|
26 | 25 | if (currentSlug) { |
27 | | - localStorage.setItem("lastViewedAssetVersionSlug", currentSlug); |
| 26 | + localStorage.setItem( |
| 27 | + "lastViewedAssetVersionSlug" + asset?.slug, |
| 28 | + currentSlug, |
| 29 | + ); |
28 | 30 | setAssetVersionSlug(currentSlug); |
29 | 31 | } else { |
30 | | - const stored = localStorage.getItem("lastViewedAssetVersionSlug"); |
31 | | - setAssetVersionSlug(stored ?? undefined); |
| 32 | + const stored = localStorage.getItem( |
| 33 | + "lastViewedAssetVersionSlug" + asset?.slug, |
| 34 | + ); |
| 35 | + setAssetVersionSlug(stored && stored !== "undefined" ? stored : ""); |
32 | 36 | } |
33 | | - }, [assetVersion?.slug, params?.assetVersionSlug]); |
| 37 | + }, [params?.assetVersionSlug, asset?.slug]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + eventBus.subscribe("assetTitleListener", "assetVersionDeleted", () => { |
| 41 | + const stored = localStorage.getItem( |
| 42 | + "lastViewedAssetVersionSlug" + asset?.slug, |
| 43 | + ); |
| 44 | + setAssetVersionSlug(stored && stored !== "undefined" ? stored : ""); |
| 45 | + }); |
| 46 | + |
| 47 | + return () => { |
| 48 | + eventBus.unsubscribe("assetTitleListener"); |
| 49 | + }; |
| 50 | + }, [asset?.slug]); |
| 51 | + |
34 | 52 | return ( |
35 | 53 | <span className="flex flex-row gap-2 min-w-0 overflow-hidden"> |
36 | 54 | <ProjectTitle /> |
37 | 55 | <span className="opacity-75 flex-shrink-0">/</span> |
38 | 56 | <Link |
39 | 57 | className="flex !text-header-foreground items-center gap-1 hover:no-underline min-w-0" |
40 | | - href={`/${activeOrg?.slug}/projects/${project?.slug}/assets/${asset?.slug}/refs/${assetVersionSlug}`} |
| 58 | + href={ |
| 59 | + `/${activeOrg?.slug}/projects/${project?.slug}/assets/${asset?.slug}` + |
| 60 | + (assetVersionSlug && assetVersionSlug !== "undefined" |
| 61 | + ? `/refs/${assetVersionSlug}` |
| 62 | + : "") |
| 63 | + } |
41 | 64 | title={asset?.name} |
42 | 65 | > |
43 | 66 | <span className="truncate">{asset?.name}</span> |
|
0 commit comments