Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/Welcome/Views/RecentProjectsListView.swift (Swift)

Code before:

import SwiftUI
import CoreSpotlight

struct RecentProjectsListView: View {

    @State private var selection: Set<URL>
    @State var recentProjects: [URL]

    private let openDocument: (URL?, @escaping () -> Void) -> Void
    private let dismissWindow: () -> Void

    init(openDocument: @escaping (URL?, @escaping () -> Void) -> Void, dismissWindow: @escaping () -> Void) {
        self.openDocument = openDocument
        self.dismissWindow = dismissWindow

        let recentProjectPaths: [String] = UserDefaults.standard.array(
            forKey: "recentProjectPaths"
        ) as? [String] ?? []
        let projectsURL = recentProjectPaths.map { URL(filePath: $0) }
        _selection = .init(initialValue: Set(projectsURL.prefix(1)))
        _recentProjects = .init(initialValue: projectsURL)
        donateSearchableItems()
    }
    func donateSearchableItems() {
        let searchableItems = recentProjects.map { entity -> CSSearchableItem in
            let attributeSet = CSSearchableItemAttributeSet(contentType: .content)
            attributeSet.title = String(entity.lastPathComponent)
            attributeSet.relatedUniqueIdentifier = String(entity.path())
            return CSSearchableItem(
                uniqueIdentifier: String(entity.path()),
                domainIdentifier: "app.codeedit.CodeEdit.ProjectItem",
                attributeSet: attributeSet
            )
        }
        CSSearchableIndex.default().indexSearchableItems(searchableItems)
    }

    var listEmptyView: some View {
        VStack {
            Spacer()
            Text(NSLocalizedString("No Recent Projects", comment: ""))
                .font(.body)
                .foregroundColor(.secondary)
            Spacer()
        }
    }

    var body: some View {
        List(recentProjects, id: \.self, selection: $selection) { project in
            RecentProjectItem(projectPath: project)

Review comment (suggestion):
Remove the `CSSearchableItem` type. The `recentProjects` variable is a `[URL]`.

Code after:

import SwiftUI
import CoreSpotlight

struct RecentProjectsListView: View {

    @State private var selection: Set<URL>
    @State var recentProjects: [URL]

    private let openDocument: (URL?, @escaping () -> Void) -> Void
    private let dismissWindow: () -> Void

    init(openDocument: @escaping (URL?, @escaping () -> Void) -> Void, dismissWindow: @escaping () -> Void) {
        self.openDocument = openDocument
        self.dismissWindow = dismissWindow

        let recentProjectPaths: [String] = UserDefaults.standard.array(
            forKey: "recentProjectPaths"
        ) as? [String] ?? []
        let projectsURL = recentProjectPaths.map { URL(filePath: $0) }
        _selection = .init(initialValue: Set(projectsURL.prefix(1)))
        _recentProjects = .init(initialValue: projectsURL)
        donateSearchableItems()
    }

    var listEmptyView: some View {
        VStack {
            Spacer()
            Text(NSLocalizedString("No Recent Projects", comment: ""))
                .font(.body)
                .foregroundColor(.secondary)
            Spacer()
        }
    }

    var body: some View {
        List(recentProjects, id: \.self, selection: $selection) { project in
            RecentProjectItem(projectPath: project)
        }
        .listStyle(.sidebar)
        .contextMenu(forSelectionType: URL.self) { items in
            switch items.count {
            case 0:
                EmptyView()
            default:
                Button("Show in Finder") {
                    NSWorkspace.shared.activateFileViewerSelecting(Array(items))
                }

                Button("Copy path\(items.count > 1 ? "s" : "")") {
                    let pasteBoard = NSPasteboard.general