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

Code before:

    @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)
        }
        .listStyle(.sidebar)
        .contextMenu(forSelectionType: URL.self) { items in
            switch items.count {
            case 0:

Review comment (suggestion):
`entity.path()` is already a String, no need to cast

Code after:

    @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
                    pasteBoard.clearContents()
                    pasteBoard.writeObjects(selection.map(\.relativePath) as [NSString])
                }

                Button("Remove from Recents") {